-
Notifications
You must be signed in to change notification settings - Fork 5
/
migrateUpWithWrapper.mjs
39 lines (33 loc) · 1.03 KB
/
migrateUpWithWrapper.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Hacky script that wraps the 'node-pg-migrate' program
* and make sure that logged errors do not leak some
* sensitive information such as db password for instance..
* */
import dotenv from 'dotenv';
import { spawn } from 'child_process';
dotenv.config();
const dbUser = process.env.PGUSER;
const dbPassword = process.env.PGPASSWORD;
const child = spawn('npm', ['run', 'migrate', 'up']);
child.stdout.on('data', (data) => {
console.log(`${data}`);
});
let hasError = false;
child.stderr.on('data', (data) => {
hasError = true;
if (dbPassword && data.includes(dbPassword)) {
const message = Buffer.from(data).toString('utf8');
console.error(message.replaceAll(dbPassword, '*****').replaceAll(dbUser, '*****'));
} else {
console.error(`${data}`);
}
});
child.on('exit', () => {
if (hasError) {
console.log("Error(s) occurred during the migration");
//Important to notify that error(s) occurred.
return process.exit(2);
} else {
return process.exit(0);
}
});