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