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

reconcileV2PreviewMigration.test.ts

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