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