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

reconcileV2PreviewMigration.test.ts

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