From a2f6f78c6aacdef6e7b98854f462bbf50e6aa49b Mon Sep 17 00:00:00 2001 From: Ben Davis <45952064+bmdavis419@users.noreply.github.com> Date: Mon, 27 Jul 2026 15:54:01 -0700 Subject: [PATCH] fix: make migrations 036/037 idempotent like 035 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 036 and 037 issued bare ALTER TABLE ... ADD COLUMN statements, which SQLite rejects with "duplicate column name" when the column already exists (e.g. a database touched by a divergent branch or a restored backup whose tracking table lags the schema). Mirror 035's PRAGMA table_info guard so re-running either migration is a no-op. Each test file gains an idempotence case that re-applies the migration effect directly against the already-migrated shared in-memory database — going through runMigrations twice would be short-circuited by the migrator's tracking table and prove nothing. Co-Authored-By: Claude Fable 5 --- ...rojectionThreadMessageNotification.test.ts | 20 ++++++++++++++++ ...036_ProjectionThreadMessageNotification.ts | 13 +++++++---- ...rojectionThreadsLatestNotification.test.ts | 23 +++++++++++++++++++ ...037_ProjectionThreadsLatestNotification.ts | 13 +++++++---- 4 files changed, 61 insertions(+), 8 deletions(-) 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 + `; + } });