First commit, phased 1 and 2 created but untested

This commit is contained in:
bepis
2026-10-20 07:40:35 +11:00
commit 7a3e408ffc
66 changed files with 5639 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
const path = require('path');
const fs = require('fs');
const { getDb } = require('../src/db');
const migrationsDir = path.resolve(__dirname, '..', 'src', 'db', 'migrations');
function runMigrations() {
const db = getDb();
// Create _migrations table if it doesn't exist
db.exec(`
CREATE TABLE IF NOT EXISTS _migrations (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
// Get already-applied migrations
const applied = new Set(
db.prepare('SELECT name FROM _migrations').pluck().all()
);
// Find migration files
const files = fs.readdirSync(migrationsDir)
.filter(f => f.endsWith('.sql'))
.sort();
for (const file of files) {
if (applied.has(file)) {
console.log(` SKIP ${file} (already applied)`);
continue;
}
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf-8');
console.log(` APPLY ${file}`);
// Wrap in a transaction
db.transaction(() => {
db.exec(sql);
db.prepare('INSERT INTO _migrations (name) VALUES (?)').run(file);
})();
}
console.log('Migrations complete.');
}
// Run if called directly
if (require.main === module) {
runMigrations();
process.exit(0);
}
module.exports = { runMigrations };