fix(#14): Implement database migrations system with baseline migration
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit is contained in:
parent
2f2ea4d483
commit
3916dd42bf
5 changed files with 248 additions and 23 deletions
|
|
@ -1,29 +1,18 @@
|
|||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { runMigrations } from '../../migrations/runner.js';
|
||||
import { pool } from './connection.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const run = async () => {
|
||||
const schemaPath = path.resolve(__dirname, '../../sql/schema.sql');
|
||||
const sql = await fs.readFile(schemaPath, 'utf8');
|
||||
const statements = sql
|
||||
.split(';')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
for (const statement of statements) {
|
||||
await pool.query(statement);
|
||||
try {
|
||||
// Run migrations
|
||||
await runMigrations();
|
||||
|
||||
console.log('Database initialized with migrations.');
|
||||
await pool.end();
|
||||
} catch (err) {
|
||||
console.error('Database init failed:', err.message);
|
||||
await pool.end();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Applied ${statements.length} SQL statements.`);
|
||||
await pool.end();
|
||||
};
|
||||
|
||||
run().catch(async (err) => {
|
||||
console.error('Database init failed:', err.message);
|
||||
await pool.end();
|
||||
process.exit(1);
|
||||
});
|
||||
run();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue