@launchql/migrate
Version:
PostgreSQL Migration Tools
44 lines (43 loc) • 1.18 kB
JavaScript
import { getSpawnEnvWithPg } from '@launchql/types';
import { spawn } from 'child_process';
import { Readable } from 'stream';
function setArgs(config) {
const args = [
'-U', config.user,
'-h', config.host,
'-d', config.database
];
if (config.port) {
args.push('-p', String(config.port));
}
return args;
}
// Converts a string to a readable stream (replaces streamify-string)
function stringToStream(text) {
const stream = new Readable({
read() {
this.push(text);
this.push(null);
}
});
return stream;
}
export async function streamSql(config, sql) {
const args = setArgs(config);
return new Promise((resolve, reject) => {
const sqlStream = stringToStream(sql);
const proc = spawn('psql', args, {
env: getSpawnEnvWithPg(config)
});
sqlStream.pipe(proc.stdin);
proc.on('close', (code) => {
resolve();
});
proc.on('error', (error) => {
reject(error);
});
proc.stderr.on('data', (data) => {
reject(new Error(data.toString()));
});
});
}