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

reconcileV2PreviewMigration.test.ts

146 lines6.5 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​
13// The V2 schema is unchanged from the published September 15–16 previews.
14const seedPreview = Effect.gen(function* () {
15 const sql = yield* SqlClient.SqlClient;
16 yield* runMigrations({ toMigrationInclusive: 52 });
17 yield* Migrator.make({})({
18 loader: Migrator.fromRecord({ "53_OrchestrationV2": OrchestrationV2 }),
19 });
20 yield* sql`
21 INSERT INTO orchestration_v2_legacy_imports
22 (thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count)
23 VALUES ('preview-thread', '2026-09-15', '2026-09-15', '2026-09-16', 42)
24 `;
25 yield* sql`
26 UPDATE effect_sql_migrations SET created_at = '2026-09-15 00:00:00' WHERE migration_id = 53
27 `;
28});
29​
30describe("V2 preview upgrade", () => {
31 it.effect("upgrades a published preview without replaying V2 or losing import progress", () =>
32 Effect.gen(function* () {
33 const sql = yield* SqlClient.SqlClient;
34 yield* seedPreview;
35 const imports = yield* sql`SELECT * FROM orchestration_v2_legacy_imports`;
36 assert.deepStrictEqual(yield* runMigrations(), [
37 [53, "PullRequestFilesViewed"],
38 [54, "ProjectionThreadsAutoSettleDisabledAt"],
39 [56, "RemoveRedundantProjectionIndexes"],
40 [57, "ScheduledTaskWebhooks"],
41 [58, "WebhookRelayDeliveries"],
42 [59, "McpAppModelContext"],
43 [60, "ThreadSnapshotWindowIndexes"],
44 [61, "PullRequestReviewGuides"],
45 ]);
46 assert.deepStrictEqual(yield* runMigrations(), []);
47 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
48 const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
49 SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
50 `;
51 assert.deepStrictEqual(
52 history.map((row) => [row.migration_id, row.name] as const),
53 migrationManifest,
54 );
55 assert.deepStrictEqual(
56 yield* sql`SELECT created_at FROM effect_sql_migrations WHERE migration_id = 55`,
57 [{ created_at: "2026-09-15 00:00:00" }],
58 );
59 yield* sql`
60 INSERT INTO pull_request_files_viewed
61 (provider, host, repository, number, viewer, path, revision, viewed_at)
62 VALUES ('github', 'github.com', 'owner/repo', 1, 'viewer', 'file.ts', 'revision', '2026-09-17')
63 `;
64 assert.strictEqual((yield* sql`SELECT * FROM pull_request_files_viewed`).length, 1);
65 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
66 );
67​
68 it.effect.each([false, true])(
69 "upgrades preview migration 54 with index cleanup %s",
70 (withIndexes) =>
71 Effect.gen(function* () {
72 const sql = yield* SqlClient.SqlClient;
73 yield* runMigrations({ toMigrationInclusive: 52 });
74 yield* Migrator.make({})({
75 loader: Migrator.fromRecord({
76 "53_PullRequestFilesViewed": PullRequestFilesViewed,
77 "54_OrchestrationV2": OrchestrationV2,
78 ...(withIndexes
79 ? { "55_RemoveRedundantProjectionIndexes": RemoveRedundantProjectionIndexes }
80 : {}),
81 }),
82 });
83 yield* runMigrations();
84 assert.deepStrictEqual(yield* runMigrations(), []);
85 const history = yield* sql<{
86 readonly migration_id: number;
87 readonly name: string;
88 }>`SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id`;
89 assert.deepStrictEqual(
90 history.map((row) => [row.migration_id, row.name] as const),
91 migrationManifest,
92 );
93 const columns = yield* sql<{
94 readonly name: string;
95 }>`PRAGMA table_info(projection_threads)`;
96 assert.ok(columns.some((column) => column.name === "auto_settle_disabled_at"));
97 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
98 );
99​
100 it.effect("rolls back schema and ledger together on failure and can retry", () =>
101 Effect.gen(function* () {
102 const sql = yield* SqlClient.SqlClient;
103 yield* seedPreview;
104 yield* sql`
105 CREATE TRIGGER fail_preview_upgrade BEFORE INSERT ON effect_sql_migrations
106 WHEN NEW.name = 'PullRequestFilesViewed'
107 BEGIN SELECT RAISE(ABORT, 'injected failure'); END
108 `;
109 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
110 assert.deepStrictEqual(
111 yield* sql`SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53`,
112 [{ migration_id: 53, name: "OrchestrationV2" }],
113 );
114 assert.deepStrictEqual(
115 yield* sql`SELECT name FROM sqlite_master WHERE name = 'pull_request_files_viewed'`,
116 [],
117 );
118 assert.strictEqual((yield* sql`SELECT * FROM orchestration_v2_legacy_imports`).length, 1);
119 yield* sql`DROP TRIGGER fail_preview_upgrade`;
120 assert.deepStrictEqual(yield* runMigrations(), [
121 [53, "PullRequestFilesViewed"],
122 [54, "ProjectionThreadsAutoSettleDisabledAt"],
123 [56, "RemoveRedundantProjectionIndexes"],
124 [57, "ScheduledTaskWebhooks"],
125 [58, "WebhookRelayDeliveries"],
126 [59, "McpAppModelContext"],
127 [60, "ThreadSnapshotWindowIndexes"],
128 [61, "PullRequestReviewGuides"],
129 ]);
130 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
131 );
132​
133 it.effect("refuses unexpected later migrations without modifying their history", () =>
134 Effect.gen(function* () {
135 const sql = yield* SqlClient.SqlClient;
136 yield* seedPreview;
137 yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES (54, 'UnknownFork')`;
138 const history = yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`;
139 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
140 assert.deepStrictEqual(
141 yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`,
142 history,
143 );
144 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
145 );
146});