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