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

reconcileV2PreviewMigration.test.ts

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