fix: make migrations 036/037 idempotent like 035

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 <[email protected]>
This commit is contained in:
Ben DavisandClaude Fable 5 committed 2026-07-27 15:54:01 -07:00
1 parent 4659e0d9e7
commit a2f6f78c6a
4 files changed
+61 -8

No files matched your search

@@ -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);
}),
);
});
@@ -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
`;
}
});
@@ -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,
);
}),
);
});
@@ -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
`;
}
});