import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const HERE = path.dirname(fileURLToPath(import.meta.url)); const MIGRATIONS_DIR = path.join(HERE, 'migrations'); export function migrate(db, { migrationsDir = MIGRATIONS_DIR } = {}) { db.exec(` CREATE TABLE IF NOT EXISTS schema_migrations ( version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL ); `); const applied = new Set( db.prepare('SELECT version FROM schema_migrations').all().map((row) => row.version) ); const files = fs .readdirSync(migrationsDir) .filter((name) => /^\d+_.+\.sql$/.test(name)) .sort((a, b) => a.localeCompare(b)); const insert = db.prepare( 'INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)' ); let count = 0; for (const file of files) { const version = Number.parseInt(file.split('_')[0], 10); if (applied.has(version)) continue; const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8'); const run = db.transaction(() => { db.exec(sql); insert.run(version, file, new Date().toISOString()); }); run(); count += 1; } return count; }