Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Migrator from "effect/unstable/sql/Migrator";
3import * as SqlClient from "effect/unstable/sql/SqlClient";
4
5import PullRequestFilesViewed from "./Migrations/053_PullRequestFilesViewed.ts";
6
7// Published V2 previews used 53 before main assigned it to PullRequestFilesViewed.
8// Preserve their completed V2 migration without replaying its schema or imports.
9// Keep this bridge for upgrades that skip releases; new migrations must be additive.
10export const reconcileV2PreviewMigration = Effect.fn("reconcileV2PreviewMigration")(function* () {
11 const sql = yield* SqlClient.SqlClient;
12 return yield* sql.withTransaction(
13 Effect.gen(function* () {
14 const tables = yield* sql`
15 SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations'
16 `;
17 if (tables.length === 0) return [];
18 const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
19 SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53
20 `;
21 if (!history.some((row) => row.migration_id === 53 && row.name === "OrchestrationV2")) {
22 return [];
23 }
24 if (history.length !== 1) {
25 return yield* new Migrator.MigrationError({
26 kind: "BadState",
27 message: "Cannot upgrade V2 preview migration 53 with unexpected later migrations.",
28 });
29 }
30
31 yield* PullRequestFilesViewed;
32 yield* sql`
33 UPDATE effect_sql_migrations SET migration_id = 54
34 WHERE migration_id = 53 AND name = 'OrchestrationV2'
35 `;
36 yield* sql`
37 INSERT INTO effect_sql_migrations (migration_id, name) VALUES (53, 'PullRequestFilesViewed')
38 `;
39 return [[53, "PullRequestFilesViewed"]] as const;
40 }),
41 );
42});