-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-migration.js
More file actions
36 lines (29 loc) · 927 Bytes
/
Copy pathrun-migration.js
File metadata and controls
36 lines (29 loc) · 927 Bytes
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
const { Client } = require('pg');
const fs = require('fs');
const path = require('path');
const envContent = fs.readFileSync(path.resolve(__dirname, '.env'), 'utf8');
const dbUrlMatch = envContent.match(/DATABASE_URL="([^"]+)"/);
let dbUrl = dbUrlMatch ? dbUrlMatch[1] : null;
if (dbUrl) {
dbUrl = dbUrl.replace(/\\(\$)/g, '$1');
}
async function migrate() {
const client = new Client({
connectionString: dbUrl,
ssl: { rejectUnauthorized: false }
});
try {
await client.connect();
console.log('Connected to database.');
const sqlPath = path.join(__dirname, 'database', 'migration_cv.sql');
const sql = fs.readFileSync(sqlPath, 'utf8');
console.log('Executing migration...');
await client.query(sql);
console.log('Migration completed successfully!');
} catch (err) {
console.error('Migration failed:', err);
} finally {
await client.end();
}
}
migrate();