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