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

reconcileV2PreviewMigration.test.ts

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