project-bifrost-platform/scripts/migrate.js
Arlind fab927884c fix(db): migrate.js honors BIFROST_DB_PATH
So production migrations hit the same SQLite file the running app uses
(src/lib/db.ts), instead of a repo-local bifrost.db. Mirrors the pattern
already in seed-production.js and seed-roadmap.js. Falls back to the dev
db when unset.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 12:54:22 +02:00

42 lines
1.3 KiB
JavaScript

#!/usr/bin/env node
import Database from 'better-sqlite3';
import { readFileSync, readdirSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Honor BIFROST_DB_PATH in production so migrations hit the same file the
// running app uses (see src/lib/db.ts). Falls back to the repo-local dev db.
const dbPath = process.env.BIFROST_DB_PATH ?? join(__dirname, '..', 'bifrost.db');
const migrationsDir = join(__dirname, '..', 'migrations');
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON');
db.exec(`CREATE TABLE IF NOT EXISTS _migrations (
id TEXT PRIMARY KEY,
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
)`);
const applied = new Set(
db.prepare('SELECT id FROM _migrations').all().map(r => r.id)
);
const files = readdirSync(migrationsDir)
.filter(f => f.endsWith('.sql'))
.sort();
let count = 0;
for (const file of files) {
if (applied.has(file)) continue;
const sql = readFileSync(join(migrationsDir, file), 'utf8');
db.exec(sql);
db.prepare('INSERT INTO _migrations (id) VALUES (?)').run(file);
console.log(` applied: ${file}`);
count++;
}
if (count === 0) console.log(' nothing to apply — already up to date');
else console.log(`\n ${count} migration(s) applied.`);
db.close();