CI / install-and-build (pull_request) Successful in 1m29s
CI / format (pull_request) Successful in 51s
CI / typecheck-source (pull_request) Successful in 41s
CI / typecheck-tests (pull_request) Successful in 39s
CI / test (pull_request) Successful in 51s
CI / lint (pull_request) Successful in 21s
Co-Authored-By: gpt-5.6-terra <noreply@openai.com>
86 lines
3.1 KiB
TypeScript
86 lines
3.1 KiB
TypeScript
import { describe, it, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { WrappedNodeSqliteDb } from "../../../src/db/sqlite/impl/node-sqlite.js";
|
|
import { SqliteMigrator } from "../../../src/db/sqlite/SqliteMigrator.js";
|
|
import { migrations } from "../../../src/db/sqlite/migrations/index.js";
|
|
|
|
describe("SqliteMigrator", () => {
|
|
it("listAvailableMigrations returns all known migrations", () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
const available = migrator.listAvailableMigrations();
|
|
assert.ok(Array.isArray(available));
|
|
assert.ok(available.length >= 3, "at least 3 migrations");
|
|
const ids = available.map((m) => m.id);
|
|
assert.ok(ids.includes(1), "migration 1 present");
|
|
assert.ok(ids.includes(2), "migration 2 present");
|
|
assert.ok(ids.includes(3), "migration 3 present");
|
|
db.destroy();
|
|
});
|
|
|
|
it("listAppliedMigrations returns empty array on fresh db", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.deepEqual(applied, []);
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo(1) applies first migration", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await migrator.migrateTo(1);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.equal(applied.length, 1);
|
|
assert.equal(applied[0].id, 1);
|
|
assert.ok(applied[0].name, "migration has a name");
|
|
assert.ok(applied[0].applied_at, "migration has applied_at");
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo(3) applies all three migrations in order", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await migrator.migrateTo(3);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.equal(applied.length, 3);
|
|
assert.deepEqual(
|
|
applied.map((m) => m.id),
|
|
[1, 2, 3],
|
|
);
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo(3) twice is idempotent (nothing to do)", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await migrator.migrateTo(3);
|
|
await migrator.migrateTo(3);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.equal(applied.length, 3);
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo with unknown id throws", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await assert.rejects(
|
|
() => migrator.migrateTo(9999),
|
|
/No known migration with id 9999/,
|
|
);
|
|
db.destroy();
|
|
});
|
|
|
|
it("listAvailableMigrations names match migration objects", () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
const available = migrator.listAvailableMigrations();
|
|
for (const { id, name } of available) {
|
|
const migration = migrations.find((m) => m.id === id);
|
|
assert.ok(migration, `migration ${id} exists`);
|
|
assert.equal(migration.name, name);
|
|
}
|
|
db.destroy();
|
|
});
|
|
});
|