import assert from "node:assert/strict"; import fs from "node:fs"; import sqlite from "../../src/sqlite.cjs"; import * as runner from "../runner.mjs"; import * as db from "../../src/db.mjs"; import * as u from "../../src/utils.mjs"; import { MIGRATIONS_DIR, runMigrations, } from "../../src/accretion.mjs"; const test_runMigrations = async t => { t.start("runMigrations()"); await t.test("running twice is a noop", async () => { const contents = []; const logFn = x => contents.push(x); const handle = await db.open(":memory:"); const migrationsFn = () => handle.all("SELECT filename FROM migrations;"); assert.rejects( migrationsFn, { message: "SQLITE_ERROR: no such table: migrations" }, ); await runMigrations(logFn, handle); const filled = await migrationsFn(); await runMigrations(logFn, handle); const unchanged = await migrationsFn(); assert.deepEqual(filled, unchanged); const migrationLogs = fs .readdirSync(MIGRATIONS_DIR) .sort(u.strSortFn) .map(filename => ({ log: "exec-migration", filename })); assert.deepEqual(contents, migrationLogs); }); }; await runner.runTests([ test_runMigrations, ]);