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

reconcileV2PreviewMigration.test.ts

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