Skip to content

Commit 020dcc7

Browse files
committed
fix(server): block incompatible migration history in preflight
1 parent ddc4f57 commit 020dcc7

3 files changed

Lines changed: 51 additions & 22 deletions

File tree

‎apps/server/src/cloud/servicePreflight.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ it.layer(NodeServices.layer)("service preflight database", (it) => {
6767
expect(preflight().status).toBe("ready");
6868
expect(databaseMigrationHead(databasePath)).toBe(BUILD_MIGRATION_HEAD);
6969

70+
db.prepare("UPDATE effect_sql_migrations SET name = ? WHERE migration_id = ?").run(
71+
"ForkMigration",
72+
BUILD_MIGRATION_HEAD,
73+
);
74+
const conflict = preflight();
75+
expect(conflict.status).toBe("blocked");
76+
expect(conflict.status === "blocked" ? conflict.reason : "").toContain("ForkMigration");
77+
db.prepare("UPDATE effect_sql_migrations SET name = ? WHERE migration_id = ?").run(
78+
migrationManifest.at(-1)![1],
79+
BUILD_MIGRATION_HEAD,
80+
);
81+
7082
const newerId = Math.max(...migrationManifest.map(([id]) => id)) + 1;
7183
insert.run(newerId, "FromNightly");
7284
db.close();

‎apps/server/src/cloud/servicePreflight.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { skippedV2Migrations } from "../persistence/Migrations.ts";
12
import packageJson from "../../package.json" with { type: "json" };
23
import { BUILD_MIGRATION_HEAD, recordedMigrations } from "../persistence/databaseMigrationHead.ts";
34
import {
@@ -41,7 +42,13 @@ export function runServicePreflight(input: {
4142
};
4243
}
4344

44-
const newer = newerRecordedMigrations(input.databasePath);
45+
const recorded = recordedMigrations(input.databasePath);
46+
const newer =
47+
recorded === null
48+
? null
49+
: recorded
50+
.filter(({ id }) => id > BUILD_MIGRATION_HEAD)
51+
.map(({ id, name }) => `${id}_${name}`);
4552
if (newer === null) {
4653
return {
4754
status: "blocked",
@@ -57,16 +64,16 @@ export function runServicePreflight(input: {
5764
};
5865
}
5966

60-
return { status: "ready", version, launcherProtocol };
61-
}
67+
const skipped = skippedV2Migrations(recorded ?? []);
68+
if (skipped.length > 0) {
69+
return {
70+
status: "blocked",
71+
version,
72+
reason: `This database has incompatible migration history: ${skipped.join(", ")}. Start the build that wrote it, or report this migration history so an upgrade path can be added.`,
73+
};
74+
}
6275

63-
/** Migrations a newer build recorded, or null when the database exists but cannot be read.
64-
* Startup already handles renamed ids this build knows. */
65-
function newerRecordedMigrations(databasePath: string): ReadonlyArray<string> | null {
66-
const recorded = recordedMigrations(databasePath);
67-
return recorded === null
68-
? null
69-
: recorded.filter(({ id }) => id > BUILD_MIGRATION_HEAD).map(({ id, name }) => `${id}_${name}`);
76+
return { status: "ready", version, launcherProtocol };
7077
}
7178

7279
export function decodeServicePreflightResult(

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,24 @@ const makeMigrationLoader = (throughId?: number) =>
185185

186186
const FIRST_V2_MIGRATION_ID = 55;
187187

188+
export function skippedV2Migrations(
189+
recorded: ReadonlyArray<{ readonly id: number; readonly name: string }>,
190+
throughId?: number,
191+
): string[] {
192+
const latestRecordedId = Math.max(0, ...recorded.map((row) => row.id));
193+
const recordedNames = new Map(recorded.map((row) => [row.id, row.name]));
194+
return migrationManifest.flatMap(([id, name]) => {
195+
if (
196+
id < FIRST_V2_MIGRATION_ID ||
197+
id > latestRecordedId ||
198+
(throughId !== undefined && id > throughId)
199+
)
200+
return [];
201+
const recordedName = recordedNames.get(id);
202+
return recordedName === name ? [] : [`${id}_${name} (recorded: ${recordedName ?? "nothing"})`];
203+
});
204+
}
205+
188206
// Migrators skip ids up to the highest recorded; reject collisions that would skip V2 schema.
189207
const assertV2MigrationsCanRun = Effect.fn("assertV2MigrationsCanRun")(function* (
190208
throughId?: number,
@@ -198,19 +216,11 @@ const assertV2MigrationsCanRun = Effect.fn("assertV2MigrationsCanRun")(function*
198216
readonly migration_id: number;
199217
readonly name: string;
200218
}>`SELECT migration_id, name FROM effect_sql_migrations`;
219+
const skipped = skippedV2Migrations(
220+
recorded.map((row) => ({ id: row.migration_id, name: row.name })),
221+
throughId,
222+
);
201223
const latestRecordedId = Math.max(0, ...recorded.map((row) => row.migration_id));
202-
const recordedNames = new Map(recorded.map((row) => [row.migration_id, row.name]));
203-
const skipped = migrationEntries.flatMap(([id, name]) => {
204-
if (
205-
id < FIRST_V2_MIGRATION_ID ||
206-
id > latestRecordedId ||
207-
(throughId !== undefined && id > throughId)
208-
)
209-
return [];
210-
const recordedName = recordedNames.get(id);
211-
if (recordedName === name) return [];
212-
return [`${id}_${name} (recorded: ${recordedName ?? "nothing"})`];
213-
});
214224
if (skipped.length === 0) return;
215225
return yield* new Migrator.MigrationError({
216226
kind: "BadState",

0 commit comments

Comments
 (0)