-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset.js
34 lines (28 loc) · 909 Bytes
/
reset.js
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
/*
* WARN:
* This file will reset your database specified in ./app.js
*/
import pg from 'pg';
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
dotenv.config();
const db = new pg.Client({
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
}); await db.connect();
const tables = fs.readFileSync(path.join(path.resolve(), 'tables.sql'), 'utf8').split("---table_separator---")
for (let table of tables) {
const tablename = table.split("TABLE IF NOT EXISTS ")[1].split(" ")[0]
try {
await db.query(`DROP TABLE IF EXISTS ${tablename} CASCADE`)
} catch (e) {
console.log(`> did not drop table ${tablename}`)
}
await db.query(table)
console.log(`> created table ${tablename}`)
}
process.exit(0)