diff --git a/apps/server/scripts/migrate-dev-db.test.ts b/apps/server/scripts/migrate-dev-db.test.ts index b944bfcb42ef1..6898eff9ccbf0 100644 --- a/apps/server/scripts/migrate-dev-db.test.ts +++ b/apps/server/scripts/migrate-dev-db.test.ts @@ -14,8 +14,14 @@ const withDatabase = ( effect: Effect.Effect, ) => effect.pipe(Effect.provide(NodeSqliteClient.layer({ filename: databasePath }))); +const insertPeerLink = (sql: SqlClient.SqlClient) => sql`INSERT INTO peer_environment_links + (environment_id, secret_name, label, urls_json, access, linked_at, expires_at) + VALUES ('environment-box', 'peer-link-box', 'Box', '["https://box.example"]', 'auto', + '2026-08-01', '2027-08-01')`; + const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(function* ( baseDir: string, + toMigrationInclusive?: number, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -26,7 +32,7 @@ const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(functio databasePath, Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations(); + yield* runMigrations({ toMigrationInclusive }); yield* sql`INSERT INTO projection_projects (project_id, title, workspace_root, scripts_json, created_at, updated_at, deleted_at) @@ -120,10 +126,7 @@ const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(functio VALUES ('delivery-1', 'task-1', 'key-1', 1900000000000, '{}', '{}', '[]')`; yield* sql`INSERT INTO auth_sessions (session_id, subject, scopes, method, issued_at, expires_at) VALUES ('session-1', 'user', '[]', 'pairing', '2026-08-01', '2027-08-01')`; - yield* sql`INSERT INTO peer_environment_links - (environment_id, secret_name, label, urls_json, access, linked_at, expires_at) - VALUES ('environment-box', 'peer-link-box', 'Box', '["https://box.example"]', 'auto', - '2026-08-01', '2027-08-01')`; + if (toMigrationInclusive === undefined) yield* insertPeerLink(sql); }), ); return databasePath; @@ -355,6 +358,70 @@ it.layer(NodeServices.layer)("migrate-dev-db", (it) => { }), ); + it.effect("imports a dev seed whose ids after 67 came from the peer links branch", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-peer-" }); + const destDir = yield* fs.makeTempDirectoryScoped({ prefix: "migrate-dev-db-peer-dest-" }); + const source = yield* createFixtureSource(sourceDir, 67); + // Mirrors a real ~/.t3 seed written by a build whose 68-70 differ from main's. + yield* withDatabase( + source, + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql`CREATE TABLE peer_environment_links ( + environment_id TEXT PRIMARY KEY, + secret_name TEXT NOT NULL, + label TEXT NOT NULL, + urls_json TEXT NOT NULL, + access TEXT NOT NULL, + linked_at TEXT NOT NULL, + expires_at TEXT NOT NULL, + last_reached_at TEXT, + last_error TEXT, + fenced INTEGER NOT NULL DEFAULT 0)`; + yield* sql`CREATE INDEX orchestration_v2_projection_turn_items_waiting_link_idx + ON orchestration_v2_projection_turn_items(thread_id, updated_at DESC, turn_item_id DESC) + WHERE type = 'link_request' AND status = 'waiting'`; + yield* insertPeerLink(sql); + yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES + (68, 'PeerLinks'), (69, 'PeerLinkFence'), (70, 'WaitingLinkRequestIndex')`; + }), + ); + + const result = yield* runMigrateDevDb( + { baseDir: destDir, source, projects: 5, threadsPerProject: 2 }, + { sharedHome: sourceDir }, + ); + + const imported = yield* withDatabase( + result.databasePath, + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const threads = yield* sql<{ thread_id: string }>` + SELECT thread_id FROM orchestration_v2_projection_threads ORDER BY thread_id`; + const schema = yield* sql<{ name: string }>` + SELECT name FROM sqlite_master WHERE name IN ( + 'orchestration_v2_projection_turn_items_stop_request_idx', + 'orchestration_v2_message_search') + ORDER BY name`; + return { threads, schema }; + }), + ); + assert.deepStrictEqual( + imported.threads.map((row) => row.thread_id), + ["fork-thread", "settled-thread", "stopped-thread"], + ); + assert.deepStrictEqual( + imported.schema.map((row) => row.name), + [ + "orchestration_v2_message_search", + "orchestration_v2_projection_turn_items_stop_request_idx", + ], + ); + }), + ); + 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.ts index f05120ddaf181..afec0c9458b81 100644 --- a/apps/server/scripts/migrate-dev-db.ts +++ b/apps/server/scripts/migrate-dev-db.ts @@ -410,6 +410,33 @@ const pruneSnapshot = Effect.fn("pruneDevDbSnapshot")(function* (input: RunMigra }; }); +// Peer links preview builds recorded 68-70 before main took those slots for other migrations. +// A snapshot with exactly that history drops the preview's schema so main's 68+ can run. +const PEER_LINKS_PREVIEW_HISTORY = [ + "68:PeerLinks", + "69:PeerLinkFence", + "70:WaitingLinkRequestIndex", +]; + +const forgetPeerLinksPreviewHistory = Effect.fn("forgetPeerLinksPreviewHistory")(function* () { + const sql = yield* SqlClient.SqlClient; + const tables = yield* sql` + SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations'`; + if (tables.length === 0) return; + const tail = yield* sql<{ migration_id: number; name: string }>` + SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 68 + ORDER BY migration_id`; + const history = tail.map((row) => `${row.migration_id}:${row.name}`); + if (history.join() !== PEER_LINKS_PREVIEW_HISTORY.join()) return; + yield* sql.withTransaction( + Effect.gen(function* () { + yield* sql`DROP INDEX IF EXISTS orchestration_v2_projection_turn_items_waiting_link_idx`; + yield* sql`DROP TABLE IF EXISTS peer_environment_links`; + yield* sql`DELETE FROM effect_sql_migrations WHERE migration_id >= 68`; + }), + ); +}); + export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* ( input: RunMigrateDevDbInput, options: RunMigrateDevDbOptions = {}, @@ -485,6 +512,7 @@ export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* ( const executed = yield* Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; yield* sql.unsafe("PRAGMA foreign_keys = ON").unprepared; + yield* forgetPeerLinksPreviewHistory(); return yield* runMigrations(); }).pipe( Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),