@@ -5,7 +5,43 @@ import * as SqlClient from "effect/sql/SqlClient";
55import PullRequestFilesViewed from "./Migrations/053_PullRequestFilesViewed.ts" ;
66import AutoSettleDisabledAt from "./Migrations/054_ProjectionThreadsAutoSettleDisabledAt.ts" ;
77
8+ type MigrationRow = { readonly id : number ; readonly name : string } ;
9+
810// Previews used V2 ids 53/54; preserve their schema and import progress while reserving main's ids.
11+ function v2PreviewId ( history : ReadonlyArray < MigrationRow > ) : number | "invalid" | undefined {
12+ const later = history . filter ( ( row ) => row . id >= 53 ) ;
13+ const legacy = later . find (
14+ ( row ) => row . name === "OrchestrationV2" && ( row . id === 53 || row . id === 54 ) ,
15+ ) ;
16+ if ( ! legacy ) return undefined ;
17+ const valid = later . every (
18+ ( row ) =>
19+ row === legacy ||
20+ ( legacy . id === 54 &&
21+ ( ( row . id === 53 && row . name === "PullRequestFilesViewed" ) ||
22+ ( row . id === 55 && row . name === "RemoveRedundantProjectionIndexes" ) ) ) ,
23+ ) ;
24+ return valid ? legacy . id : "invalid" ;
25+ }
26+
27+ /** The history as startup's preview reconciliation leaves it, or null when startup rejects it. */
28+ export function reconciledV2PreviewHistory (
29+ history : ReadonlyArray < MigrationRow > ,
30+ ) : ReadonlyArray < MigrationRow > | null {
31+ const previewId = v2PreviewId ( history ) ;
32+ if ( previewId === undefined ) return history ;
33+ if ( previewId === "invalid" ) return null ;
34+ return [
35+ ...history . filter ( ( row ) => row . id < 53 ) ,
36+ { id : 53 , name : "PullRequestFilesViewed" } ,
37+ { id : 54 , name : "ProjectionThreadsAutoSettleDisabledAt" } ,
38+ { id : 55 , name : "OrchestrationV2" } ,
39+ ...( history . some ( ( row ) => row . id === 55 )
40+ ? [ { id : 56 , name : "RemoveRedundantProjectionIndexes" } ]
41+ : [ ] ) ,
42+ ] ;
43+ }
44+
945export const reconcileV2PreviewMigration = Effect . fn ( "reconcileV2PreviewMigration" ) ( function * ( ) {
1046 const sql = yield * SqlClient . SqlClient ;
1147 return yield * sql . withTransaction (
@@ -17,35 +53,27 @@ export const reconcileV2PreviewMigration = Effect.fn("reconcileV2PreviewMigratio
1753 const history = yield * sql < { readonly migration_id : number ; readonly name : string } > `
1854 SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53
1955 ` ;
20- const legacy = history . find (
21- ( row ) =>
22- row . name === "OrchestrationV2" && ( row . migration_id === 53 || row . migration_id === 54 ) ,
23- ) ;
24- if ( ! legacy ) return [ ] ;
25- const valid = history . every (
26- ( row ) =>
27- row === legacy ||
28- ( legacy . migration_id === 54 &&
29- ( ( row . migration_id === 53 && row . name === "PullRequestFilesViewed" ) ||
30- ( row . migration_id === 55 && row . name === "RemoveRedundantProjectionIndexes" ) ) ) ,
56+ const previewId = v2PreviewId (
57+ history . map ( ( row ) => ( { id : row . migration_id , name : row . name } ) ) ,
3158 ) ;
32- if ( ! valid ) {
59+ if ( previewId === undefined ) return [ ] ;
60+ if ( previewId === "invalid" ) {
3361 return yield * new Migrator . MigrationError ( {
3462 kind : "BadState" ,
3563 message : "Cannot upgrade V2 preview with unexpected later migrations." ,
3664 } ) ;
3765 }
3866 const executed : Array < readonly [ number , string ] > = [ ] ;
39- if ( legacy . migration_id === 53 ) {
67+ if ( previewId === 53 ) {
4068 yield * PullRequestFilesViewed ;
4169 executed . push ( [ 53 , "PullRequestFilesViewed" ] ) ;
4270 }
4371 yield * AutoSettleDisabledAt ;
4472 executed . push ( [ 54 , "ProjectionThreadsAutoSettleDisabledAt" ] ) ;
4573 // Move the later entry first to avoid a primary-key collision.
4674 yield * sql `UPDATE effect_sql_migrations SET migration_id = 56 WHERE migration_id = 55 AND name = 'RemoveRedundantProjectionIndexes'` ;
47- yield * sql `UPDATE effect_sql_migrations SET migration_id = 55 WHERE migration_id = ${ legacy . migration_id } AND name = 'OrchestrationV2'` ;
48- if ( legacy . migration_id === 53 ) {
75+ yield * sql `UPDATE effect_sql_migrations SET migration_id = 55 WHERE migration_id = ${ previewId } AND name = 'OrchestrationV2'` ;
76+ if ( previewId === 53 ) {
4977 yield * sql `INSERT INTO effect_sql_migrations (migration_id, name) VALUES (53, 'PullRequestFilesViewed')` ;
5078 }
5179 yield * sql `INSERT INTO effect_sql_migrations (migration_id, name) VALUES (54, 'ProjectionThreadsAutoSettleDisabledAt')` ;
0 commit comments