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

main
ts 114 lines 4.37 KB
Raw
1 import { Database } from "bun:sqlite";
2 import { applyMigrations } from "@alchemy.run/sql/Migrations";
3 import { expect, layer } from "@alchemy.run/test";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import { drizzle } from "drizzle-orm/bun-sqlite";
6 import { migrate as drizzleMigrate } from "drizzle-orm/bun-sqlite/migrator";
7 import * as Effect from "effect/Effect";
8 import { makeSqliteExecutor, tableNames } from "./sqlite-executor.ts";
9
10 /**
11 * Adopting a drizzle-kit-migrated database (the Seth case): the user ran
12 * `drizzle-kit generate` + `drizzle-kit migrate` before Alchemy ever saw
13 * the database. Migrating to Alchemy is a ONE-WAY conversion — drizzle's
14 * REAL migrator (bun-sqlite driver) produces the starting state, our flow
15 * copies its history into `__alchemy_migrations` and owns bookkeeping from
16 * then on; `__drizzle_migrations` is frozen, never written or dropped.
17 */
18
19 const fixturesDir = new URL("./fixtures/drizzle-v1", import.meta.url).pathname;
20
21 const describe = layer(NodeServices.layer);
22
23 const rowsOf = (db: Database, table: string) =>
24 db.query(`SELECT hash, name FROM ${table} ORDER BY id;`).all() as Array<{
25 hash: string;
26 name: string;
27 }>;
28
29 describe("drizzle adoption (one-way conversion)", (it) => {
30 it.effect(
31 "converts drizzle-kit history into __alchemy_migrations without replaying",
32 () =>
33 Effect.gen(function* () {
34 const db = new Database(":memory:");
35 // The user's pre-Alchemy state, produced by drizzle's own migrator.
36 yield* Effect.sync(() =>
37 drizzleMigrate(drizzle({ client: db }), { migrationsFolder: fixturesDir }),
38 );
39 const drizzleRows = rowsOf(db, "__drizzle_migrations");
40 expect(drizzleRows.length).toBe(2);
41
42 // First Alchemy deploy: adopt. A replay would throw on the bare
43 // CREATE TABLEs, so passing proves conversion-not-reapplication.
44 const executor = makeSqliteExecutor(db);
45 yield* applyMigrations({
46 resolved: { dir: fixturesDir, table: "__alchemy_migrations" },
47 executor,
48 });
49
50 const ours = rowsOf(db, "__alchemy_migrations");
51 expect(ours.map((r) => r.name)).toEqual([
52 "20240101000000_init",
53 "20240102000000_add_posts",
54 ]);
55 // drizzle's hashes are sha256 of migration.sql — carried verbatim.
56 expect(ours.map((r) => r.hash)).toEqual(drizzleRows.map((r) => r.hash));
57 // The drizzle table is frozen, not dropped.
58 expect(rowsOf(db, "__drizzle_migrations")).toEqual(drizzleRows);
59 expect(tableNames(db)).toEqual(
60 expect.arrayContaining(["users", "posts", "__drizzle_migrations"]),
61 );
62 }),
63 { tags: ["unit", "local"] },
64 );
65
66 it.effect(
67 "after conversion, drizzle's table is never written again",
68 () =>
69 Effect.gen(function* () {
70 const db = new Database(":memory:");
71 yield* Effect.sync(() =>
72 drizzleMigrate(drizzle({ client: db }), { migrationsFolder: fixturesDir }),
73 );
74 const frozen = rowsOf(db, "__drizzle_migrations");
75
76 const executor = makeSqliteExecutor(db);
77 yield* applyMigrations({
78 resolved: { dir: fixturesDir, table: "__alchemy_migrations" },
79 executor,
80 });
81 yield* applyMigrations({
82 resolved: { dir: fixturesDir, table: "__alchemy_migrations" },
83 executor,
84 });
85
86 // One-way: drizzle's table never moves again.
87 expect(rowsOf(db, "__drizzle_migrations")).toEqual(frozen);
88 expect(rowsOf(db, "__alchemy_migrations").length).toBe(2);
89 }),
90 { tags: ["unit", "local"] },
91 );
92
93 it.effect(
94 "our flow is idempotent across repeated deploys",
95 () =>
96 Effect.gen(function* () {
97 const db = new Database(":memory:");
98 const executor = makeSqliteExecutor(db);
99 yield* applyMigrations({
100 resolved: { dir: fixturesDir, table: "__alchemy_migrations" },
101 executor,
102 });
103 const first = rowsOf(db, "__alchemy_migrations");
104 yield* applyMigrations({
105 resolved: { dir: fixturesDir, table: "__alchemy_migrations" },
106 executor,
107 });
108 expect(rowsOf(db, "__alchemy_migrations")).toEqual(first);
109 // No drizzle table was ever created on a greenfield database.
110 expect(tableNames(db)).not.toContain("__drizzle_migrations");
111 }),
112 { tags: ["unit", "local"] },
113 );
114 });