Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

reconcileV2PreviewMigration.test.ts

193 lines8.3 KB

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