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

reconcileV2PreviewMigration.test.ts

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