The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | detectLayout, |
| 3 | inlineSqlParams, |
| 4 | normalizeMigrationsInput, |
| 5 | readDrizzleDirRecords, |
| 6 | readFlatRecords, |
| 7 | resolveMigrations, |
| 8 | timestampPrefixMillis, |
| 9 | } from "@alchemy.run/sql/Migrations"; |
| 10 | import { describe as plainDescribe, expect, layer, test } from "@alchemy.run/test"; |
| 11 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Result from "effect/Result"; |
| 14 | |
| 15 | const fixture = (name: string) => new URL(`./fixtures/${name}`, import.meta.url).pathname; |
| 16 | |
| 17 | const describe = layer(NodeServices.layer); |
| 18 | |
| 19 | describe("detectLayout", (it) => { |
| 20 | it.effect( |
| 21 | "classifies drizzle-kit v1 dirs as directory layout", |
| 22 | () => |
| 23 | Effect.gen(function* () { |
| 24 | expect(yield* detectLayout(fixture("drizzle-v1"))).toBe("directory"); |
| 25 | }), |
| 26 | { tags: ["unit", "local"] }, |
| 27 | ); |
| 28 | |
| 29 | it.effect( |
| 30 | "classifies Prisma dirs as directory layout too", |
| 31 | () => |
| 32 | // Prisma and drizzle-v1 share the `<ts>_<name>/migration.sql` shape; |
| 33 | // both key records by directory name. |
| 34 | Effect.gen(function* () { |
| 35 | expect(yield* detectLayout(fixture("prisma"))).toBe("directory"); |
| 36 | }), |
| 37 | { tags: ["unit", "local"] }, |
| 38 | ); |
| 39 | |
| 40 | it.effect( |
| 41 | "classifies flat .sql directories", |
| 42 | () => |
| 43 | Effect.gen(function* () { |
| 44 | expect(yield* detectLayout(fixture("flat"))).toBe("flat"); |
| 45 | }), |
| 46 | { tags: ["unit", "local"] }, |
| 47 | ); |
| 48 | |
| 49 | it.effect( |
| 50 | "fails on drizzle-v0 layouts with the drizzle-kit up hint", |
| 51 | () => |
| 52 | Effect.gen(function* () { |
| 53 | const result = yield* Effect.result(detectLayout(fixture("drizzle-v0"))); |
| 54 | expect(Result.isFailure(result)).toBe(true); |
| 55 | if (Result.isFailure(result)) { |
| 56 | expect(result.failure._tag).toBe("DrizzleV0LayoutError"); |
| 57 | expect(result.failure.message).toContain("drizzle-kit up"); |
| 58 | } |
| 59 | }), |
| 60 | { tags: ["unit", "local"] }, |
| 61 | ); |
| 62 | |
| 63 | it.effect( |
| 64 | "treats a missing directory as flat", |
| 65 | () => |
| 66 | Effect.gen(function* () { |
| 67 | expect(yield* detectLayout(fixture("does-not-exist"))).toBe("flat"); |
| 68 | }), |
| 69 | { tags: ["unit", "local"] }, |
| 70 | ); |
| 71 | }); |
| 72 | |
| 73 | describe("readers", (it) => { |
| 74 | it.effect( |
| 75 | "directory-layout records are keyed by directory name", |
| 76 | () => |
| 77 | Effect.gen(function* () { |
| 78 | const records = yield* readDrizzleDirRecords(fixture("drizzle-v1")); |
| 79 | expect(records.map((r) => r.name)).toEqual([ |
| 80 | "20240101000000_init", |
| 81 | "20240102000000_add_posts", |
| 82 | ]); |
| 83 | expect(records[0].createdAtMillis).toBe(Date.UTC(2024, 0, 1)); |
| 84 | // statement-breakpoint splitting |
| 85 | expect(records[0].statements.length).toBe(2); |
| 86 | expect(records[0].hash).toMatch(/^[0-9a-f]{64}$/); |
| 87 | }), |
| 88 | { tags: ["unit", "local"] }, |
| 89 | ); |
| 90 | |
| 91 | it.effect( |
| 92 | "flat records are keyed by relative file path", |
| 93 | () => |
| 94 | Effect.gen(function* () { |
| 95 | const records = yield* readFlatRecords(fixture("flat")); |
| 96 | expect(records.map((r) => r.name)).toEqual(["0001_users.sql", "0002_posts.sql"]); |
| 97 | }), |
| 98 | { tags: ["unit", "local"] }, |
| 99 | ); |
| 100 | }); |
| 101 | |
| 102 | plainDescribe("resolveMigrations", { tags: ["local"] }, () => { |
| 103 | test( |
| 104 | "defaults to __alchemy_migrations", |
| 105 | () => { |
| 106 | expect(resolveMigrations({ input: { dir: "./m" }, stamped: {} })).toEqual({ |
| 107 | dir: "./m", |
| 108 | table: "__alchemy_migrations", |
| 109 | }); |
| 110 | }, |
| 111 | { tags: ["unit"] }, |
| 112 | ); |
| 113 | |
| 114 | test( |
| 115 | "a table persisted by a prior deploy wins over the default", |
| 116 | () => { |
| 117 | // Pre-registry deploys persisted their table name (d1_migrations, |
| 118 | // neon_migrations, custom names); honoring it keeps them converging |
| 119 | // against the same table, upgraded in place. |
| 120 | expect( |
| 121 | resolveMigrations({ input: { dir: "./m" }, stamped: { table: "neon_migrations" } }).table, |
| 122 | ).toBe("neon_migrations"); |
| 123 | }, |
| 124 | { tags: ["unit"] }, |
| 125 | ); |
| 126 | |
| 127 | test( |
| 128 | "an explicit table always wins", |
| 129 | () => { |
| 130 | expect( |
| 131 | resolveMigrations({ |
| 132 | input: { dir: "./m", table: "my_migrations" }, |
| 133 | stamped: { table: "d1_migrations" }, |
| 134 | }).table, |
| 135 | ).toBe("my_migrations"); |
| 136 | }, |
| 137 | { tags: ["unit"] }, |
| 138 | ); |
| 139 | }); |
| 140 | |
| 141 | plainDescribe("normalizeMigrationsInput", { tags: ["local"] }, () => { |
| 142 | test( |
| 143 | "string is a directory", |
| 144 | () => { |
| 145 | expect(normalizeMigrationsInput("./migrations")).toEqual({ dir: "./migrations" }); |
| 146 | }, |
| 147 | { tags: ["unit"] }, |
| 148 | ); |
| 149 | test( |
| 150 | "Drizzle.Schema-shaped outputs are accepted structurally", |
| 151 | () => { |
| 152 | expect(normalizeMigrationsInput({ out: "./migrations" })).toEqual({ dir: "./migrations" }); |
| 153 | }, |
| 154 | { tags: ["unit"] }, |
| 155 | ); |
| 156 | test( |
| 157 | "object form passes through", |
| 158 | () => { |
| 159 | expect(normalizeMigrationsInput({ dir: "./m", table: "t" })).toEqual({ |
| 160 | dir: "./m", |
| 161 | table: "t", |
| 162 | }); |
| 163 | }, |
| 164 | { tags: ["unit"] }, |
| 165 | ); |
| 166 | }); |
| 167 | |
| 168 | plainDescribe("helpers", { tags: ["local"] }, () => { |
| 169 | test( |
| 170 | "timestampPrefixMillis parses drizzle dir prefixes", |
| 171 | () => { |
| 172 | expect(timestampPrefixMillis("20240101000000_init")).toBe(Date.UTC(2024, 0, 1)); |
| 173 | expect(timestampPrefixMillis("0001_users.sql")).toBeUndefined(); |
| 174 | }, |
| 175 | { tags: ["unit"] }, |
| 176 | ); |
| 177 | |
| 178 | test( |
| 179 | "inlineSqlParams inlines ? placeholders outside quotes", |
| 180 | () => { |
| 181 | expect(inlineSqlParams("INSERT INTO t (a, b) VALUES (?, ?);", ["it's", 42], "sqlite")).toBe( |
| 182 | "INSERT INTO t (a, b) VALUES ('it''s', 42);", |
| 183 | ); |
| 184 | expect(inlineSqlParams("SELECT * FROM t WHERE a = 'lit?' AND b = ?;", [1], "sqlite")).toBe( |
| 185 | "SELECT * FROM t WHERE a = 'lit?' AND b = 1;", |
| 186 | ); |
| 187 | }, |
| 188 | { tags: ["unit"] }, |
| 189 | ); |
| 190 | |
| 191 | test( |
| 192 | "inlineSqlParams inlines $n placeholders for postgres", |
| 193 | () => { |
| 194 | expect(inlineSqlParams("SELECT $1, $2;", ["x", null], "postgres")).toBe("SELECT 'x', NULL;"); |
| 195 | }, |
| 196 | { tags: ["unit"] }, |
| 197 | ); |
| 198 | }); |