-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetup-db.js
More file actions
40 lines (32 loc) · 1.24 KB
/
setup-db.js
File metadata and controls
40 lines (32 loc) · 1.24 KB
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
const { execSync } = require('child_process');
const postgres = require('postgres');
const fs = require('fs');
const path = require('path');
// Load environment variables
require('dotenv').config({ path: '.env.local' });
const POSTGRES_URL = process.env.POSTGRES_URL || 'postgresql://studio_user:studio_password@localhost:5432/studio_db';
async function runMigrations() {
console.log('Connecting to database...');
const sql = postgres(POSTGRES_URL);
try {
// Read and execute migration files
const migrationsPath = path.join(__dirname, 'lib', 'db', 'migrations');
const migrationFiles = fs.readdirSync(migrationsPath)
.filter(file => file.endsWith('.sql'))
.sort();
console.log(`Found ${migrationFiles.length} migration files`);
for (const file of migrationFiles) {
console.log(`Running migration: ${file}`);
const migrationSQL = fs.readFileSync(path.join(migrationsPath, file), 'utf8');
await sql.unsafe(migrationSQL);
console.log(`✓ Migration ${file} completed`);
}
console.log('All migrations completed successfully!');
} catch (error) {
console.error('Migration error:', error.message);
process.exit(1);
} finally {
await sql.end();
}
}
runMigrations();