TypeScript
fix(server): dev seeding accepts migration histories the server boots (#24665)
Victor Vogeleb129c0600ca
diff --git a/apps/server/scripts/migrate-dev-db.test.ts b/apps/server/scripts/migrate-dev-db.test.tsindex 2b56111a98b..9dd418a9486 100644--- a/apps/server/scripts/migrate-dev-db.test.ts+++ b/apps/server/scripts/migrate-dev-db.test.ts@@ -253,6 +253,73 @@ it.layer(NodeServices.layer)("migrate-dev-db", (it) => {}),);+ it.effect("seeds from a source that recorded another migration below V2", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-local-" });+ const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-local-dest-" });+ const source = yield* createFixtureSource(sourceDir);+ yield* withDatabase(+ source,+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ yield* sql`UPDATE effect_sql_migrations SET name = 'SiteLocalMigration'+ WHERE migration_id = 51`;+ }),+ );++ const result = yield* runMigrateDevDb(+ { baseDir: destDir, source, projects: 5, threadsPerProject: 2 },+ { sharedHome: sourceDir },+ );+ const threads = yield* withDatabase(+ result.databasePath,+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ return yield* sql<{ thread_id: string }>`+ SELECT thread_id FROM orchestration_v2_projection_threads ORDER BY thread_id`;+ }),+ );+ assert.deepStrictEqual(+ threads.map((row) => row.thread_id),+ ["fork-thread", "settled-thread", "stopped-thread"],+ );+ }),+ );++ it.effect("refuses a source whose V2 migration history clashes with this checkout", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-clash-" });+ const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-clash-dest-" });+ const source = yield* createFixtureSource(sourceDir);+ yield* withDatabase(+ source,+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ yield* sql`UPDATE effect_sql_migrations SET name = 'OtherBuildMigration'+ WHERE migration_id = 60`;+ }),+ );++ const destination = yield* createFixtureSource(destDir);+ const sourceBefore = yield* fs.readFile(source);+ const destinationBefore = yield* fs.readFile(destination);++ const error = yield* runMigrateDevDb(+ { baseDir: destDir, source, projects: 5, threadsPerProject: 2 },+ { sharedHome: sourceDir },+ ).pipe(Effect.flip);+ assert.equal(error._tag, "MigrateDevDbPhaseError");+ if (error._tag === "MigrateDevDbPhaseError") {+ assert.equal(error.phase, "migrate");+ assert.include(String(error.cause), "60_ThreadSnapshotWindowIndexes");+ }+ assert.deepStrictEqual(yield* fs.readFile(source), sourceBefore);+ assert.deepStrictEqual(yield* fs.readFile(destination), destinationBefore);+ }),+ );+it.effect("refuses while a dev server holds the destination", () =>Effect.gen(function* () {const fs = yield* FileSystem.FileSystem;diff --git a/apps/server/scripts/migrate-dev-db.ts b/apps/server/scripts/migrate-dev-db.tsindex 5d1446a8c07..fd763f60a1e 100644--- a/apps/server/scripts/migrate-dev-db.ts+++ b/apps/server/scripts/migrate-dev-db.ts@@ -17,7 +17,7 @@ import { Command, Flag } from "effect/cli";import * as ProjectionStore from "../src/orchestration-v2/ProjectionStore.ts";import { rebuildMessageSearch } from "../src/orchestration-v2/messageSearchIndex.ts";-import { migrationManifest, runMigrations } from "../src/persistence/Migrations.ts";+import { runMigrations } from "../src/persistence/Migrations.ts";import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";export class MigrateDevDbNotInWorktreeError extends Schema.TaggedError<MigrateDevDbNotInWorktreeError>()(@@ -89,23 +89,10 @@ export class MigrateDevDbDestinationBusyError extends Schema.TaggedError<Migrate}}-export class MigrateDevDbSlotCollisionError extends Schema.TaggedError<MigrateDevDbSlotCollisionError>()(- "MigrateDevDbSlotCollisionError",- {- slot: Schema.Number,- codeName: Schema.String,- appliedName: Schema.String,- },-) {- override get message(): string {- 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.`;- }-}-export class MigrateDevDbPhaseError extends Schema.TaggedError<MigrateDevDbPhaseError>()("MigrateDevDbPhaseError",{- phase: Schema.Literals(["snapshot", "prune", "compact", "migrate", "verify"]),+ phase: Schema.Literals(["snapshot", "prune", "compact", "migrate"]),databasePath: Schema.String,cause: Schema.Defect(),},@@ -416,19 +403,6 @@ const pruneSnapshot = Effect.fn("pruneDevDbSnapshot")(function* (input: RunMigra};});-const verifyMigrationSlots = Effect.fn("verifyMigrationSlots")(function* () {- const sql = yield* SqlClient.SqlClient;- const applied = yield* sql<{ migration_id: number; name: string }>`- SELECT migration_id, name FROM effect_sql_migrations`;- const appliedById = new Map(applied.map((row) => [Number(row.migration_id), row.name]));- for (const [slot, codeName] of migrationManifest) {- const appliedName = appliedById.get(slot);- if (appliedName !== undefined && appliedName !== codeName) {- return yield* new MigrateDevDbSlotCollisionError({ slot, codeName, appliedName });- }- }-});-export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (input: RunMigrateDevDbInput,options: RunMigrateDevDbOptions = {},@@ -510,17 +484,6 @@ export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (wrapPhase("migrate", snapshotPath),);- // Detect migration slot collisions before replacing the worktree database.- yield* verifyMigrationSlots().pipe(- Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),- Effect.catchTags({- SqlError: (cause) =>- Effect.fail(- new MigrateDevDbPhaseError({ phase: "verify", databasePath: snapshotPath, cause }),- ),- }),- );-yield* Console.log(`Pruning to ${input.projects} projects, ${input.threadsPerProject} stopped threads each...`,);