-
Notifications
You must be signed in to change notification settings - Fork 5
/
migrate.ts
54 lines (44 loc) · 1.38 KB
/
migrate.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
44
45
46
47
48
49
50
51
52
53
54
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
import { config } from 'dotenv';
config();
const DB_USER = process.env.DB_USER;
const DB_HOST = process.env.DB_HOST;
const DB_PORT = parseInt(process.env.DB_PORT || '0', 10);
const DB_PASS = process.env.DB_PASS;
const DB_NAME = process.env.DB_NAME;
const Option = {
max: 1,
host: DB_HOST,
user: DB_USER,
pass: DB_PASS,
database: DB_NAME,
port: DB_PORT,
};
export const connectionString = `postgres://${DB_USER}:${DB_PASS}@localhost:${DB_PORT}/${DB_NAME}`;
const sql = postgres(connectionString, Option);
const db = drizzle(sql);
import * as readline from 'readline';
async function main() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log(`Migrating to ${DB_HOST} on Database ${DB_NAME}`);
rl.question('Are you sure you want to migrate? (y/n) ', async (answer) => {
if (answer.toLowerCase() === 'y') {
console.log('Migrating Started');
await migrate(db, { migrationsFolder: './migrations' });
console.log('Migration Finish');
process.exit();
} else {
console.log('Migration cancelled');
rl.close();
}
});
}
// Call the async function to start your script
main().catch((error) => {
console.error('An error occurred:', error);
});