Phase 3 of the cross-project sqlite-migrate adoption — port nanodrop to
consume bchen-sqlite-migrate@v0.1.0. Replaces the inline db.exec(...)
block in src/db/schema.ts with applyMigrations(db, MIGRATIONS_DIR,
{ genesisProbeTable: 'users' }).
The genesis-probe (table 'users' exists) handles pre-existing prod DBs
automatically — first deploy after merge stamps 0001_init as applied
without re-executing, subsequent boots are no-ops.
Adds three npm scripts (db:migrate, db:status, db:stamp) and a
byte-stability test pinning sha256(0001_init.sql) so the migration is
treated as immutable history.
28 lines
889 B
TypeScript
28 lines
889 B
TypeScript
import path from 'node:path';
|
|
import Database from 'better-sqlite3';
|
|
import { applyMigrations } from 'bchen-sqlite-migrate';
|
|
|
|
export const MIGRATIONS_DIR = path.resolve(import.meta.dirname, 'migrations');
|
|
|
|
export function applySchema(db: Database.Database): void {
|
|
applyMigrations(db, MIGRATIONS_DIR, { genesisProbeTable: 'users' });
|
|
}
|
|
|
|
export function initDb(dbPath: string): Database.Database {
|
|
const db = new Database(dbPath);
|
|
db.pragma('journal_mode = WAL');
|
|
db.pragma('foreign_keys = ON');
|
|
|
|
const stampGenesis = process.env.DB_MIGRATIONS_STAMP_GENESIS === '1';
|
|
const summary = applyMigrations(db, MIGRATIONS_DIR, {
|
|
stampGenesis,
|
|
genesisProbeTable: 'users',
|
|
logger: (msg) => process.stdout.write(`${msg}\n`),
|
|
});
|
|
process.stdout.write(
|
|
`migrations: ${summary.applied + summary.alreadyApplied} applied, ${summary.pending} pending\n`,
|
|
);
|
|
|
|
return db;
|
|
}
|