Fix db:export ENOBUFS by streaming pg_dump output to file

Made-with: Cursor
This commit is contained in:
Michilis
2026-03-12 19:18:24 +00:00
parent 4da26e7ef1
commit f8ebc3760d

View File

@@ -1,5 +1,5 @@
import 'dotenv/config'; import 'dotenv/config';
import { existsSync, mkdirSync, writeFileSync } from 'fs'; import { closeSync, existsSync, mkdirSync, openSync } from 'fs';
import { dirname, resolve } from 'path'; import { dirname, resolve } from 'path';
import { spawnSync } from 'child_process'; import { spawnSync } from 'child_process';
import Database from 'better-sqlite3'; import Database from 'better-sqlite3';
@@ -43,11 +43,13 @@ function exportSqlite(outputPath: string): void {
function exportPostgres(outputPath: string): void { function exportPostgres(outputPath: string): void {
const connString = process.env.DATABASE_URL || 'postgresql://localhost:5432/spanglish'; const connString = process.env.DATABASE_URL || 'postgresql://localhost:5432/spanglish';
const outFd = openSync(outputPath, 'w');
try {
const result = spawnSync( const result = spawnSync(
'pg_dump', 'pg_dump',
['--clean', '--if-exists', connString], ['--clean', '--if-exists', connString],
{ {
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', outFd, 'pipe'],
encoding: 'utf-8', encoding: 'utf-8',
} }
); );
@@ -63,8 +65,10 @@ function exportPostgres(outputPath: string): void {
process.exit(1); process.exit(1);
} }
writeFileSync(outputPath, result.stdout);
console.log(`Exported to ${outputPath}`); console.log(`Exported to ${outputPath}`);
} finally {
closeSync(outFd);
}
} }
async function main() { async function main() {