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