Files
sqlite-migrate/dist/cli.js
Brendan Chen accd3ca340
All checks were successful
ci / test (pull_request) Successful in 17s
build: regenerate dist/ for v0.1.0
Pre-compiled ESM JS + .d.ts from tsconfig.build.json. Consumers install
via 'git+https://gitea.bchen.dev/...#v0.1.0' so the built output must
travel with the source. CI verifies in-sync via 'git diff --exit-code
dist/'.
2026-05-12 02:17:02 -07:00

72 lines
2.5 KiB
JavaScript

import { applyMigrations, listMigrations, readAppliedRows, stampMigration, } from './migrate.js';
export function runMigrateCli(opts) {
const stdout = opts.stdout ?? process.stdout;
const stderr = opts.stderr ?? process.stderr;
if (opts.command === 'stamp') {
if (!opts.version) {
stderr.write('error: version required\n');
return 1;
}
const db = opts.openDb();
try {
const file = stampMigration(db, opts.migrationsDir, opts.version);
stdout.write(`stamped:${file.version}\n`);
db.close();
return 0;
}
catch (err) {
const msg = err instanceof Error ? err.message : String(err);
stderr.write(`error: ${msg}\n`);
db.close();
return 1;
}
}
if (opts.command === 'status') {
const db = opts.openDb();
const files = listMigrations(opts.migrationsDir);
const appliedByVersion = new Map(readAppliedRows(db).map((r) => [r.version, r]));
let exitCode = 0;
const pending = [];
const mismatched = [];
for (const file of files) {
const applied = appliedByVersion.get(file.version);
if (!applied) {
pending.push(file.version);
stdout.write(`pending:${file.version}\n`);
continue;
}
if (applied.checksum !== file.checksum) {
mismatched.push(file.version);
stdout.write(`checksum-mismatch:${file.version}\n`);
exitCode = 2;
continue;
}
stdout.write(`applied:${file.version}\n`);
}
if (pending.length === 0 && mismatched.length === 0) {
stdout.write(`pending:none\n`);
}
db.close();
return exitCode;
}
// migrate
const db = opts.openDb();
const summary = applyMigrations(db, opts.migrationsDir, {
stampGenesis: opts.stampGenesis,
genesisProbeTable: opts.genesisProbeTable,
logger: (msg) => stdout.write(`${msg}\n`),
});
if (summary.stamped.length > 0) {
stdout.write(`stamped:${summary.stamped.join(',')}\n`);
}
if (summary.applied === 0 && summary.stamped.length === 0) {
stdout.write(`pending:none\n`);
}
else if (summary.applied > 0) {
stdout.write(`applied:${summary.applied}\n`);
}
stdout.write(`migrations: ${summary.applied + summary.alreadyApplied} applied, ${summary.pending} pending\n`);
db.close();
return 0;
}