diff --git a/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.test.ts b/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.test.ts index f5a7bd9627..211c903ca0 100644 --- a/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.test.ts +++ b/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.test.ts @@ -7,6 +7,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient"; import { runMigrations } from "../Migrations.ts"; import * as NodeSqliteClient from "../NodeSqliteClient.ts"; +import Migration036 from "./036_ProjectionThreadMessageNotification.ts"; const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory())); @@ -106,4 +107,23 @@ layer("036_ProjectionThreadMessageNotification", (it) => { }); }), ); + + it.effect("is idempotent: re-running against a migrated database is a no-op", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // The layer shares one in-memory database across this file's tests, so + // the schema may already be at 36 here; make sure it is, then apply the + // migration effect again directly — the migrator's tracking table would + // otherwise skip it, which is exactly the protection a restored backup + // or divergent branch does not have. + yield* runMigrations({ toMigrationInclusive: 36 }); + yield* Migration036; + + const columns = yield* sql<{ readonly name: string }>` + PRAGMA table_info(projection_thread_messages) + `; + assert.equal(columns.filter((column) => column.name === "notification_json").length, 1); + }), + ); }); diff --git a/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.ts b/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.ts index 7adf2bbeb9..ae7372a412 100644 --- a/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.ts +++ b/apps/server/src/persistence/Migrations/036_ProjectionThreadMessageNotification.ts @@ -3,9 +3,14 @@ import * as Effect from "effect/Effect"; export default Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - - yield* sql` - ALTER TABLE projection_thread_messages - ADD COLUMN notification_json TEXT + const columns = yield* sql<{ readonly name: string }>` + PRAGMA table_info(projection_thread_messages) `; + + if (!columns.some((column) => column.name === "notification_json")) { + yield* sql` + ALTER TABLE projection_thread_messages + ADD COLUMN notification_json TEXT + `; + } }); diff --git a/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.test.ts b/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.test.ts index 5cec4aa89d..4a191d6df6 100644 --- a/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.test.ts +++ b/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.test.ts @@ -5,6 +5,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient"; import { runMigrations } from "../Migrations.ts"; import * as NodeSqliteClient from "../NodeSqliteClient.ts"; +import Migration037 from "./037_ProjectionThreadsLatestNotification.ts"; const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory())); @@ -77,4 +78,26 @@ layer("037_ProjectionThreadsLatestNotification", (it) => { assert.deepStrictEqual(rows, [{ latestNotification: null }]); }), ); + + it.effect("is idempotent: re-running against a migrated database is a no-op", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // The layer shares one in-memory database across this file's tests, so + // the schema may already be at 37 here; make sure it is, then apply the + // migration effect again directly — the migrator's tracking table would + // otherwise skip it, which is exactly the protection a restored backup + // or divergent branch does not have. + yield* runMigrations({ toMigrationInclusive: 37 }); + yield* Migration037; + + const columns = yield* sql<{ readonly name: string }>` + PRAGMA table_info(projection_threads) + `; + assert.equal( + columns.filter((column) => column.name === "latest_notification_json").length, + 1, + ); + }), + ); }); diff --git a/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.ts b/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.ts index 8cd62432c5..44bbc50ccb 100644 --- a/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.ts +++ b/apps/server/src/persistence/Migrations/037_ProjectionThreadsLatestNotification.ts @@ -14,9 +14,14 @@ import * as Effect from "effect/Effect"; */ export default Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - - yield* sql` - ALTER TABLE projection_threads - ADD COLUMN latest_notification_json TEXT + const columns = yield* sql<{ readonly name: string }>` + PRAGMA table_info(projection_threads) `; + + if (!columns.some((column) => column.name === "latest_notification_json")) { + yield* sql` + ALTER TABLE projection_threads + ADD COLUMN latest_notification_json TEXT + `; + } });