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