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