Skip to content

Commit 7464886

Browse files
committed
fix(server): heal skipped 045 and warn on migration slot collisions
Databases whose journal already occupied ID 45 (forks, nightlies, renumbered branches) silently skip 045_ProjectionProjectsAutoPull because effect's Migrator only compares the highest applied ID, never names. Every project query then fails with 'no such column: auto_pull' and the backend crash-loops at startup without opening a window. Add idempotent repair migration 047 that adds the column when absent, and log a loud non-fatal warning naming collided slots on every run (fatal would brick otherwise-healed databases). Production counterpart of the dev-db slot check in scripts/migrate-dev-db.ts. Fixes the migrator half of #8896. Built and verified with OpenCode + Muse Spark.
1 parent d897641 commit 7464886

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import * as Migrator from "effect/unstable/sql/Migrator";
1212
import * as Effect from "effect/Effect";
13+
import * as SqlClient from "effect/unstable/sql/SqlClient";
1314

1415
// Import all migrations statically
1516
import Migration0001 from "./Migrations/001_OrchestrationEvents.ts";
@@ -58,6 +59,7 @@ import Migration0043 from "./Migrations/043_ProjectionThreadsUnsettledAt.ts";
5859
import Migration0044 from "./Migrations/044_ClearAutomaticProjectModelDefaults.ts";
5960
import Migration0045 from "./Migrations/045_ProjectionProjectsAutoPull.ts";
6061
import Migration0046 from "./Migrations/046_RepairAutomaticSettlementTimestamps.ts";
62+
import Migration0047 from "./Migrations/047_EnsureProjectionProjectsAutoPull.ts";
6163

