|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect, Predicate } from "effect"; |
| 3 | + |
| 4 | +import { |
| 5 | + DataMigrationError, |
| 6 | + runSqliteDataMigrations, |
| 7 | + sqliteDataMigration, |
| 8 | + type SqliteDataMigration, |
| 9 | + type SqliteDataMigrationClient, |
| 10 | +} from "./sqlite-data-migrations"; |
| 11 | + |
| 12 | +// A tiny scripted fake standing in for a libSQL client: tracks statements |
| 13 | +// and simulates the ledger table. |
| 14 | +const makeFakeClient = (stampedNames: string[]) => { |
| 15 | + const log: unknown[] = []; |
| 16 | + const stamps = [...stampedNames]; |
| 17 | + const client: SqliteDataMigrationClient = { |
| 18 | + execute: (stmt) => { |
| 19 | + log.push(stmt); |
| 20 | + if (typeof stmt === "string" && stmt.startsWith("SELECT name FROM data_migration")) { |
| 21 | + return Promise.resolve({ rows: stamps.map((name) => ({ name })) }); |
| 22 | + } |
| 23 | + if (typeof stmt === "object" && stmt.sql.startsWith("INSERT INTO data_migration")) { |
| 24 | + stamps.push(String(stmt.args[0])); |
| 25 | + } |
| 26 | + return Promise.resolve({ rows: [] }); |
| 27 | + }, |
| 28 | + }; |
| 29 | + return { client, log, stamps }; |
| 30 | +}; |
| 31 | + |
| 32 | +const migrationSpy = (name: string) => { |
| 33 | + const calls: number[] = []; |
| 34 | + const migration: SqliteDataMigration = { |
| 35 | + name, |
| 36 | + run: () => Effect.sync(() => void calls.push(calls.length)), |
| 37 | + }; |
| 38 | + return { migration, calls }; |
| 39 | +}; |
| 40 | + |
| 41 | +describe("runSqliteDataMigrations", () => { |
| 42 | + it.effect("runs pending migrations in order and stamps them", () => |
| 43 | + Effect.gen(function* () { |
| 44 | + const { client, log, stamps } = makeFakeClient([]); |
| 45 | + const a = migrationSpy("2026-06-05-a"); |
| 46 | + const b = migrationSpy("2026-06-11-b"); |
| 47 | + const applied = yield* runSqliteDataMigrations(client, [a.migration, b.migration]); |
| 48 | + expect(applied).toEqual(["2026-06-05-a", "2026-06-11-b"]); |
| 49 | + expect(a.calls.length).toBe(1); |
| 50 | + expect(b.calls.length).toBe(1); |
| 51 | + expect(stamps).toEqual(["2026-06-05-a", "2026-06-11-b"]); |
| 52 | + expect(String(log[0])).toContain("CREATE TABLE IF NOT EXISTS data_migration"); |
| 53 | + }), |
| 54 | + ); |
| 55 | + |
| 56 | + it.effect("skips stamped migrations without running them", () => |
| 57 | + Effect.gen(function* () { |
| 58 | + const { client } = makeFakeClient(["2026-06-05-a"]); |
| 59 | + const a = migrationSpy("2026-06-05-a"); |
| 60 | + const b = migrationSpy("2026-06-11-b"); |
| 61 | + const applied = yield* runSqliteDataMigrations(client, [a.migration, b.migration]); |
| 62 | + expect(applied).toEqual(["2026-06-11-b"]); |
| 63 | + expect(a.calls.length).toBe(0); |
| 64 | + expect(b.calls.length).toBe(1); |
| 65 | + }), |
| 66 | + ); |
| 67 | + |
| 68 | + it.effect("is a no-op once everything is stamped", () => |
| 69 | + Effect.gen(function* () { |
| 70 | + const { client } = makeFakeClient([]); |
| 71 | + const a = migrationSpy("2026-06-05-a"); |
| 72 | + yield* runSqliteDataMigrations(client, [a.migration]); |
| 73 | + const applied = yield* runSqliteDataMigrations(client, [a.migration]); |
| 74 | + expect(applied).toEqual([]); |
| 75 | + expect(a.calls.length).toBe(1); |
| 76 | + }), |
| 77 | + ); |
| 78 | + |
| 79 | + it.effect("a failing migration leaves no stamp and surfaces the failure", () => |
| 80 | + Effect.gen(function* () { |
| 81 | + const { client, stamps } = makeFakeClient([]); |
| 82 | + const boom: SqliteDataMigration = { |
| 83 | + name: "2026-06-11-boom", |
| 84 | + run: () => |
| 85 | + Effect.fail(new DataMigrationError({ migration: "2026-06-11-boom", cause: "nope" })), |
| 86 | + }; |
| 87 | + const after = migrationSpy("2026-06-12-after"); |
| 88 | + const failure = yield* runSqliteDataMigrations(client, [boom, after.migration]).pipe( |
| 89 | + Effect.flip, |
| 90 | + ); |
| 91 | + expect(Predicate.isTagged(failure, "DataMigrationError")).toBe(true); |
| 92 | + expect(stamps).toEqual([]); |
| 93 | + expect(after.calls.length).toBe(0); |
| 94 | + }), |
| 95 | + ); |
| 96 | + |
| 97 | + it.effect("rejects duplicate names before touching the database", () => |
| 98 | + Effect.gen(function* () { |
| 99 | + const { client, log } = makeFakeClient([]); |
| 100 | + const a1 = migrationSpy("2026-06-05-a"); |
| 101 | + const a2 = migrationSpy("2026-06-05-a"); |
| 102 | + const failure = yield* runSqliteDataMigrations(client, [a1.migration, a2.migration]).pipe( |
| 103 | + Effect.flip, |
| 104 | + ); |
| 105 | + expect(Predicate.isTagged(failure, "DuplicateDataMigrationError")).toBe(true); |
| 106 | + expect(log).toEqual([]); |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + it.effect("sqliteDataMigration adapts promise-shaped bodies with typed failures", () => |
| 111 | + Effect.gen(function* () { |
| 112 | + const { client, stamps } = makeFakeClient([]); |
| 113 | + let ran = 0; |
| 114 | + const ok = sqliteDataMigration("2026-06-05-promise", () => { |
| 115 | + ran++; |
| 116 | + return Promise.resolve(42); |
| 117 | + }); |
| 118 | + yield* runSqliteDataMigrations(client, [ok]); |
| 119 | + expect(ran).toBe(1); |
| 120 | + expect(stamps).toEqual(["2026-06-05-promise"]); |
| 121 | + |
| 122 | + // oxlint-disable-next-line executor/no-promise-reject -- simulates a raw driver rejection at the adapter boundary under test |
| 123 | + const bad = sqliteDataMigration("2026-06-12-bad", () => Promise.reject("disk full")); |
| 124 | + const failure = yield* runSqliteDataMigrations(client, [ok, bad]).pipe(Effect.flip); |
| 125 | + expect(Predicate.isTagged(failure, "DataMigrationError")).toBe(true); |
| 126 | + expect((failure as DataMigrationError).migration).toBe("2026-06-12-bad"); |
| 127 | + expect(ran).toBe(1); |
| 128 | + }), |
| 129 | + ); |
| 130 | +}); |
0 commit comments