From f8ebc3760d10c4b42544668fd70a0702148e5e3b Mon Sep 17 00:00:00 2001 From: Michilis Date: Thu, 12 Mar 2026 19:18:24 +0000 Subject: [PATCH] Fix db:export ENOBUFS by streaming pg_dump output to file Made-with: Cursor --- backend/src/db/export.ts | 44 ++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/backend/src/db/export.ts b/backend/src/db/export.ts index b6d347c..976cec2 100644 --- a/backend/src/db/export.ts +++ b/backend/src/db/export.ts @@ -1,5 +1,5 @@ import 'dotenv/config'; -import { existsSync, mkdirSync, writeFileSync } from 'fs'; +import { closeSync, existsSync, mkdirSync, openSync } from 'fs'; import { dirname, resolve } from 'path'; import { spawnSync } from 'child_process'; import Database from 'better-sqlite3'; @@ -43,28 +43,32 @@ function exportSqlite(outputPath: string): void { function exportPostgres(outputPath: string): void { const connString = process.env.DATABASE_URL || 'postgresql://localhost:5432/spanglish'; - const result = spawnSync( - 'pg_dump', - ['--clean', '--if-exists', connString], - { - stdio: ['ignore', 'pipe', 'pipe'], - encoding: 'utf-8', + const outFd = openSync(outputPath, 'w'); + try { + const result = spawnSync( + 'pg_dump', + ['--clean', '--if-exists', connString], + { + stdio: ['ignore', outFd, 'pipe'], + encoding: 'utf-8', + } + ); + + if (result.error) { + console.error('pg_dump failed. Ensure pg_dump is installed and in PATH.'); + console.error(result.error.message); + process.exit(1); } - ); - if (result.error) { - console.error('pg_dump failed. Ensure pg_dump is installed and in PATH.'); - console.error(result.error.message); - process.exit(1); + if (result.status !== 0) { + console.error('pg_dump failed:', result.stderr); + process.exit(1); + } + + console.log(`Exported to ${outputPath}`); + } finally { + closeSync(outFd); } - - if (result.status !== 0) { - console.error('pg_dump failed:', result.stderr); - process.exit(1); - } - - writeFileSync(outputPath, result.stdout); - console.log(`Exported to ${outputPath}`); } async function main() {