6264
/**
6365
* Migration loader with all migrations defined inline.
@@ -116,6 +118,7 @@ export const migrationEntries = [
116118
[44, "ClearAutomaticProjectModelDefaults", Migration0044],
117119
[45, "ProjectionProjectsAutoPull", Migration0045],
118120
[46, "RepairAutomaticSettlementTimestamps", Migration0046],
121+
[47, "EnsureProjectionProjectsAutoPull", Migration0047],
119122
] as const;
120123

121124
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
@@ -139,6 +142,40 @@ export interface RunMigrationsOptions {
139142
readonly toMigrationInclusive?: number | undefined;
140143
}
141144

145+
/**
146+
* Warn about migration slots the journal recorded under a different name.
147+
*
148+
* effect's Migrator skips every migration at or below the highest recorded ID
149+
* without comparing names, so a slot claimed by another line (fork, nightly,
150+
* renumbered branch) silently disables this checkout's migration and leaves
151+
* the schema behind the code (#8896). This is the production counterpart of
152+
* the hard failure in scripts/migrate-dev-db.ts: loud, but non-fatal, because
153+
* refusing to start would brick databases that are otherwise healed (e.g. by
154+
* repair migrations like 047).
155+
*/
156+
const warnOnMigrationSlotCollisions = Effect.gen(function* () {
157+
const sql = yield* SqlClient.SqlClient;
158+
const applied = yield* sql<{ readonly migration_id: number; readonly name: string }>`
159+
SELECT migration_id, name FROM effect_sql_migrations
160+
`;
161+
const appliedById = new Map(applied.map((row) => [Number(row.migration_id), row.name]));
162+
const collisions = migrationManifest
163+
.filter(([slot, codeName]) => {
164+
const appliedName = appliedById.get(slot);
165+
return appliedName !== undefined && appliedName !== codeName;
166+
})
167+
.map(
168+
([slot, codeName]) => `${slot} (journal: '${appliedById.get(slot)}', code: '${codeName}')`,
169+
);
170+
if (collisions.length > 0) {
171+
yield* Effect.logWarning(
172+
`Migration slot collision: this database recorded different migrations under ${collisions.join(
173+
", ",
174+
)}. The affected migrations were skipped; the schema may be behind the code. See https://github.com/pingdotgg/t3code/issues/8896`,
175+
);
176+
}
177+
});
178+
142179
/**
143180
* Run all pending migrations.
144181
*
@@ -153,6 +190,7 @@ export const runMigrations = Effect.fn("runMigrations")(function* ({
153190
toMigrationInclusive,
154191
}: RunMigrationsOptions = {}) {
155192
const executedMigrations = yield* run({ loader: makeMigrationLoader(toMigrationInclusive) });
193+
yield* warnOnMigrationSlotCollisions;
156194
const migrations = executedMigrations.map(([id, name]) => `${id}_${name}`);
157195
yield* migrations.length === 0
158196
? Effect.logDebug("Database schema is current")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import * as SqlClient from "effect/unstable/sql/SqlClient";
5+
6+
import { runMigrations } from "../Migrations.ts";
7+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8+
9+
const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory()));
10+
11+
layer("047_EnsureProjectionProjectsAutoPull", (it) => {
12+
it.effect("heals databases whose 045 was skipped by a slot collision", () =>
13+
Effect.gen(function* () {
14+
const sql = yield* SqlClient.SqlClient;
15+
16+
// Migrate to just before 045, then simulate a foreign line that claimed
17+
// ID 45 first: the journal row exists, the column does not. The
18+
// Migrator's max-ID logic then skips the real 045 forever (#8896).
19+
yield* runMigrations({ toMigrationInclusive: 44 });
20+
yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES (45, 'SomethingElse')`;
21+
22+
yield* runMigrations();
23+
24+
const columns = yield* sql<{ readonly name: string; readonly notnull: number }>`
25+
PRAGMA table_info(projection_projects)
26+
`;
27+
const autoPull = columns.find((column) => column.name === "auto_pull");
28+
assert.equal(autoPull?.name, "auto_pull");
29+
assert.equal(autoPull?.notnull, 1);
30+
31+
// Healing is recorded and repeat runs stay green.
32+
const journal = yield* sql<{ readonly migration_id: number; readonly name: string }>`
33+
SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id = 47
34+
`;
35+
assert.deepStrictEqual(journal, [
36+
{ migration_id: 47, name: "EnsureProjectionProjectsAutoPull" },
37+
]);
38+
39+
yield* runMigrations();
40+
const rerun = yield* sql<{ readonly name: string }>`
41+
PRAGMA table_info(projection_projects)
42+
`;
43+
assert.isTrue(rerun.some((column) => column.name === "auto_pull"));
44+
}),
45+
);
46+
47+
it.effect("is a no-op when auto_pull already exists", () =>
48+
Effect.gen(function* () {
49+
const sql = yield* SqlClient.SqlClient;
50+
51+
yield* runMigrations({ toMigrationInclusive: 46 });
52+
yield* runMigrations();
53+
54+
const columns = yield* sql<{ readonly name: string }>`
55+
PRAGMA table_info(projection_projects)
56+
`;
57+
assert.equal(columns.filter((column) => column.name === "auto_pull").length, 1);
58+
}),
59+
);
60+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as Effect from "effect/Effect";
2+
import * as SqlClient from "effect/unstable/sql/SqlClient";
3+
4+
// Repair migration for databases whose 045 was silently skipped.
5+
//
6+
// effect's Migrator only runs migrations with an ID above the highest recorded
7+
// one and never compares names, so any line that occupied ID 45 first (a fork,
8+
// a nightly, a renumbered branch) marks 045_ProjectionProjectsAutoPull as done
9+
// without running it. Every project query then fails with
10+
// "no such column: auto_pull" and the backend crash-loops at startup (#8896).
11+
//
12+
// Idempotent: adds the column only when absent, so it is a no-op on healthy
13+
// databases. Safe to keep forever.
14+
export default Effect.gen(function* () {
15+
const sql = yield* SqlClient.SqlClient;
16+
const columns = yield* sql<{ readonly name: string }>`
17+
PRAGMA table_info(projection_projects)
18+
`;
19+
20+
if (!columns.some((column) => column.name === "auto_pull")) {
21+
yield* sql`
22+
ALTER TABLE projection_projects
23+
ADD COLUMN auto_pull INTEGER NOT NULL DEFAULT 0
24+
`;
25+
}
26+
});

0 commit comments

Comments
 (0)