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 OrchestrationV2 from "./Migrations/054_OrchestrationV2.ts";
10
11// The V2 schema is unchanged from the published September 15–16 previews.
12const seedPreview = Effect.gen(function* () {
13 const sql = yield* SqlClient.SqlClient;
14 yield* runMigrations({ toMigrationInclusive: 52 });
15 yield* Migrator.make({})({
16 loader: Migrator.fromRecord({ "53_OrchestrationV2": OrchestrationV2 }),
17 });
18 yield* sql`
19 INSERT INTO orchestration_v2_legacy_imports
20 (thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count)
21 VALUES ('preview-thread', '2026-09-15', '2026-09-15', '2026-09-16', 42)
22 `;
23 yield* sql`
24 UPDATE effect_sql_migrations SET created_at = '2026-09-15 00:00:00' WHERE migration_id = 53
25 `;
26});
27
28describe("V2 preview upgrade", () => {
29 it.effect("upgrades a published preview without replaying V2 or losing import progress", () =>
30 Effect.gen(function* () {
31 const sql = yield* SqlClient.SqlClient;
32 yield* seedPreview;
33 const imports = yield* sql`SELECT * FROM orchestration_v2_legacy_imports`;
34 assert.deepStrictEqual(yield* runMigrations(), [[53, "PullRequestFilesViewed"]]);
35 assert.deepStrictEqual(yield* runMigrations(), []);
36 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
37 const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
38 SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
39 `;
40 assert.deepStrictEqual(
41 history.map((row) => [row.migration_id, row.name] as const),
42 migrationManifest,
43 );
44 assert.deepStrictEqual(
45 yield* sql`SELECT created_at FROM effect_sql_migrations WHERE migration_id = 54`,
46 [{ created_at: "2026-09-15 00:00:00" }],
47 );
48 yield* sql`
49 INSERT INTO pull_request_files_viewed
50 (provider, host, repository, number, viewer, path, revision, viewed_at)
51 VALUES ('github', 'github.com', 'owner/repo', 1, 'viewer', 'file.ts', 'revision', '2026-09-17')
52 `;
53 assert.strictEqual((yield* sql`SELECT * FROM pull_request_files_viewed`).length, 1);
54 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
55 );
56
57 it.effect("rolls back schema and ledger together on failure and can retry", () =>
58 Effect.gen(function* () {
59 const sql = yield* SqlClient.SqlClient;
60 yield* seedPreview;
61 yield* sql`
62 CREATE TRIGGER fail_preview_upgrade BEFORE INSERT ON effect_sql_migrations
63 WHEN NEW.name = 'PullRequestFilesViewed'
64 BEGIN SELECT RAISE(ABORT, 'injected failure'); END
65 `;
66 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
67 assert.deepStrictEqual(
68 yield* sql`SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 53`,
69 [{ migration_id: 53, name: "OrchestrationV2" }],
70 );
71 assert.deepStrictEqual(
72 yield* sql`SELECT name FROM sqlite_master WHERE name = 'pull_request_files_viewed'`,
73 [],
74 );
75 assert.strictEqual((yield* sql`SELECT * FROM orchestration_v2_legacy_imports`).length, 1);
76 yield* sql`DROP TRIGGER fail_preview_upgrade`;
77 assert.deepStrictEqual(yield* runMigrations(), [[53, "PullRequestFilesViewed"]]);
78 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
79 );
80
81 it.effect("refuses unexpected later migrations without modifying their history", () =>
82 Effect.gen(function* () {
83 const sql = yield* SqlClient.SqlClient;
84 yield* seedPreview;
85 yield* sql`INSERT INTO effect_sql_migrations (migration_id, name) VALUES (54, 'UnknownFork')`;
86 const history = yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`;
87 assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
88 assert.deepStrictEqual(
89 yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`,
90 history,
91 );
92 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
93 );
94});