The best way to control Claude Code, Codex and more from any computer or phone

main
ts 158 lines 4.83 KB
Raw
1 import { classifyMigrationHistory, rewrittenMigrationHistory } from "@alchemy.run/sql/Migrations";
2 import { hashMigrations } from "@alchemy.run/sql/SqlFile";
3 import { expect, layer } from "@alchemy.run/test";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8
9 const describe = layer(NodeServices.layer);
10
11 describe("rewrittenMigrationHistory", (it) => {
12 it.effect(
13 "treats missing prior hashes as not rewritten",
14 () =>
15 Effect.sync(() => {
16 expect(rewrittenMigrationHistory(undefined, { "0001.sql": "aaa" })).toBeUndefined();
17 }),
18 { tags: ["unit", "local"] },
19 );
20
21 it.effect(
22 "treats additive files as not rewritten",
23 () =>
24 Effect.sync(() => {
25 expect(
26 rewrittenMigrationHistory(
27 { "0001.sql": "aaa" },
28 { "0001.sql": "aaa", "0002.sql": "bbb" },
29 ),
30 ).toBeUndefined();
31 }),
32 { tags: ["unit", "local"] },
33 );
34
35 it.effect(
36 "detects a changed hash on an already-applied file",
37 () =>
38 Effect.sync(() => {
39 expect(
40 rewrittenMigrationHistory(
41 { "0001.sql": "aaa", "0002.sql": "bbb" },
42 { "0001.sql": "ccc", "0002.sql": "bbb" },
43 ),
44 ).toEqual({ changed: ["0001.sql"], removed: [] });
45 }),
46 { tags: ["unit", "local"] },
47 );
48
49 it.effect(
50 "detects a removed already-applied file",
51 () =>
52 Effect.sync(() => {
53 expect(
54 rewrittenMigrationHistory(
55 { "0001.sql": "aaa", "0002.sql": "bbb" },
56 { "0002.sql": "bbb" },
57 ),
58 ).toEqual({ changed: [], removed: ["0001.sql"] });
59 }),
60 { tags: ["unit", "local"] },
61 );
62 });
63
64 const writeDir = (files: Record<string, string>) =>
65 Effect.gen(function* () {
66 const fs = yield* FileSystem.FileSystem;
67 const path = yield* Path.Path;
68 const dir = yield* fs.makeTempDirectoryScoped({
69 prefix: "alchemy-mig-hist-",
70 });
71 for (const [name, sql] of Object.entries(files)) {
72 yield* fs.writeFileString(path.join(dir, name), sql);
73 }
74 return dir;
75 });
76
77 describe("classifyMigrationHistory", (it) => {
78 it.effect(
79 "classifies a new file as pending",
80 () =>
81 Effect.gen(function* () {
82 const dir = yield* writeDir({
83 "0001_init.sql": "CREATE TABLE users (id int);",
84 "0002_posts.sql": "CREATE TABLE posts (id int);",
85 });
86 const hashes = yield* hashMigrations(dir);
87 const previous = { ...hashes };
88 delete previous["0002_posts.sql"];
89 const change = yield* classifyMigrationHistory({
90 news: { migrations: dir },
91 output: {
92 migrationsTable: "__alchemy_migrations",
93 migrationsHashes: previous,
94 },
95 });
96 expect(change).toEqual({ kind: "pending" });
97 }),
98 { tags: ["unit", "local"] },
99 );
100
101 it.effect(
102 "classifies an edited prior file as rewritten",
103 () =>
104 Effect.gen(function* () {
105 const fs = yield* FileSystem.FileSystem;
106 const path = yield* Path.Path;
107 const dir = yield* writeDir({
108 "0001_init.sql": "CREATE TABLE users (id int);",
109 });
110 const previous = yield* hashMigrations(dir);
111 yield* fs.writeFileString(
112 path.join(dir, "0001_init.sql"),
113 "CREATE TABLE users (id int, name text);",
114 );
115 const change = yield* classifyMigrationHistory({
116 news: { migrations: dir },
117 output: {
118 migrationsTable: "__alchemy_migrations",
119 migrationsHashes: previous,
120 },
121 });
122 expect(change.kind).toBe("rewritten");
123 if (change.kind === "rewritten") {
124 expect(change.changed).toEqual(["0001_init.sql"]);
125 expect(change.removed).toEqual([]);
126 }
127 }),
128 { tags: ["unit", "local"] },
129 );
130
131 it.effect(
132 "classifies a deleted prior file as rewritten",
133 () =>
134 Effect.gen(function* () {
135 const fs = yield* FileSystem.FileSystem;
136 const path = yield* Path.Path;
137 const dir = yield* writeDir({
138 "0001_init.sql": "CREATE TABLE users (id int);",
139 "0002_posts.sql": "CREATE TABLE posts (id int);",
140 });
141 const previous = yield* hashMigrations(dir);
142 yield* fs.remove(path.join(dir, "0001_init.sql"));
143 const change = yield* classifyMigrationHistory({
144 news: { migrations: dir },
145 output: {
146 migrationsTable: "__alchemy_migrations",
147 migrationsHashes: previous,
148 },
149 });
150 expect(change.kind).toBe("rewritten");
151 if (change.kind === "rewritten") {
152 expect(change.changed).toEqual([]);
153 expect(change.removed).toEqual(["0001_init.sql"]);
154 }
155 }),
156 { tags: ["unit", "local"] },
157 );
158 });