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