Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Migrations.test.ts

145 lines5.8 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import * as Effect from "effect/Effect";
5import * as Exit from "effect/Exit";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as SqlClient from "effect/sql/SqlClient";
9​
10import { migrationManifest, runMigrations } from "./Migrations.ts";
11​
12const v2Migrations = migrationManifest.filter(([id]) => id >= 55);
13const newerMigrationId = Math.max(...migrationManifest.map(([id]) => id)) + 1;
14​
15const history = Effect.gen(function* () {
16 const sql = yield* SqlClient.SqlClient;
17 const rows = yield* sql<{ readonly migration_id: number; readonly name: string }>`
18 SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
19 `;
20 return rows.map((row) => [row.migration_id, row.name] as const);
21});
22​
23const v2TableCount = Effect.gen(function* () {
24 const sql = yield* SqlClient.SqlClient;
25 const rows = yield* sql`
26 SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'orchestration_v2_%'
27 `;
28 return rows.length;
29});
30​
31describe("runMigrations", () => {
32 it.effect("refuses to start when another migration took a V2 id", () =>
33 Effect.gen(function* () {
34 const sql = yield* SqlClient.SqlClient;
35 yield* runMigrations({ toMigrationInclusive: 54 });
36 yield* sql`
37 INSERT INTO effect_sql_migrations (migration_id, name) VALUES (55, 'FutureMainMigration')
38 `;
39 const before = yield* history;
40​
41 assert.deepStrictEqual(yield* runMigrations({ toMigrationInclusive: 54 }), []);
42 const exit = yield* Effect.exit(runMigrations());
43​
44 assert.isTrue(Exit.isFailure(exit));
45 assert.include(String(exit), "55_OrchestrationV2 (recorded: FutureMainMigration)");
46 assert.deepStrictEqual(yield* history, before);
47 assert.strictEqual(yield* v2TableCount, 0);
48 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
49 );
50​
51 it.effect("refuses to start when a later id would skip V2 migrations", () =>
52 Effect.gen(function* () {
53 const sql = yield* SqlClient.SqlClient;
54 yield* runMigrations({ toMigrationInclusive: 54 });
55 yield* sql`
56 INSERT INTO effect_sql_migrations (migration_id, name)
57 VALUES (${newerMigrationId}, 'FutureMainMigration')
58 `;
59 const before = yield* history;
60​
61 const exit = yield* Effect.exit(runMigrations());
62​
63 assert.isTrue(Exit.isFailure(exit));
64 for (const [id, name] of v2Migrations) {
65 assert.include(String(exit), `${id}_${name} (recorded: nothing)`);
66 }
67 assert.deepStrictEqual(yield* history, before);
68 assert.strictEqual(yield* v2TableCount, 0);
69 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
70 );
71​
72 it.effect("upgrades a database at exactly 54", () =>
73 Effect.gen(function* () {
74 yield* runMigrations({ toMigrationInclusive: 54 });
75 assert.deepStrictEqual(yield* runMigrations(), v2Migrations);
76 assert.deepStrictEqual(yield* history, migrationManifest);
77 assert.isAbove(yield* v2TableCount, 0);
78 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
79 );
80​
81 it.effect.each([30, 54])("still starts with a site-local migration at id %s", (id) =>
82 Effect.gen(function* () {
83 const sql = yield* SqlClient.SqlClient;
84 yield* runMigrations({ toMigrationInclusive: 54 });
85 yield* sql`
86 UPDATE effect_sql_migrations SET name = 'SiteLocalMigration' WHERE migration_id = ${id}
87 `;
88​
89 assert.deepStrictEqual(yield* runMigrations(), v2Migrations);
90 assert.isAbove(yield* v2TableCount, 0);
91 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
92 );
93​
94 it.effect("only checks V2 migrations within the requested limit", () =>
95 Effect.gen(function* () {
96 const sql = yield* SqlClient.SqlClient;
97 yield* runMigrations({ toMigrationInclusive: 55 });
98 yield* sql`
99 INSERT INTO effect_sql_migrations (migration_id, name)
100 VALUES (56, 'SiteLocalMigration'), (${newerMigrationId}, 'NewerV2Migration')
101 `;
102 const before = yield* history;
103​
104 assert.deepStrictEqual(yield* runMigrations({ toMigrationInclusive: 55 }), []);
105 const exit = yield* Effect.exit(runMigrations({ toMigrationInclusive: 56 }));
106 assert.isTrue(Exit.isFailure(exit));
107 assert.include(
108 String(exit),
109 "56_RemoveRedundantProjectionIndexes (recorded: SiteLocalMigration)",
110 );
111 assert.notInclude(String(exit), "57_ScheduledTaskWebhooks");
112 assert.deepStrictEqual(yield* history, before);
113 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
114 );
115​
116 it.effect("still starts after a newer build recorded a later V2 migration", () =>
117 Effect.gen(function* () {
118 const sql = yield* SqlClient.SqlClient;
119 yield* runMigrations();
120 yield* sql`
121 INSERT INTO effect_sql_migrations (migration_id, name)
122 VALUES (${newerMigrationId}, 'NewerV2Migration')
123 `;
124​
125 assert.deepStrictEqual(yield* runMigrations(), []);
126 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
127 );
128});
129​
130// The npm launcher reads the highest migration id from these file names.
131it.effect("registers every migration file", () =>
132 Effect.gen(function* () {
133 const fs = yield* FileSystem.FileSystem;
134 const path = yield* Path.Path;
135 const dir = path.join(import.meta.dirname, "Migrations");
136 const files = (yield* fs.readDirectory(dir)).flatMap((name) => {
137 const match = /^(\d+)_(\w+)\.ts$/.exec(name);
138 return match ? [[Number(match[1]), match[2]] as const] : [];
139 });
140 assert.deepStrictEqual(
141 files.toSorted(([a], [b]) => a - b),
142 migrationManifest,
143 );
144 }).pipe(Effect.provide(NodeServices.layer)),
145);