Merge pull request 'Fix db:export ENOBUFS by streaming pg_dump output to file' (#17) from dev into main

Reviewed-on: #17
This commit is contained in:
2026-03-12 19:18:57 +00:00

View File

@@ -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,11 +43,13 @@ function exportSqlite(outputPath: string): void {
function exportPostgres(outputPath: string): void {
const connString = process.env.DATABASE_URL || 'postgresql://localhost:5432/spanglish';
const outFd = openSync(outputPath, 'w');
try {
const result = spawnSync(
'pg_dump',
['--clean', '--if-exists', connString],
{
stdio: ['ignore', 'pipe', 'pipe'],
stdio: ['ignore', outFd, 'pipe'],
encoding: 'utf-8',
}
);
@@ -63,8 +65,10 @@ function exportPostgres(outputPath: string): void {
process.exit(1);
}
writeFileSync(outputPath, result.stdout);
console.log(`Exported to ${outputPath}`);
} finally {
closeSync(outFd);
}
}
async function main() {