-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrations.ts
43 lines (34 loc) · 1.37 KB
/
migrations.ts
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
40
41
42
43
require('dotenv').config();
const PQuery = require('prettyquery');
async function up(pQuery: any, loud: boolean): Promise<void> {
if (loud) console.log('Migrating for gratitude');
await pQuery.query('CREATE TABLE gratitude (id INTEGER PRIMARY KEY AUTO_INCREMENT, grateful_for VARCHAR(255), datetime_submitted DATETIME DEFAULT (NOW()));');
}
async function down(pQuery: any, loud: boolean): Promise<void> {
await dropTable(loud, pQuery, 'gratitude');
}
async function dropTable(loud: boolean, pQuery: any, table_name: string) {
if (loud)
console.log(`Dropping for ${table_name}`);
await pQuery.query(`DROP TABLE IF EXISTS ${table_name};`);
}
async function refresh(pQuery: any, loud: boolean): Promise<void> {
await down(pQuery, loud);
await up(pQuery, loud);
}
async function main(isTest:boolean, loud: boolean): Promise<void> {
if (loud) console.log('The MODE is ===', process.env.MODE);
let pQuery = new PQuery({user: process.env.DB_USER, password: process.env.DB_PASSWORD, host: 'localhost'});
await pQuery.useDb('gratitude');
await refresh(pQuery, loud);
pQuery.connection.end();
}
if (require.main === module) {
if (/l|(loud)/i.test(process.argv[2])) {
main('loud').catch(err => console.error(err));
} else {
main().catch(err => console.error(err));
}
}
delete require.cache[module.id];
module.exports = main;