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