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