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:
@@ -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,28 +43,32 @@ 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 result = spawnSync(
|
const outFd = openSync(outputPath, 'w');
|
||||||
'pg_dump',
|
try {
|
||||||
['--clean', '--if-exists', connString],
|
const result = spawnSync(
|
||||||
{
|
'pg_dump',
|
||||||
stdio: ['ignore', 'pipe', 'pipe'],
|
['--clean', '--if-exists', connString],
|
||||||
encoding: 'utf-8',
|
{
|
||||||
|
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) {
|
if (result.status !== 0) {
|
||||||
console.error('pg_dump failed. Ensure pg_dump is installed and in PATH.');
|
console.error('pg_dump failed:', result.stderr);
|
||||||
console.error(result.error.message);
|
process.exit(1);
|
||||||
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() {
|
async function main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user