Skip to content

Commit eb129c0

Browse files
only21milclaudemaria-rcks
authored
fix(server): dev seeding accepts migration histories the server boots (#24665)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 3e0d877 commit eb129c0

2 files changed

Lines changed: 69 additions & 39 deletions

File tree

‎apps/server/scripts/migrate-dev-db.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,73 @@ it.layer(NodeServices.layer)("migrate-dev-db", (it) => {
253253
}),
254254
);
255255

256+
it.effect("seeds from a source that recorded another migration below V2", () =>
257+
Effect.gen(function* () {
258+
const fs = yield* FileSystem.FileSystem;
259+
const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-local-" });
260+
const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-local-dest-" });
261+
const source = yield* createFixtureSource(sourceDir);
262+
yield* withDatabase(
263+
source,
264+
Effect.gen(function* () {
265+
const sql = yield* SqlClient.SqlClient;
266+
yield* sql`UPDATE effect_sql_migrations SET name = 'SiteLocalMigration'
267+
WHERE migration_id = 51`;
268+
}),
269+
);
270+
271+
const result = yield* runMigrateDevDb(
272+
{ baseDir: destDir, source, projects: 5, threadsPerProject: 2 },
273+
{ sharedHome: sourceDir },
274+
);
275+
const threads = yield* withDatabase(
276+
result.databasePath,
277+
Effect.gen(function* () {
278+
const sql = yield* SqlClient.SqlClient;
279+
return yield* sql<{ thread_id: string }>`
280+
SELECT thread_id FROM orchestration_v2_projection_threads ORDER BY thread_id`;
281+
}),
282+
);
283+
assert.deepStrictEqual(
284+
threads.map((row) => row.thread_id),
285+
["fork-thread", "settled-thread", "stopped-thread"],
286+
);
287+
}),
288+
);
289+
290+
it.effect("refuses a source whose V2 migration history clashes with this checkout", () =>
291+
Effect.gen(function* () {
292+
const fs = yield* FileSystem.FileSystem;
293+
const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-clash-" });
294+
const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-clash-dest-" });
295+
const source = yield* createFixtureSource(sourceDir);
296+
yield* withDatabase(
297+
source,
298+
Effect.gen(function* () {
299+
const sql = yield* SqlClient.SqlClient;
300+
yield* sql`UPDATE effect_sql_migrations SET name = 'OtherBuildMigration'
301+
WHERE migration_id = 60`;
302+
}),
303+
);
304+
305+
const destination = yield* createFixtureSource(destDir);
306+
const sourceBefore = yield* fs.readFile(source);
307+
const destinationBefore = yield* fs.readFile(destination);
308+
309+
const error = yield* runMigrateDevDb(
310+
{ baseDir: destDir, source, projects: 5, threadsPerProject: 2 },
311+
{ sharedHome: sourceDir },
312+
).pipe(Effect.flip);
313+
assert.equal(error._tag, "MigrateDevDbPhaseError");
314+
if (error._tag === "MigrateDevDbPhaseError") {
315+
assert.equal(error.phase, "migrate");
316+
assert.include(String(error.cause), "60_ThreadSnapshotWindowIndexes");
317+
}
318+
assert.deepStrictEqual(yield* fs.readFile(source), sourceBefore);
319+
assert.deepStrictEqual(yield* fs.readFile(destination), destinationBefore);
320+
}),
321+
);
322+
256323
it.effect("refuses while a dev server holds the destination", () =>
257324
Effect.gen(function* () {
258325
const fs = yield* FileSystem.FileSystem;

‎apps/server/scripts/migrate-dev-db.ts‎

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Command, Flag } from "effect/cli";
1717

1818
import * as ProjectionStore from "../src/orchestration-v2/ProjectionStore.ts";
1919
import { rebuildMessageSearch } from "../src/orchestration-v2/messageSearchIndex.ts";
20-
import { migrationManifest, runMigrations } from "../src/persistence/Migrations.ts";
20+
import { runMigrations } from "../src/persistence/Migrations.ts";
2121
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
2222

2323
export class MigrateDevDbNotInWorktreeError extends Schema.TaggedError<MigrateDevDbNotInWorktreeError>()(
@@ -89,23 +89,10 @@ export class MigrateDevDbDestinationBusyError extends Schema.TaggedError<Migrate
8989
}
9090
}
9191

92-
export class MigrateDevDbSlotCollisionError extends Schema.TaggedError<MigrateDevDbSlotCollisionError>()(
93-
"MigrateDevDbSlotCollisionError",
94-
{
95-
slot: Schema.Number,
96-
codeName: Schema.String,
97-
appliedName: Schema.String,
98-
},
99-
) {
100-
override get message(): string {
101-
return `Migration slot collision at ${this.slot}: this checkout registers '${this.codeName}' but the database already applied '${this.appliedName}' in that slot. Renumber the new migration to a free slot.`;
102-
}
103-
}
104-
10592
export class MigrateDevDbPhaseError extends Schema.TaggedError<MigrateDevDbPhaseError>()(
10693
"MigrateDevDbPhaseError",
10794
{
108-
phase: Schema.Literals(["snapshot", "prune", "compact", "migrate", "verify"]),
95+
phase: Schema.Literals(["snapshot", "prune", "compact", "migrate"]),
10996
databasePath: Schema.String,
11097
cause: Schema.Defect(),
11198
},
@@ -416,19 +403,6 @@ const pruneSnapshot = Effect.fn("pruneDevDbSnapshot")(function* (input: RunMigra
416403
};
417404
});
418405

419-
const verifyMigrationSlots = Effect.fn("verifyMigrationSlots")(function* () {
420-
const sql = yield* SqlClient.SqlClient;
421-
const applied = yield* sql<{ migration_id: number; name: string }>`
422-
SELECT migration_id, name FROM effect_sql_migrations`;
423-
const appliedById = new Map(applied.map((row) => [Number(row.migration_id), row.name]));
424-
for (const [slot, codeName] of migrationManifest) {
425-
const appliedName = appliedById.get(slot);
426-
if (appliedName !== undefined && appliedName !== codeName) {
427-
return yield* new MigrateDevDbSlotCollisionError({ slot, codeName, appliedName });
428-
}
429-
}
430-
});
431-
432406
export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (
433407
input: RunMigrateDevDbInput,
434408
options: RunMigrateDevDbOptions = {},
@@ -510,17 +484,6 @@ export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (
510484
wrapPhase("migrate", snapshotPath),
511485
);
512486

513-
// Detect migration slot collisions before replacing the worktree database.
514-
yield* verifyMigrationSlots().pipe(
515-
Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
516-
Effect.catchTags({
517-
SqlError: (cause) =>
518-
Effect.fail(
519-
new MigrateDevDbPhaseError({ phase: "verify", databasePath: snapshotPath, cause }),
520-
),
521-
}),
522-
);
523-
524487
yield* Console.log(
525488
`Pruning to ${input.projects} projects, ${input.threadsPerProject} stopped threads each...`,
526489
);

0 commit comments

Comments
 (0)