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