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

reconcileV2PreviewMigration.test.ts

251 lines10.6 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 [90, "AuthConnectDevices"],
73 [91, "AuthConnectDeniedRequests"],
74 [92, "QueuedTurnInputIndexJsonValid"],
75 [93, "ProviderTurnAttemptIndex"],
76 [94, "TurnItemMessageOrdinalIndex"],
77 [95, "TurnItemInlineWorkOutput"],
78 [96, "RepairStaleTitleRegeneration"],
79 [97, "IssueTrackerBindings"],
80 [98, "ActiveShellIndexIndexes"],
81 [99, "BackgroundRecoveryIndexes"],
82 [100, "TurnStartReadIndexes"],
83 [101, "DelegatedTaskReadIndexes"],
84 [102, "ThreadItemCounts"],
85 [103, "IssueTrackerAdditionalScopes"],
86 [104, "ProjectionProjectsTrackedFolders"],
87 [105, "TaskFolders"],
88 [106, "WorktreePathKeyIndex"],
89 [107, "DeferredSlashCommandIndex"],
90 [108, "AgentFeedback"],
91 [109, "IssueTrackerAccounts"],
92 [110, "Workflows"],
93 [111, "SupersededRunAttemptIndex"],
94 [112, "LayoutCommandApprovals"],
95 [113, "WorkflowSkippedRuns"],
96 [114, "WorkflowRunIndexes"],
97 ]);
98 assert.deepStrictEqual(yield* runMigrations(), []);
99 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
100 const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
101 SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
102 `;
103 assert.deepStrictEqual(
104 history.map((row) => [row.migration_id, row.name] as const),
105 migrationManifest,
106 );
107 assert.deepStrictEqual(
108 yield* sql`SELECT created_at FROM effect_sql_migrations WHERE migration_id = 55`,
109 [{ created_at: "2026-09-15 00:00:00" }],
110 );
111 yield* sql`
112 INSERT INTO pull_request_files_viewed
113 (provider, host, repository, number, viewer, path, revision, viewed_at)
114 VALUES ('github', 'github.com', 'owner/repo', 1, 'viewer', 'file.ts', 'revision', '2026-09-17')
115 `;
116 assert.strictEqual((yield* sql`SELECT * FROM pull_request_files_viewed`).length, 1);
117 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
118 );
119​
120 it.effect.each([false, true])(
121 "upgrades preview migration 54 with index cleanup %s",
122 (withIndexes) =>
123 Effect.gen(function* () {
124 const sql = yield* SqlClient.SqlClient;
125 yield* runMigrations({ toMigrationInclusive: 52 });
126 yield* Migrator.make({})({
127 loader: Migrator.fromRecord({
128 "53_PullRequestFilesViewed": PullRequestFilesViewed,
129 "54_OrchestrationV2": OrchestrationV2,
130 ...(withIndexes
131 ? { "55_RemoveRedundantProjectionIndexes": RemoveRedundantProjectionIndexes }
132 : {}),
133 }),
134 });
135 yield* runMigrations();
136 assert.deepStrictEqual(yield* runMigrations(), []);
137 const history = yield* sql<{
138 readonly migration_id: number;
139 readonly name: string;
140 }>`SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id`;
141 assert.deepStrictEqual(
142 history.map((row) => [row.migration_id, row.name] as const),
143 migrationManifest,
144 );
145 const columns = yield* sql<{
146 readonly name: string;
147 }>`PRAGMA table_info(projection_threads)`;
148 assert.ok(columns.some((column) => column.name === "auto_settle_disabled_at"));
149 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
150 );
151​
152 it.effect("rolls back schema and ledger together on failure and can retry", () =>
153 Effect.gen(function* () {
154 const sql = yield* SqlClient.SqlClient;
155 yield* seedPreview;
156 yield* sql`
157 CREATE TRIGGER fail_preview_upgrade BEFORE INSERT ON effect_sql_migrations
158 WHEN NEW.name = 'PullRequestFilesViewed'
159 BEGIN SELECT RAISE(ABORT, 'injected failure'); END
160 `;
161 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
162 assert.deepStrictEqual(
163 yield* sql`SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53`,
164 [{ migration_id: 53, name: "OrchestrationV2" }],
165 );
166 assert.deepStrictEqual(
167 yield* sql`SELECT name FROM sqlite_master WHERE name = 'pull_request_files_viewed'`,
168 [],
169 );
170 assert.strictEqual((yield* sql`SELECT * FROM orchestration_v2_legacy_imports`).length, 1);
171 yield* sql`DROP TRIGGER fail_preview_upgrade`;
172 assert.deepStrictEqual(yield* runMigrations(), [
173 [53, "PullRequestFilesViewed"],
174 [54, "ProjectionThreadsAutoSettleDisabledAt"],
175 [56, "RemoveRedundantProjectionIndexes"],
176 [57, "ScheduledTaskWebhooks"],
177 [58, "WebhookRelayDeliveries"],
178 [59, "McpAppModelContext"],
179 [60, "ThreadSnapshotWindowIndexes"],
180 [61, "PullRequestReviewGuides"],
181 [62, "ThreadFailureLookupIndex"],
182 [63, "DropUnusedV1Projections"],
183 [64, "CanonicalizeThreadPullRequestLinks"],
184 [65, "CodexUsageNativeTurnIndex"],
185 [66, "RepairClientSettlementTimestamps"],
186 [67, "WebhookDedupe"],
187 [68, "ShellStopRequestIndex"],
188 [69, "ThreadMessageSearch"],
189 [70, "PeerLinks"],
190 [71, "PeerLinkFence"],
191 [72, "WaitingLinkRequestIndex"],
192 [73, "ActiveTurnItemIndexes"],
193 [74, "ExcludeAttachmentPlaceholdersFromSearch"],
194 [75, "SubagentDeliveryState"],
195 [76, "ProjectionProjectsPinArchive"],
196 [77, "RepairBooleanSettledOverride"],
197 [78, "DropOrchestrationEventCorrelationIndex"],
198 [79, "DelegatedRecoveryIndexes"],
199 [80, "ScheduledTaskThreadMode"],
200 [81, "ProjectionMessageSearchText"],
201 [82, "TurnItemConversationIndex"],
202 [83, "CommandReadIndexes"],
203 [84, "LastExecutedRunIndex"],
204 [85, "TurnItemCounts"],
205 [86, "QueuedTurnInputIndex"],
206 [87, "ProjectionProjectsCustomTitle"],
207 [88, "TurnItemTypeStatusIndex"],
208 [89, "ShellThreadReadIndexes"],
209 [90, "AuthConnectDevices"],
210 [91, "AuthConnectDeniedRequests"],
211 [92, "QueuedTurnInputIndexJsonValid"],
212 [93, "ProviderTurnAttemptIndex"],
213 [94, "TurnItemMessageOrdinalIndex"],
214 [95, "TurnItemInlineWorkOutput"],
215 [96, "RepairStaleTitleRegeneration"],
216 [97, "IssueTrackerBindings"],
217 [98, "ActiveShellIndexIndexes"],
218 [99, "BackgroundRecoveryIndexes"],
219 [100, "TurnStartReadIndexes"],
220 [101, "DelegatedTaskReadIndexes"],
221 [102, "ThreadItemCounts"],
222 [103, "IssueTrackerAdditionalScopes"],
223 [104, "ProjectionProjectsTrackedFolders"],
224 [105, "TaskFolders"],
225 [106, "WorktreePathKeyIndex"],
226 [107, "DeferredSlashCommandIndex"],
227 [108, "AgentFeedback"],
228 [109, "IssueTrackerAccounts"],
229 [110, "Workflows"],
230 [111, "SupersededRunAttemptIndex"],
231 [112, "LayoutCommandApprovals"],
232 [113, "WorkflowSkippedRuns"],
233 [114, "WorkflowRunIndexes"],
234 ]);
235 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
236 );
237​
238 it.effect("refuses unexpected later migrations without modifying their history", () =>
239 Effect.gen(function* () {
240 const sql = yield* SqlClient.SqlClient;
241 yield* seedPreview;
242 yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES (54, 'UnknownFork')`;
243 const history = yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`;
244 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
245 assert.deepStrictEqual(
246 yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`,
247 history,
248 );
249 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
250 );
251});