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

reconcileV2PreviewMigration.test.ts

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