Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

reconcileV2PreviewMigration.test.ts

201 lines8.7 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as Effect from "effect/Effect";
4import * as Exit from "effect/Exit";
5import * as Migrator from "effect/sql/Migrator";
6import * as SqlClient from "effect/sql/SqlClient";
7​
8import { migrationManifest, runMigrations } from "./Migrations.ts";
9import PullRequestFilesViewed from "./Migrations/053_PullRequestFilesViewed.ts";
10import RemoveRedundantProjectionIndexes from "./Migrations/056_RemoveRedundantProjectionIndexes.ts";
11import OrchestrationV2 from "./Migrations/055_OrchestrationV2.ts";
12​
13const seedPreview = Effect.gen(function* () {
14 const sql = yield* SqlClient.SqlClient;
15 yield* runMigrations({ toMigrationInclusive: 52 });
16 yield* Migrator.make({})({
17 loader: Migrator.fromRecord({ "53_OrchestrationV2": OrchestrationV2 }),
18 });
19 yield* sql`
20 INSERT INTO orchestration_v2_legacy_imports
21 (thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count)
22 VALUES ('preview-thread', '2026-09-15', '2026-09-15', '2026-09-16', 42)
23 `;
24 yield* sql`
25 UPDATE effect_sql_migrations SET created_at = '2026-09-15 00:00:00' WHERE migration_id = 53
26 `;
27});
28​
29describe("V2 preview upgrade", () => {
30 it.effect("upgrades a published preview without replaying V2 or losing import progress", () =>
31 Effect.gen(function* () {
32 const sql = yield* SqlClient.SqlClient;
33 yield* seedPreview;
34 const imports = yield* sql`SELECT * FROM orchestration_v2_legacy_imports`;
35 assert.deepStrictEqual(yield* runMigrations(), [
36 [53, "PullRequestFilesViewed"],
37 [54, "ProjectionThreadsAutoSettleDisabledAt"],
38 [56, "RemoveRedundantProjectionIndexes"],
39 [57, "ScheduledTaskWebhooks"],
40 [58, "WebhookRelayDeliveries"],
41 [59, "McpAppModelContext"],
42 [60, "ThreadSnapshotWindowIndexes"],
43 [61, "PullRequestReviewGuides"],
44 [62, "ThreadFailureLookupIndex"],
45 [63, "DropUnusedV1Projections"],
46 [64, "CanonicalizeThreadPullRequestLinks"],
47 [65, "CodexUsageNativeTurnIndex"],
48 [66, "RepairClientSettlementTimestamps"],
49 [67, "WebhookDedupe"],
50 [68, "ShellStopRequestIndex"],
51 [69, "ThreadMessageSearch"],
52 [70, "PeerLinks"],
53 [71, "PeerLinkFence"],
54 [72, "WaitingLinkRequestIndex"],
55 [73, "ActiveTurnItemIndexes"],
56 [74, "ExcludeAttachmentPlaceholdersFromSearch"],
57 [75, "SubagentDeliveryState"],
58 [76, "ProjectionProjectsPinArchive"],
59 [77, "RepairBooleanSettledOverride"],
60 [78, "DropOrchestrationEventCorrelationIndex"],
61 [79, "DelegatedRecoveryIndexes"],
62 [80, "ScheduledTaskThreadMode"],
63 [81, "ProjectionMessageSearchText"],
64 [82, "TurnItemConversationIndex"],
65 [83, "CommandReadIndexes"],
66 [84, "LastExecutedRunIndex"],
67 [85, "TurnItemCounts"],
68 [86, "QueuedTurnInputIndex"],
69 [87, "ProjectionProjectsCustomTitle"],
70 [88, "TurnItemTypeStatusIndex"],
71 [89, "ShellThreadReadIndexes"],
72 ]);
73 assert.deepStrictEqual(yield* runMigrations(), []);
74 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
75 const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
76 SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
77 `;
78 assert.deepStrictEqual(
79 history.map((row) => [row.migration_id, row.name] as const),
80 migrationManifest,
81 );
82 assert.deepStrictEqual(
83 yield* sql`SELECT created_at FROM effect_sql_migrations WHERE migration_id = 55`,
84 [{ created_at: "2026-09-15 00:00:00" }],
85 );
86 yield* sql`
87 INSERT INTO pull_request_files_viewed
88 (provider, host, repository, number, viewer, path, revision, viewed_at)
89 VALUES ('github', 'github.com', 'owner/repo', 1, 'viewer', 'file.ts', 'revision', '2026-09-17')
90 `;
91 assert.strictEqual((yield* sql`SELECT * FROM pull_request_files_viewed`).length, 1);
92 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
93 );
94​
95 it.effect.each([false, true])(
96 "upgrades preview migration 54 with index cleanup %s",
97 (withIndexes) =>
98 Effect.gen(function* () {
99 const sql = yield* SqlClient.SqlClient;
100 yield* runMigrations({ toMigrationInclusive: 52 });
101 yield* Migrator.make({})({
102 loader: Migrator.fromRecord({
103 "53_PullRequestFilesViewed": PullRequestFilesViewed,
104 "54_OrchestrationV2": OrchestrationV2,
105 ...(withIndexes
106 ? { "55_RemoveRedundantProjectionIndexes": RemoveRedundantProjectionIndexes }
107 : {}),
108 }),
109 });
110 yield* runMigrations();
111 assert.deepStrictEqual(yield* runMigrations(), []);
112 const history = yield* sql<{
113 readonly migration_id: number;
114 readonly name: string;
115 }>`SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id`;
116 assert.deepStrictEqual(
117 history.map((row) => [row.migration_id, row.name] as const),
118 migrationManifest,
119 );
120 const columns = yield* sql<{
121 readonly name: string;
122 }>`PRAGMA table_info(projection_threads)`;
123 assert.ok(columns.some((column) => column.name === "auto_settle_disabled_at"));
124 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
125 );
126​
127 it.effect("rolls back schema and ledger together on failure and can retry", () =>
128 Effect.gen(function* () {
129 const sql = yield* SqlClient.SqlClient;
130 yield* seedPreview;
131 yield* sql`
132 CREATE TRIGGER fail_preview_upgrade BEFORE INSERT ON effect_sql_migrations
133 WHEN NEW.name = 'PullRequestFilesViewed'
134 BEGIN SELECT RAISE(ABORT, 'injected failure'); END
135 `;
136 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
137 assert.deepStrictEqual(
138 yield* sql`SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53`,
139 [{ migration_id: 53, name: "OrchestrationV2" }],
140 );
141 assert.deepStrictEqual(
142 yield* sql`SELECT name FROM sqlite_master WHERE name = 'pull_request_files_viewed'`,
143 [],
144 );
145 assert.strictEqual((yield* sql`SELECT * FROM orchestration_v2_legacy_imports`).length, 1);
146 yield* sql`DROP TRIGGER fail_preview_upgrade`;
147 assert.deepStrictEqual(yield* runMigrations(), [
148 [53, "PullRequestFilesViewed"],
149 [54, "ProjectionThreadsAutoSettleDisabledAt"],
150 [56, "RemoveRedundantProjectionIndexes"],
151 [57, "ScheduledTaskWebhooks"],
152 [58, "WebhookRelayDeliveries"],
153 [59, "McpAppModelContext"],
154 [60, "ThreadSnapshotWindowIndexes"],
155 [61, "PullRequestReviewGuides"],
156 [62, "ThreadFailureLookupIndex"],
157 [63, "DropUnusedV1Projections"],
158 [64, "CanonicalizeThreadPullRequestLinks"],
159 [65, "CodexUsageNativeTurnIndex"],
160 [66, "RepairClientSettlementTimestamps"],
161 [67, "WebhookDedupe"],
162 [68, "ShellStopRequestIndex"],
163 [69, "ThreadMessageSearch"],
164 [70, "PeerLinks"],
165 [71, "PeerLinkFence"],
166 [72, "WaitingLinkRequestIndex"],
167 [73, "ActiveTurnItemIndexes"],
168 [74, "ExcludeAttachmentPlaceholdersFromSearch"],
169 [75, "SubagentDeliveryState"],
170 [76, "ProjectionProjectsPinArchive"],
171 [77, "RepairBooleanSettledOverride"],
172 [78, "DropOrchestrationEventCorrelationIndex"],
173 [79, "DelegatedRecoveryIndexes"],
174 [80, "ScheduledTaskThreadMode"],
175 [81, "ProjectionMessageSearchText"],
176 [82, "TurnItemConversationIndex"],
177 [83, "CommandReadIndexes"],
178 [84, "LastExecutedRunIndex"],
179 [85, "TurnItemCounts"],
180 [86, "QueuedTurnInputIndex"],
181 [87, "ProjectionProjectsCustomTitle"],
182 [88, "TurnItemTypeStatusIndex"],
183 [89, "ShellThreadReadIndexes"],
184 ]);
185 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
186 );
187​
188 it.effect("refuses unexpected later migrations without modifying their history", () =>
189 Effect.gen(function* () {
190 const sql = yield* SqlClient.SqlClient;
191 yield* seedPreview;
192 yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES (54, 'UnknownFork')`;
193 const history = yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`;
194 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
195 assert.deepStrictEqual(
196 yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`,
197 history,
198 );
199 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
200 );
201});