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

reconcileV2PreviewMigration.test.ts

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