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

reconcileV2PreviewMigration.test.ts

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