@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 1,386 lines 51.8 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { hashMigrations } from "@alchemy.run/sql/SqlFile";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as d1 from "@distilled.cloud/cloudflare/d1";
7 import * as workers from "@distilled.cloud/cloudflare/workers";
8 import * as Drift from "alchemy/Drift";
9 import { State } from "alchemy/State";
10 import * as Context from "effect/Context";
11 import * as Data from "effect/Data";
12 import * as Duration from "effect/Duration";
13 import * as Effect from "effect/Effect";
14 import * as FileSystem from "effect/FileSystem";
15 import * as HttpClient from "effect/http/HttpClient";
16 import * as HttpClientRequest from "effect/http/HttpClientRequest";
17 import * as HttpClientResponse from "effect/http/HttpClientResponse";
18 import * as Layer from "effect/Layer";
19 import * as Path from "effect/Path";
20 import * as Ref from "effect/Ref";
21 import { MinimumLogLevel } from "effect/References";
22 import * as Schedule from "effect/Schedule";
23 import { waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
24
25 const { test } = Test.make({ providers: Cloudflare.providers() });
26
27 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
28
29 test.provider(
30 "create and delete database with default props",
31 (stack) =>
32 Effect.gen(function* () {
33 const { accountId } = yield* yield* CloudflareEnvironment;
34
35 yield* stack.destroy();
36
37 const database = yield* stack.deploy(
38 Effect.gen(function* () {
39 return yield* Cloudflare.D1.Database("DefaultDatabase");
40 }),
41 );
42
43 expect(database.databaseName).toBeDefined();
44 expect(database.databaseId).toBeDefined();
45
46 const actualDatabase = yield* d1.getDatabase({ accountId, databaseId: database.databaseId });
47 expect(actualDatabase.uuid).toEqual(database.databaseId);
48
49 // Cloudflare reports `{ mode: "disabled" }` for an omitted
50 // `readReplication`; `read` keeps the persisted shape, so an unchanged
51 // database is not reported as drifted.
52 const detectDrift = () => Drift.detect(stack).pipe(Effect.provide(stack.state));
53 const unchanged = yield* detectDrift();
54 expect(unchanged.resources.DefaultDatabase?.action).toBe("unchanged");
55
56 // Enabling replication outside Alchemy is still real drift.
57 yield* d1.patchDatabase({
58 accountId,
59 databaseId: database.databaseId,
60 readReplication: { mode: "auto" },
61 });
62 const changed = yield* detectDrift();
63 expect(changed.resources.DefaultDatabase?.action).toBe("drifted");
64 expect(changed.resources.DefaultDatabase?.attr.readReplication).toEqual({ mode: "auto" });
65
66 yield* stack.destroy();
67
68 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
69 }).pipe(logLevel),
70 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
71 );
72
73 test.provider(
74 "create, update, delete database",
75 (stack) =>
76 Effect.gen(function* () {
77 const { accountId } = yield* yield* CloudflareEnvironment;
78
79 yield* stack.destroy();
80
81 const database = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* Cloudflare.D1.Database("TestDatabase", {
84 readReplication: { mode: "disabled" },
85 });
86 }),
87 );
88
89 const actualDatabase = yield* d1.getDatabase({ accountId, databaseId: database.databaseId });
90 expect(actualDatabase.uuid).toEqual(database.databaseId);
91
92 const updatedDatabase = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* Cloudflare.D1.Database("TestDatabase", {
95 readReplication: { mode: "auto" },
96 });
97 }),
98 );
99
100 expect(updatedDatabase.databaseId).toEqual(database.databaseId);
101
102 const actualUpdatedDatabase = yield* d1.getDatabase({
103 accountId,
104 databaseId: updatedDatabase.databaseId,
105 });
106 expect(actualUpdatedDatabase.readReplication?.mode).toEqual("auto");
107
108 yield* stack.destroy();
109
110 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
111 }).pipe(logLevel),
112 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
113 );
114
115 test.provider(
116 "replace keeping an explicit name deletes the old database first and rebinds its Worker",
117 (stack) =>
118 Effect.gen(function* () {
119 const { accountId } = yield* yield* CloudflareEnvironment;
120 const path = yield* Path.Path;
121 const main = path.join(import.meta.dirname, "fixtures", "replace-worker.ts");
122
123 // The database is bound to a Worker so the replacement is exercised
124 // with a live dependent: the old database is deleted while the Worker
125 // still binds it, and the Worker must then be re-bound to the new one.
126 const deployWithWorker = (props: Cloudflare.D1.DatabaseProps) =>
127 stack.deploy(
128 Effect.gen(function* () {
129 const database = yield* Cloudflare.D1.Database("ReplacedDatabase", props);
130 const worker = yield* Cloudflare.Worker("ReplacedDatabaseWorker", {
131 main,
132 env: { DB: database },
133 });
134 return {
135 databaseId: database.databaseId,
136 databaseName: database.databaseName,
137 workerName: worker.workerName,
138 url: worker.url.as<string>(),
139 };
140 }),
141 );
142
143 yield* stack.destroy();
144
145 // A generated name keeps concurrent runs collision-free; the replace
146 // below pins it as the explicit name.
147 const initial = yield* deployWithWorker({});
148 yield* writeEntry(initial.url);
149 expect(yield* readEntries(initial.url)).toEqual(1);
150
151 // `primaryLocationHint` is fixed at creation, so this is a replace
152 // whose replacement has the same name as the database it replaces.
153 const replaced = yield* deployWithWorker({
154 name: initial.databaseName,
155 primaryLocationHint: "weur",
156 });
157
158 expect(replaced.databaseName).toEqual(initial.databaseName);
159 expect(replaced.databaseId).not.toEqual(initial.databaseId);
160 yield* waitForDatabaseToBeDeleted(initial.databaseId, accountId);
161 const actualDatabase = yield* d1.getDatabase({
162 accountId,
163 databaseId: replaced.databaseId,
164 });
165 expect(actualDatabase.name).toEqual(initial.databaseName);
166
167 // The deployed Worker's D1 binding now points at the replacement.
168 const settings = yield* workers.getScriptScriptAndVersionSetting({
169 accountId,
170 scriptName: replaced.workerName,
171 });
172 const d1Bindings = (settings.bindings ?? []).flatMap((binding) =>
173 binding.type === "d1" ? [{ name: binding.name, databaseId: binding.databaseId }] : [],
174 );
175 expect(d1Bindings).toEqual([{ name: "DB", databaseId: replaced.databaseId }]);
176
177 // The replacement starts empty, and the Worker writes land in it.
178 yield* writeEntry(replaced.url);
179 expect(yield* readEntries(replaced.url)).toEqual(1);
180 expect(
181 yield* queryAll<{ n: number }>(
182 accountId,
183 replaced.databaseId,
184 "SELECT count(*) AS n FROM entries;",
185 ),
186 ).toEqual([{ n: 1 }]);
187
188 yield* stack.destroy();
189
190 yield* waitForDatabaseToBeDeleted(replaced.databaseId, accountId);
191 yield* waitForWorkerToBeDeleted(replaced.workerName, accountId);
192 }).pipe(logLevel),
193 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"], timeout: 180_000 },
194 );
195
196 test.provider(
197 "applies migrations from migrationsDir",
198 (stack) =>
199 Effect.gen(function* () {
200 const { accountId } = yield* yield* CloudflareEnvironment;
201 const fs = yield* FileSystem.FileSystem;
202 const path = yield* Path.Path;
203 const migrationsDir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-migrations-" });
204
205 yield* fs.writeFileString(
206 path.join(migrationsDir, "0001_users.sql"),
207 "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
208 );
209 yield* fs.writeFileString(
210 path.join(migrationsDir, "0002_posts.sql"),
211 "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);",
212 );
213
214 yield* stack.destroy();
215
216 const database = yield* stack.deploy(
217 Effect.gen(function* () {
218 return yield* Cloudflare.D1.Database("MigrationDatabase", { migrations: migrationsDir });
219 }),
220 );
221
222 expect(database.migrationsDir).toEqual(migrationsDir);
223 expect(database.migrationsTable).toEqual("__alchemy_migrations");
224 expect(Object.keys(database.migrationsHashes).sort()).toEqual([
225 "0001_users.sql",
226 "0002_posts.sql",
227 ]);
228
229 const tables = yield* listTables(accountId, database.databaseId);
230 expect(tables).toContain("users");
231 expect(tables).toContain("posts");
232 expect(tables).toContain("__alchemy_migrations");
233
234 // Alchemy's shape: INTEGER ids, name-keyed, hashed.
235 const applied = yield* queryAll<{ id: number; name: string; hash: string }>(
236 accountId,
237 database.databaseId,
238 "SELECT id, name, hash FROM __alchemy_migrations ORDER BY id;",
239 );
240 expect(applied.map((r) => ({ id: r.id, name: r.name }))).toEqual([
241 { id: 1, name: "0001_users.sql" },
242 { id: 2, name: "0002_posts.sql" },
243 ]);
244 expect(applied[0].hash).toMatch(/^[0-9a-f]{64}$/);
245
246 // Adding a new migration on update should apply only the new one and the
247 // sequential id should continue from where it left off.
248 yield* fs.writeFileString(
249 path.join(migrationsDir, "0003_comments.sql"),
250 "CREATE TABLE comments (id INTEGER PRIMARY KEY, body TEXT NOT NULL);",
251 );
252
253 const updated = yield* stack.deploy(
254 Effect.gen(function* () {
255 return yield* Cloudflare.D1.Database("MigrationDatabase", { migrations: migrationsDir });
256 }),
257 );
258 expect(updated.databaseId).toEqual(database.databaseId);
259
260 const tablesAfter = yield* listTables(accountId, database.databaseId);
261 expect(tablesAfter).toContain("comments");
262
263 const appliedAfter = yield* queryAll<{ id: number; name: string }>(
264 accountId,
265 database.databaseId,
266 "SELECT id, name FROM __alchemy_migrations ORDER BY id;",
267 );
268 expect(appliedAfter).toEqual([
269 { id: 1, name: "0001_users.sql" },
270 { id: 2, name: "0002_posts.sql" },
271 { id: 3, name: "0003_comments.sql" },
272 ]);
273
274 yield* stack.destroy();
275 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
276 }).pipe(logLevel),
277 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
278 );
279
280 test.provider(
281 "applies migrations using a custom migrationsTable",
282 (stack) =>
283 Effect.gen(function* () {
284 const { accountId } = yield* yield* CloudflareEnvironment;
285 const fs = yield* FileSystem.FileSystem;
286 const path = yield* Path.Path;
287 const migrationsDir = yield* fs.makeTempDirectory({
288 prefix: "alchemy-d1-custom-migrations-",
289 });
290 yield* fs.writeFileString(
291 path.join(migrationsDir, "0001_create.sql"),
292 "CREATE TABLE test_migrations_table (id INTEGER PRIMARY KEY, name TEXT);",
293 );
294
295 yield* stack.destroy();
296
297 const database = yield* stack.deploy(
298 Effect.gen(function* () {
299 return yield* Cloudflare.D1.Database("CustomMigrationsTableDb", {
300 migrations: { dir: migrationsDir, table: "custom_migration_tracking" },
301 });
302 }),
303 );
304
305 expect(database.migrationsTable).toEqual("custom_migration_tracking");
306
307 const tables = yield* listTables(accountId, database.databaseId);
308 expect(tables).toContain("custom_migration_tracking");
309 expect(tables).toContain("test_migrations_table");
310 // The default table must NOT be created when a custom one is configured.
311 expect(tables).not.toContain("__alchemy_migrations");
312
313 yield* stack.destroy();
314 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
315 }).pipe(logLevel),
316 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
317 );
318
319 /**
320 * D1 can run a migration batch and still fail its reply (#1889), or fail a
321 * request it never ran. A blind resend of the first would run its DDL twice
322 * and fail with "table already exists". The deploy instead reads
323 * `__alchemy_migrations`: a batch that landed is done, and one that did not
324 * is resent once.
325 */
326 class FlakyD1 extends Context.Service<FlakyD1, { sends: Ref.Ref<Record<string, number>> }>()(
327 "test/D1/FlakyD1",
328 ) {}
329
330 // Forward every request to Cloudflare, except the first batch that creates
331 // one of these tables: `lost_reply_notes` runs on D1 and its reply becomes a
332 // 503; `dropped_request_notes` gets a 503 without reaching D1.
333 const FlakyD1HttpClient = Layer.effect(
334 HttpClient.HttpClient,
335 Effect.gen(function* () {
336 const client = yield* HttpClient.HttpClient;
337 const { sends } = yield* FlakyD1;
338 const unavailable = (request: HttpClientRequest.HttpClientRequest) =>
339 HttpClientResponse.fromWeb(request, new Response("Service Unavailable", { status: 503 }));
340 return HttpClient.transform(client, (response, request) =>
341 Effect.gen(function* () {
342 const body =
343 request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "";
344 const table = request.url.endsWith("/query")
345 ? ["lost_reply_notes", "dropped_request_notes"].find((name) =>
346 body.includes(`CREATE TABLE ${name}`),
347 )
348 : undefined;
349 if (table === undefined) return yield* response;
350 const counts = yield* Ref.updateAndGet(sends, (counts) => ({
351 ...counts,
352 [table]: (counts[table] ?? 0) + 1,
353 }));
354 if (counts[table] !== 1) return yield* response;
355 if (table === "lost_reply_notes") yield* response;
356 return unavailable(request);
357 }),
358 );
359 }),
360 );
361
362 const flaky = Test.make({
363 providers: Cloudflare.providers().pipe(
364 Layer.provide(FlakyD1HttpClient),
365 Layer.provideMerge(
366 Layer.effect(
367 FlakyD1,
368 Effect.map(Ref.make<Record<string, number>>({}), (sends) => ({ sends })),
369 ),
370 ),
371 ),
372 });
373
374 // D1 ran the batch, so its bookkeeping row is there and the deploy succeeds
375 // without sending it again.
376 flaky.test.provider(
377 "a migration batch whose reply is lost is not resent",
378 (stack) =>
379 Effect.gen(function* () {
380 const { accountId } = yield* yield* CloudflareEnvironment;
381 const { sends } = yield* FlakyD1;
382 const fs = yield* FileSystem.FileSystem;
383 const path = yield* Path.Path;
384 const migrationsDir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-lost_reply_notes-" });
385 yield* fs.writeFileString(
386 path.join(migrationsDir, "0001_notes.sql"),
387 "CREATE TABLE lost_reply_notes (id INTEGER PRIMARY KEY, body TEXT NOT NULL);",
388 );
389 const program = Effect.gen(function* () {
390 return yield* Cloudflare.D1.Database("LostReplyDb", { migrations: migrationsDir });
391 });
392
393 yield* stack.destroy();
394 // A retried attempt starts over, so its first batch is faulted again.
395 yield* Ref.update(sends, (counts) => ({ ...counts, lost_reply_notes: 0 }));
396
397 const database = yield* stack.deploy(program);
398 expect((yield* Ref.get(sends))["lost_reply_notes"]).toEqual(1);
399 expect(yield* listTables(accountId, database.databaseId)).toContain("lost_reply_notes");
400 const applied = yield* queryAll<{ name: string }>(
401 accountId,
402 database.databaseId,
403 "SELECT name FROM __alchemy_migrations ORDER BY id;",
404 );
405 expect(applied.map((row) => row.name)).toEqual(["0001_notes.sql"]);
406
407 yield* stack.destroy();
408 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
409 }).pipe(logLevel),
410 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"], timeout: 180_000 },
411 );
412
413 // D1 never ran the batch, so `__alchemy_migrations` has no row for it and the
414 // deploy sends it once more.
415 flaky.test.provider(
416 "a migration batch that never reached D1 is resent once",
417 (stack) =>
418 Effect.gen(function* () {
419 const { accountId } = yield* yield* CloudflareEnvironment;
420 const { sends } = yield* FlakyD1;
421 const fs = yield* FileSystem.FileSystem;
422 const path = yield* Path.Path;
423 const migrationsDir = yield* fs.makeTempDirectory({
424 prefix: "alchemy-d1-dropped_request_notes-",
425 });
426 yield* fs.writeFileString(
427 path.join(migrationsDir, "0001_notes.sql"),
428 "CREATE TABLE dropped_request_notes (id INTEGER PRIMARY KEY, body TEXT NOT NULL);",
429 );
430 const program = Effect.gen(function* () {
431 return yield* Cloudflare.D1.Database("DroppedRequestDb", { migrations: migrationsDir });
432 });
433
434 yield* stack.destroy();
435 // A retried attempt starts over, so its first batch is faulted again.
436 yield* Ref.update(sends, (counts) => ({ ...counts, dropped_request_notes: 0 }));
437
438 const database = yield* stack.deploy(program);
439 expect((yield* Ref.get(sends))["dropped_request_notes"]).toEqual(2);
440 expect(yield* listTables(accountId, database.databaseId)).toContain("dropped_request_notes");
441 const applied = yield* queryAll<{ name: string }>(
442 accountId,
443 database.databaseId,
444 "SELECT name FROM __alchemy_migrations ORDER BY id;",
445 );
446 expect(applied.map((row) => row.name)).toEqual(["0001_notes.sql"]);
447
448 yield* stack.destroy();
449 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
450 }).pipe(logLevel),
451 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"], timeout: 180_000 },
452 );
453
454 /**
455 * Adopting a drizzle-kit-migrated database: the user ran `drizzle-kit
456 * migrate` before Alchemy ever saw the database, leaving history in
457 * `__drizzle_migrations`. First deploy performs the one-way conversion —
458 * history copies into `__alchemy_migrations` (validated against local
459 * files, hashes carried verbatim) and drizzle's table is frozen, never
460 * written or dropped.
461 */
462 test.provider(
463 "adopts a drizzle-kit-migrated database via one-way conversion",
464 (stack) =>
465 Effect.gen(function* () {
466 const { accountId } = yield* yield* CloudflareEnvironment;
467 const fs = yield* FileSystem.FileSystem;
468 const path = yield* Path.Path;
469 const migrationsDir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-drizzle-" });
470 const initSql =
471 "CREATE TABLE users (id integer PRIMARY KEY NOT NULL, name text NOT NULL);\n--> statement-breakpoint\nCREATE UNIQUE INDEX users_name_unique ON users (name);";
472 yield* fs.makeDirectory(path.join(migrationsDir, "20240101000000_init"));
473 yield* fs.writeFileString(
474 path.join(migrationsDir, "20240101000000_init", "migration.sql"),
475 initSql,
476 );
477 const [initRecord] = yield* hashMigrations(migrationsDir).pipe(
478 Effect.map((hashes) => Object.values(hashes)),
479 );
480
481 yield* stack.destroy();
482
483 // Phase 1: what `drizzle-kit migrate` left behind — deploy without
484 // migrations, then hand-write drizzle's table + the applied schema.
485 const seeded = yield* stack.deploy(
486 Effect.gen(function* () {
487 return yield* Cloudflare.D1.Database("DrizzleAdoptionDb");
488 }),
489 );
490 yield* execSql(
491 accountId,
492 seeded.databaseId,
493 "CREATE TABLE users (id integer PRIMARY KEY NOT NULL, name text NOT NULL);",
494 );
495 yield* execSql(
496 accountId,
497 seeded.databaseId,
498 "CREATE UNIQUE INDEX users_name_unique ON users (name);",
499 );
500 yield* execSql(
501 accountId,
502 seeded.databaseId,
503 `CREATE TABLE __drizzle_migrations (
504 id INTEGER PRIMARY KEY,
505 hash text NOT NULL,
506 created_at numeric,
507 name text,
508 applied_at TEXT
509 );`,
510 );
511 yield* execSql(
512 accountId,
513 seeded.databaseId,
514 `INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at) VALUES
515 ('${initRecord}', 1704067200000, '20240101000000_init', '2024-01-01T00:00:00.000Z');`,
516 );
517
518 // Phase 2: first Alchemy deploy with migrations + a new pending one.
519 yield* fs.makeDirectory(path.join(migrationsDir, "20240102000000_posts"));
520 yield* fs.writeFileString(
521 path.join(migrationsDir, "20240102000000_posts", "migration.sql"),
522 "CREATE TABLE posts (id integer PRIMARY KEY NOT NULL, title text NOT NULL);",
523 );
524 const database = yield* stack.deploy(
525 Effect.gen(function* () {
526 return yield* Cloudflare.D1.Database("DrizzleAdoptionDb", { migrations: migrationsDir });
527 }),
528 );
529 expect(database.databaseId).toEqual(seeded.databaseId);
530 expect(database.migrationsTable).toEqual("__alchemy_migrations");
531
532 // History converted (hash carried verbatim), only the pending
533 // migration ran (a replay of init's bare CREATE TABLE would fail).
534 const applied = yield* queryAll<{ name: string; hash: string }>(
535 accountId,
536 database.databaseId,
537 "SELECT name, hash FROM __alchemy_migrations ORDER BY id;",
538 );
539 expect(applied.map((r) => r.name)).toEqual(["20240101000000_init", "20240102000000_posts"]);
540 expect(applied[0].hash).toBe(initRecord);
541 const tables = yield* listTables(accountId, database.databaseId);
542 expect(tables).toContain("posts");
543
544 // One-way: drizzle's table is frozen, not dropped, not extended.
545 const frozen = yield* queryAll<{ name: string }>(
546 accountId,
547 database.databaseId,
548 "SELECT name FROM __drizzle_migrations ORDER BY id;",
549 );
550 expect(frozen.map((r) => r.name)).toEqual(["20240101000000_init"]);
551
552 yield* stack.destroy();
553 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
554 }).pipe(logLevel),
555 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
556 );
557
558 /**
559 * Adopting a wrangler-migrated database: `wrangler d1 migrations apply`
560 * left its real table behind; the first Alchemy deploy converts that
561 * history into `__alchemy_migrations` (hashes backfilled from local
562 * files) and freezes wrangler's table.
563 */
564 test.provider(
565 "adopts a wrangler-migrated database via one-way conversion",
566 (stack) =>
567 Effect.gen(function* () {
568 const { accountId } = yield* yield* CloudflareEnvironment;
569 const fs = yield* FileSystem.FileSystem;
570 const path = yield* Path.Path;
571 const migrationsDir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-wrangler-" });
572 yield* fs.writeFileString(
573 path.join(migrationsDir, "0001_users.sql"),
574 "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
575 );
576 yield* fs.writeFileString(
577 path.join(migrationsDir, "0002_posts.sql"),
578 "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);",
579 );
580
581 yield* stack.destroy();
582
583 // Phase 1: what `wrangler d1 migrations apply` left behind.
584 const seeded = yield* stack.deploy(
585 Effect.gen(function* () {
586 return yield* Cloudflare.D1.Database("WranglerAdoptionDb");
587 }),
588 );
589 yield* execSql(
590 accountId,
591 seeded.databaseId,
592 "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
593 );
594 yield* execSql(
595 accountId,
596 seeded.databaseId,
597 `CREATE TABLE d1_migrations(
598 id INTEGER PRIMARY KEY AUTOINCREMENT,
599 name TEXT UNIQUE,
600 applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
601 );`,
602 );
603 yield* execSql(
604 accountId,
605 seeded.databaseId,
606 "INSERT INTO d1_migrations (name) VALUES ('0001_users.sql');",
607 );
608
609 // Phase 2: first Alchemy deploy — converts, applies only 0002.
610 const database = yield* stack.deploy(
611 Effect.gen(function* () {
612 return yield* Cloudflare.D1.Database("WranglerAdoptionDb", { migrations: migrationsDir });
613 }),
614 );
615 expect(database.databaseId).toEqual(seeded.databaseId);
616 expect(database.migrationsTable).toEqual("__alchemy_migrations");
617
618 const applied = yield* queryAll<{ name: string; hash: string }>(
619 accountId,
620 database.databaseId,
621 "SELECT name, hash FROM __alchemy_migrations ORDER BY id;",
622 );
623 expect(applied.map((r) => r.name)).toEqual(["0001_users.sql", "0002_posts.sql"]);
624 expect(applied[0].hash).toMatch(/^[0-9a-f]{64}$/);
625 const tables = yield* listTables(accountId, database.databaseId);
626 expect(tables).toContain("posts");
627
628 // wrangler's table is frozen.
629 const frozen = yield* queryAll<{ name: string }>(
630 accountId,
631 database.databaseId,
632 "SELECT name FROM d1_migrations ORDER BY id;",
633 );
634 expect(frozen.map((r) => r.name)).toEqual(["0001_users.sql"]);
635
636 yield* stack.destroy();
637 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
638 }).pipe(logLevel),
639 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
640 );
641
642 /**
643 * True roll-forward: state row AND physical table are rewritten to exactly
644 * what pre-registry Alchemy persisted (3-column `id TEXT PK` table,
645 * `migrationsHashes` + `migrationsTable` in state), then a normal deploy
646 * runs on top. The persisted table name keeps winning (legacy deploys stay
647 * on `d1_migrations`), the table upgrades in place to Alchemy's shape, and
648 * nothing replays (0001/0002's bare CREATE TABLEs would fail).
649 */
650 test.provider(
651 "rolls forward from pre-registry state and 3-column table without replaying",
652 (stack) =>
653 Effect.gen(function* () {
654 const { accountId } = yield* yield* CloudflareEnvironment;
655 const fs = yield* FileSystem.FileSystem;
656 const path = yield* Path.Path;
657 const migrationsDir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-rollforward-" });
658 yield* fs.writeFileString(
659 path.join(migrationsDir, "0001_users.sql"),
660 "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
661 );
662 yield* fs.writeFileString(
663 path.join(migrationsDir, "0002_posts.sql"),
664 "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);",
665 );
666
667 yield* stack.destroy();
668
669 // Phase 1: deploy so the database, tables, and state exist.
670 const deployed = yield* stack.deploy(
671 Effect.gen(function* () {
672 return yield* Cloudflare.D1.Database("RollForwardDb", { migrations: migrationsDir });
673 }),
674 );
675
676 // Phase 2: rewrite BOTH artifacts to the pre-registry shape.
677 // 2a. The physical table: drop the modern one, recreate the old
678 // 3-column `id TEXT PK` shape under the old default name.
679 yield* execSql(accountId, deployed.databaseId, "DROP TABLE __alchemy_migrations;");
680 yield* execSql(
681 accountId,
682 deployed.databaseId,
683 `CREATE TABLE d1_migrations (
684 id TEXT PRIMARY KEY,
685 name TEXT NOT NULL,
686 applied_at TEXT NOT NULL
687 );`,
688 );
689 yield* execSql(
690 accountId,
691 deployed.databaseId,
692 `INSERT INTO d1_migrations (id, name, applied_at) VALUES
693 ('00001', '0001_users.sql', '2024-01-01 00:00:00'),
694 ('00002', '0002_posts.sql', '2024-01-01 00:01:00');`,
695 );
696 // 2b. The state row: exactly what old code persisted — table name
697 // pinned to d1_migrations (the old D1 default).
698 yield* Effect.gen(function* () {
699 const state = yield* yield* State;
700 const row = yield* state.get({
701 stack: stack.name,
702 stage: stack.stage,
703 fqn: "RollForwardDb",
704 });
705 const attr = { ...(row as any).attr, migrationsTable: "d1_migrations" };
706 yield* state.set({
707 stack: stack.name,
708 stage: stack.stage,
709 fqn: "RollForwardDb",
710 value: { ...(row as any), attr },
711 });
712 }).pipe(Effect.provide(stack.state));
713
714 // Phase 3: add a migration and redeploy on top of the legacy state.
715 yield* fs.writeFileString(
716 path.join(migrationsDir, "0003_comments.sql"),
717 "CREATE TABLE comments (id INTEGER PRIMARY KEY, body TEXT NOT NULL);",
718 );
719 const rolled = yield* stack.deploy(
720 Effect.gen(function* () {
721 return yield* Cloudflare.D1.Database("RollForwardDb", { migrations: migrationsDir });
722 }),
723 );
724 expect(rolled.databaseId).toEqual(deployed.databaseId);
725 // The persisted table name keeps winning for legacy deploys.
726 expect(rolled.migrationsTable).toEqual("d1_migrations");
727
728 const columns = yield* queryAll<{ name: string; type: string }>(
729 accountId,
730 deployed.databaseId,
731 "PRAGMA table_info(d1_migrations);",
732 );
733 // Upgraded in place to Alchemy's shape.
734 expect(columns.map((c) => c.name)).toEqual([
735 "id",
736 "hash",
737 "created_at",
738 "name",
739 "applied_at",
740 ]);
741 expect(columns.find((c) => c.name === "id")?.type).toBe("INTEGER");
742 const rows = yield* queryAll<{ id: number; name: string }>(
743 accountId,
744 deployed.databaseId,
745 "SELECT id, name FROM d1_migrations ORDER BY id;",
746 );
747 expect(rows).toEqual([
748 { id: 1, name: "0001_users.sql" },
749 { id: 2, name: "0002_posts.sql" },
750 { id: 3, name: "0003_comments.sql" },
751 ]);
752 const tables = yield* listTables(accountId, deployed.databaseId);
753 expect(tables).toContain("comments");
754
755 yield* stack.destroy();
756 yield* waitForDatabaseToBeDeleted(deployed.databaseId, accountId);
757 }).pipe(logLevel),
758 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
759 );
760
761 /**
762 * The Seth-shaped roll-forward: a drizzle-LAYOUT directory whose history
763 * was applied by pre-registry Alchemy under flat keys
764 * (`<dir>/migration.sql`) in the legacy table. The persisted table name
765 * must keep winning — starting a second bookkeeping table would replay
766 * history into a live database.
767 */
768 test.provider(
769 "rolls forward legacy state over a drizzle-layout dir without switching tables",
770 (stack) =>
771 Effect.gen(function* () {
772 const { accountId } = yield* yield* CloudflareEnvironment;
773 const fs = yield* FileSystem.FileSystem;
774 const path = yield* Path.Path;
775 const migrationsDir = yield* fs.makeTempDirectory({
776 prefix: "alchemy-d1-rollforward-drizzle-",
777 });
778 yield* fs.makeDirectory(path.join(migrationsDir, "20240101000000_init"));
779 yield* fs.writeFileString(
780 path.join(migrationsDir, "20240101000000_init", "migration.sql"),
781 "CREATE TABLE users (id integer PRIMARY KEY NOT NULL, name text NOT NULL);",
782 );
783
784 yield* stack.destroy();
785
786 // Phase 1: deploy WITHOUT migrations, then hand-write what old
787 // Alchemy left behind: migration 1 really ran, recorded under its
788 // flat path key in the legacy 3-column table.
789 const deployed = yield* stack.deploy(
790 Effect.gen(function* () {
791 return yield* Cloudflare.D1.Database("RollForwardDrizzleDb");
792 }),
793 );
794 yield* execSql(
795 accountId,
796 deployed.databaseId,
797 "CREATE TABLE users (id integer PRIMARY KEY NOT NULL, name text NOT NULL);",
798 );
799 yield* execSql(
800 accountId,
801 deployed.databaseId,
802 `CREATE TABLE d1_migrations (
803 id TEXT PRIMARY KEY,
804 name TEXT NOT NULL,
805 applied_at TEXT NOT NULL
806 );`,
807 );
808 yield* execSql(
809 accountId,
810 deployed.databaseId,
811 `INSERT INTO d1_migrations (id, name, applied_at) VALUES
812 ('00001', '20240101000000_init/migration.sql', '2024-01-01 00:00:00');`,
813 );
814 // Old-style state: dir + table + hashes, no format stamp.
815 const legacyHashes = yield* hashMigrations(migrationsDir);
816 yield* Effect.gen(function* () {
817 const state = yield* yield* State;
818 const row = yield* state.get({
819 stack: stack.name,
820 stage: stack.stage,
821 fqn: "RollForwardDrizzleDb",
822 });
823 const attr = {
824 ...(row as any).attr,
825 migrationsDir,
826 migrationsTable: "d1_migrations",
827 migrationsHashes: legacyHashes,
828 };
829 yield* state.set({
830 stack: stack.name,
831 stage: stack.stage,
832 fqn: "RollForwardDrizzleDb",
833 value: { ...(row as any), attr },
834 });
835 }).pipe(Effect.provide(stack.state));
836
837 // Phase 2: add a second drizzle-layout migration and redeploy WITH
838 // migrations configured.
839 yield* fs.makeDirectory(path.join(migrationsDir, "20240102000000_posts"));
840 yield* fs.writeFileString(
841 path.join(migrationsDir, "20240102000000_posts", "migration.sql"),
842 "CREATE TABLE posts (id integer PRIMARY KEY NOT NULL, title text NOT NULL);",
843 );
844 const rolled = yield* stack.deploy(
845 Effect.gen(function* () {
846 return yield* Cloudflare.D1.Database("RollForwardDrizzleDb", {
847 migrations: migrationsDir,
848 });
849 }),
850 );
851 expect(rolled.databaseId).toEqual(deployed.databaseId);
852 // The persisted table name keeps winning; no second bookkeeping
853 // table appears.
854 expect(rolled.migrationsTable).toEqual("d1_migrations");
855
856 const tables = yield* listTables(accountId, deployed.databaseId);
857 expect(tables).not.toContain("__drizzle_migrations");
858 expect(tables).toContain("posts");
859
860 // Migration 1 was not replayed (bare CREATE TABLE would fail);
861 // migration 2 was recorded under the same flat-path convention.
862 const rows = yield* queryAll<{ name: string }>(
863 accountId,
864 deployed.databaseId,
865 "SELECT name FROM d1_migrations ORDER BY id;",
866 );
867 expect(rows.map((r) => r.name)).toEqual([
868 // The legacy row keeps its flat key; new records use the current
869 // directory-name convention. Aliasing keeps both recognized.
870 "20240101000000_init/migration.sql",
871 "20240102000000_posts",
872 ]);
873
874 yield* stack.destroy();
875 yield* waitForDatabaseToBeDeleted(deployed.databaseId, accountId);
876 }).pipe(logLevel),
877 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
878 );
879
880 /**
881 * State-lost adoption of the OLDEST pre-registry deploys: a 2-column
882 * `d1_migrations` exists but state carries no table name. The legacy table
883 * is treated as a conversion source — history copies into
884 * `__alchemy_migrations` and the old table is frozen, exactly like a
885 * foreign tool's table.
886 */
887 test.provider(
888 "converts a legacy 2-column d1_migrations table into __alchemy_migrations",
889 (stack) =>
890 Effect.gen(function* () {
891 const { accountId } = yield* yield* CloudflareEnvironment;
892 const fs = yield* FileSystem.FileSystem;
893 const path = yield* Path.Path;
894 const migrationsDir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-legacy-" });
895 // History validation is strict (drizzle's own upgrade policy): every
896 // recorded row must match a local file, so the two "previously
897 // applied" migrations exist in the dir. Their bare CREATE TABLEs
898 // would fail if replayed, which is exactly what the upgrade must not
899 // do.
900 yield* fs.writeFileString(
901 path.join(migrationsDir, "0000_initial_setup.sql"),
902 "CREATE TABLE settings (id INTEGER PRIMARY KEY, value TEXT);",
903 );
904 yield* fs.writeFileString(
905 path.join(migrationsDir, "0001_add_indexes.sql"),
906 "CREATE INDEX settings_value_idx ON settings (value);",
907 );
908 yield* fs.writeFileString(
909 path.join(migrationsDir, "0002_create_users.sql"),
910 "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
911 );
912 yield* fs.writeFileString(
913 path.join(migrationsDir, "0003_create_posts.sql"),
914 "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);",
915 );
916
917 yield* stack.destroy();
918
919 // Step 1: deploy the database without migrations so we can seed a legacy
920 // 2-column d1_migrations table on it before re-deploying with a
921 // migrationsDir.
922 const seeded = yield* stack.deploy(
923 Effect.gen(function* () {
924 return yield* Cloudflare.D1.Database("LegacyMigrationDb");
925 }),
926 );
927
928 yield* execSql(
929 accountId,
930 seeded.databaseId,
931 `CREATE TABLE d1_migrations (
932 id TEXT PRIMARY KEY,
933 applied_at TEXT NOT NULL
934 );`,
935 );
936 yield* execSql(
937 accountId,
938 seeded.databaseId,
939 `INSERT INTO d1_migrations (id, applied_at) VALUES
940 ('0000_initial_setup.sql', datetime('now', '-1 day')),
941 ('0001_add_indexes.sql', datetime('now', '-1 hour'));`,
942 );
943
944 // Step 2: deploy again with a migrationsDir. No table name is
945 // persisted in state, so the legacy table becomes a conversion
946 // source: history copies into __alchemy_migrations, only the two
947 // pending migrations run (0000/0001's SQL would fail if replayed
948 // out of order against the frozen history's assumptions).
949 const upgraded = yield* stack.deploy(
950 Effect.gen(function* () {
951 return yield* Cloudflare.D1.Database("LegacyMigrationDb", { migrations: migrationsDir });
952 }),
953 );
954 expect(upgraded.databaseId).toEqual(seeded.databaseId);
955 expect(upgraded.migrationsTable).toEqual("__alchemy_migrations");
956
957 const columns = yield* queryAll<{ name: string; type: string }>(
958 accountId,
959 seeded.databaseId,
960 "PRAGMA table_info(__alchemy_migrations);",
961 );
962 expect(columns.map((c) => c.name)).toEqual([
963 "id",
964 "hash",
965 "created_at",
966 "name",
967 "applied_at",
968 ]);
969
970 const records = yield* queryAll<{ id: number; name: string }>(
971 accountId,
972 seeded.databaseId,
973 "SELECT id, name FROM __alchemy_migrations ORDER BY id;",
974 );
975 expect(records.map((r) => r.name)).toEqual([
976 "0000_initial_setup.sql",
977 "0001_add_indexes.sql",
978 "0002_create_users.sql",
979 "0003_create_posts.sql",
980 ]);
981 // Ids are sequential ordinals preserving apply order.
982 expect(records.map((r) => r.id)).toEqual(records.map((_, i) => i + 1));
983
984 // The legacy table is frozen, never dropped or rewritten.
985 const frozen = yield* queryAll<{ id: string }>(
986 accountId,
987 seeded.databaseId,
988 "SELECT id FROM d1_migrations ORDER BY id;",
989 );
990 expect(frozen.map((r) => r.id)).toEqual(["0000_initial_setup.sql", "0001_add_indexes.sql"]);
991
992 yield* stack.destroy();
993 yield* waitForDatabaseToBeDeleted(seeded.databaseId, accountId);
994 }).pipe(logLevel),
995 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
996 );
997
998 test.provider(
999 "imports SQL files via importFiles",
1000 (stack) =>
1001 Effect.gen(function* () {
1002 const { accountId } = yield* yield* CloudflareEnvironment;
1003 const fs = yield* FileSystem.FileSystem;
1004 const path = yield* Path.Path;
1005 const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-imports-" });
1006 const importPath = path.join(dir, "seed.sql");
1007
1008 yield* fs.writeFileString(
1009 importPath,
1010 [
1011 "CREATE TABLE widgets (id INTEGER PRIMARY KEY, label TEXT NOT NULL);",
1012 "INSERT INTO widgets (id, label) VALUES (1, 'one');",
1013 "INSERT INTO widgets (id, label) VALUES (2, 'two');",
1014 ].join("\n"),
1015 );
1016
1017 yield* stack.destroy();
1018
1019 const database = yield* stack.deploy(
1020 Effect.gen(function* () {
1021 return yield* Cloudflare.D1.Database("ImportDatabase", { importFiles: [importPath] });
1022 }),
1023 );
1024
1025 expect(database.importHashes[importPath]).toBeDefined();
1026
1027 const widgets = yield* getResults<{ id: number; label: string }>(
1028 accountId,
1029 database.databaseId,
1030 "SELECT id, label FROM widgets ORDER BY id;",
1031 );
1032 expect(widgets).toEqual([
1033 { id: 1, label: "one" },
1034 { id: 2, label: "two" },
1035 ]);
1036
1037 yield* stack.destroy();
1038 yield* waitForDatabaseToBeDeleted(database.databaseId, accountId);
1039 }).pipe(logLevel),
1040 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
1041 );
1042
1043 test.provider(
1044 "re-imports SQL that D1 already holds after a seed rollback",
1045 (stack) =>
1046 Effect.gen(function* () {
1047 const { accountId } = yield* yield* CloudflareEnvironment;
1048 const fs = yield* FileSystem.FileSystem;
1049 const path = yield* Path.Path;
1050 const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-reimport-" });
1051 const seedPath = path.join(dir, "seed.sql");
1052 const seed = (mode: string) =>
1053 fs.writeFileString(
1054 seedPath,
1055 [
1056 "CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT NOT NULL);",
1057 `INSERT OR REPLACE INTO settings (key, value) VALUES ('mode', '${mode}');`,
1058 ].join("\n"),
1059 );
1060 const deploy = stack.deploy(
1061 Effect.gen(function* () {
1062 return yield* Cloudflare.D1.Database("ReimportDatabase", { importFiles: [seedPath] });
1063 }),
1064 );
1065 const mode = (databaseId: string) =>
1066 queryAll<{ value: string }>(
1067 accountId,
1068 databaseId,
1069 "SELECT value FROM settings WHERE key = 'mode';",
1070 );
1071
1072 yield* stack.destroy();
1073
1074 yield* seed("v1");
1075 const v1 = yield* deploy;
1076 expect(yield* mode(v1.databaseId)).toEqual([{ value: "v1" }]);
1077
1078 yield* seed("v2");
1079 const v2 = yield* deploy;
1080 expect(v2.databaseId).toEqual(v1.databaseId);
1081 expect(yield* mode(v2.databaseId)).toEqual([{ value: "v2" }]);
1082
1083 // Rolling back re-imports SQL whose etag D1 already holds: `init`
1084 // returns no upload URL and the import has already completed.
1085 yield* seed("v1");
1086 const rolledBack = yield* deploy;
1087 expect(rolledBack.databaseId).toEqual(v1.databaseId);
1088 expect(rolledBack.importHashes[seedPath]).toEqual(v1.importHashes[seedPath]);
1089 expect(yield* mode(rolledBack.databaseId)).toEqual([{ value: "v1" }]);
1090
1091 yield* stack.destroy();
1092 yield* waitForDatabaseToBeDeleted(v1.databaseId, accountId);
1093 }).pipe(logLevel),
1094 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
1095 );
1096
1097 test.provider(
1098 "clones a database by databaseId",
1099 (stack) =>
1100 Effect.gen(function* () {
1101 const { accountId } = yield* yield* CloudflareEnvironment;
1102 const fs = yield* FileSystem.FileSystem;
1103 const path = yield* Path.Path;
1104 const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-clone-id-" });
1105 const seedPath = path.join(dir, "seed.sql");
1106
1107 yield* fs.writeFileString(
1108 seedPath,
1109 [
1110 "CREATE TABLE colors (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
1111 "INSERT INTO colors (id, name) VALUES (1, 'red'), (2, 'green'), (3, 'blue');",
1112 ].join("\n"),
1113 );
1114
1115 yield* stack.destroy();
1116
1117 const { source, target } = yield* stack.deploy(
1118 Effect.gen(function* () {
1119 const source = yield* Cloudflare.D1.Database("CloneByIdSource", {
1120 importFiles: [seedPath],
1121 });
1122 const target = yield* Cloudflare.D1.Database("CloneByIdTarget", {
1123 clone: { databaseId: source.databaseId },
1124 });
1125 return { source, target };
1126 }),
1127 );
1128
1129 expect(target.databaseId).not.toEqual(source.databaseId);
1130
1131 const targetColors = yield* getResults<{ id: number; name: string }>(
1132 accountId,
1133 target.databaseId,
1134 "SELECT id, name FROM colors ORDER BY id;",
1135 );
1136 expect(targetColors).toEqual([
1137 { id: 1, name: "red" },
1138 { id: 2, name: "green" },
1139 { id: 3, name: "blue" },
1140 ]);
1141
1142 yield* stack.destroy();
1143 yield* waitForDatabaseToBeDeleted(source.databaseId, accountId);
1144 yield* waitForDatabaseToBeDeleted(target.databaseId, accountId);
1145 }).pipe(logLevel),
1146 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
1147 );
1148
1149 test.provider(
1150 "clones a database by name lookup",
1151 (stack) =>
1152 Effect.gen(function* () {
1153 const { accountId } = yield* yield* CloudflareEnvironment;
1154 const fs = yield* FileSystem.FileSystem;
1155 const path = yield* Path.Path;
1156 const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-clone-name-" });
1157 const seedPath = path.join(dir, "seed.sql");
1158
1159 yield* fs.writeFileString(
1160 seedPath,
1161 [
1162 "CREATE TABLE animals (id INTEGER PRIMARY KEY, kind TEXT NOT NULL);",
1163 "INSERT INTO animals (id, kind) VALUES (1, 'cat'), (2, 'dog');",
1164 ].join("\n"),
1165 );
1166
1167 yield* stack.destroy();
1168
1169 const { source, target } = yield* stack.deploy(
1170 Effect.gen(function* () {
1171 const source = yield* Cloudflare.D1.Database("CloneByNameSource", {
1172 importFiles: [seedPath],
1173 });
1174 const target = yield* Cloudflare.D1.Database("CloneByNameTarget", {
1175 clone: { name: source.databaseName },
1176 });
1177 return { source, target };
1178 }),
1179 );
1180
1181 expect(target.databaseId).not.toEqual(source.databaseId);
1182
1183 const animals = yield* getResults<{ id: number; kind: string }>(
1184 accountId,
1185 target.databaseId,
1186 "SELECT id, kind FROM animals ORDER BY id;",
1187 );
1188 expect(animals).toEqual([
1189 { id: 1, kind: "cat" },
1190 { id: 2, kind: "dog" },
1191 ]);
1192
1193 yield* stack.destroy();
1194 yield* waitForDatabaseToBeDeleted(source.databaseId, accountId);
1195 yield* waitForDatabaseToBeDeleted(target.databaseId, accountId);
1196 }).pipe(logLevel),
1197 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
1198 );
1199
1200 test.provider(
1201 "clones a database by passing the source resource directly",
1202 (stack) =>
1203 Effect.gen(function* () {
1204 const { accountId } = yield* yield* CloudflareEnvironment;
1205 const fs = yield* FileSystem.FileSystem;
1206 const path = yield* Path.Path;
1207 const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-d1-clone-direct-" });
1208 const seedPath = path.join(dir, "seed.sql");
1209
1210 yield* fs.writeFileString(
1211 seedPath,
1212 [
1213 "CREATE TABLE shapes (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
1214 "INSERT INTO shapes (id, name) VALUES (1, 'square'), (2, 'circle');",
1215 ].join("\n"),
1216 );
1217
1218 yield* stack.destroy();
1219
1220 const { source, target } = yield* stack.deploy(
1221 Effect.gen(function* () {
1222 const source = yield* Cloudflare.D1.Database("CloneDirectSource", {
1223 importFiles: [seedPath],
1224 });
1225 const target = yield* Cloudflare.D1.Database("CloneDirectTarget", { clone: source });
1226 return { source, target };
1227 }),
1228 );
1229
1230 expect(target.databaseId).not.toEqual(source.databaseId);
1231
1232 const shapes = yield* getResults<{ id: number; name: string }>(
1233 accountId,
1234 target.databaseId,
1235 "SELECT id, name FROM shapes ORDER BY id;",
1236 );
1237 expect(shapes).toEqual([
1238 { id: 1, name: "square" },
1239 { id: 2, name: "circle" },
1240 ]);
1241
1242 yield* stack.destroy();
1243 yield* waitForDatabaseToBeDeleted(source.databaseId, accountId);
1244 yield* waitForDatabaseToBeDeleted(target.databaseId, accountId);
1245 }).pipe(logLevel),
1246 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
1247 );
1248
1249 const queryAll = Effect.fn(function* <T>(accountId: string, databaseId: string, sql: string) {
1250 const queryDb = yield* d1.queryDatabase;
1251 const result = yield* queryDb({ accountId, databaseId, sql });
1252 return (result.result[0]?.results ?? []) as T[];
1253 });
1254
1255 const execSql = (accountId: string, databaseId: string, sql: string) =>
1256 queryAll<unknown>(accountId, databaseId, sql);
1257
1258 const listTables = Effect.fn(function* (accountId: string, databaseId: string) {
1259 const rows = yield* queryAll<{ name: string }>(
1260 accountId,
1261 databaseId,
1262 "SELECT name FROM sqlite_master WHERE type='table';",
1263 );
1264 return rows.map((r) => r.name);
1265 });
1266
1267 /**
1268 * D1 query results are eventually consistent following an import/clone, so
1269 * retry until we see at least one row (matches v1's `getResults` helper).
1270 */
1271 const getResults = Effect.fn(function* <T>(accountId: string, databaseId: string, sql: string) {
1272 return yield* queryAll<T>(accountId, databaseId, sql).pipe(
1273 Effect.flatMap((rows) =>
1274 rows.length > 0 ? Effect.succeed(rows) : Effect.fail(new EmptyResults({ sql })),
1275 ),
1276 Effect.retry({
1277 while: (e) => e instanceof EmptyResults,
1278 schedule: Schedule.max([Schedule.spaced(Duration.seconds(1)), Schedule.recurs(10)]),
1279 }),
1280 Effect.orDie,
1281 );
1282 });
1283
1284 // Engine-level adoption: D1 databases have no ownership signal (Cloudflare
1285 // doesn't expose tags on D1), so a name match in `read` is treated as silent
1286 // adoption.
1287 test.provider(
1288 "existing database (matching name) is silently adopted without --adopt",
1289 (stack) =>
1290 Effect.gen(function* () {
1291 const { accountId } = yield* yield* CloudflareEnvironment;
1292
1293 yield* stack.destroy();
1294
1295 // Phase 1: deploy normally so a real D1 database exists. No explicit
1296 // `name` — the engine generates a random-suffixed physical name
1297 // (collision-free across concurrent runs); the deploy output hands
1298 // back the real name, which pins the database's identity for the
1299 // adoption phase below.
1300 const initial = yield* stack.deploy(
1301 Effect.gen(function* () {
1302 return yield* Cloudflare.D1.Database("AdoptableDatabase");
1303 }),
1304 );
1305 const databaseName = initial.databaseName;
1306 const initialId = initial.databaseId;
1307
1308 // Phase 2: wipe local state — the database stays on Cloudflare.
1309 yield* Effect.gen(function* () {
1310 const state = yield* yield* State;
1311 yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "AdoptableDatabase" });
1312 }).pipe(Effect.provide(stack.state));
1313
1314 // Phase 3: redeploy without `adopt(true)`. The engine calls
1315 // `provider.read`, which lists databases by name and returns plain
1316 // attrs — silent adoption.
1317 const adopted = yield* stack.deploy(
1318 Effect.gen(function* () {
1319 return yield* Cloudflare.D1.Database("AdoptableDatabase", { name: databaseName });
1320 }),
1321 );
1322
1323 // Same physical database — adoption, not re-creation.
1324 expect(adopted.databaseId).toEqual(initialId);
1325 expect(adopted.databaseName).toEqual(databaseName);
1326
1327 const persisted = yield* Effect.gen(function* () {
1328 const state = yield* yield* State;
1329 return yield* state.get({
1330 stack: stack.name,
1331 stage: stack.stage,
1332 fqn: "AdoptableDatabase",
1333 });
1334 }).pipe(Effect.provide(stack.state));
1335
1336 expect((persisted as any)?.attr).toMatchObject({ databaseId: initialId, databaseName });
1337
1338 yield* stack.destroy();
1339 yield* waitForDatabaseToBeDeleted(initialId, accountId);
1340 }).pipe(logLevel),
1341 { tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"] },
1342 );
1343
1344 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
1345 status: number;
1346 body: string;
1347 }> {}
1348
1349 /** POST to the fixture until it answers 200 (rides out workers.dev cold starts). */
1350 const fetchOk = (request: HttpClientRequest.HttpClientRequest) =>
1351 HttpClient.execute(request).pipe(
1352 Effect.flatMap((res) =>
1353 res.text.pipe(
1354 Effect.flatMap((body) =>
1355 res.status === 200
1356 ? Effect.succeed(body)
1357 : Effect.fail(new WorkerNotReady({ status: res.status, body })),
1358 ),
1359 ),
1360 ),
1361 Effect.retry({
1362 while: (e) => e._tag === "WorkerNotReady",
1363 schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(30)]),
1364 }),
1365 );
1366
1367 const writeEntry = (url: string) => fetchOk(HttpClientRequest.post(`${url}/write`));
1368
1369 const readEntries = (url: string) =>
1370 fetchOk(HttpClientRequest.get(`${url}/read`)).pipe(
1371 Effect.map((body) => (JSON.parse(body) as { rows: number }).rows),
1372 );
1373
1374 const waitForDatabaseToBeDeleted = Effect.fn(function* (databaseId: string, accountId: string) {
1375 yield* d1.getDatabase({ accountId, databaseId }).pipe(
1376 Effect.flatMap(() => Effect.fail(new DatabaseStillExists())),
1377 Effect.retry({
1378 while: (e): e is DatabaseStillExists => e instanceof DatabaseStillExists,
1379 schedule: Schedule.exponential(100),
1380 }),
1381 Effect.catchTag("DatabaseNotFound", () => Effect.void),
1382 );
1383 });
1384
1385 class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {}
1386 class EmptyResults extends Data.TaggedError("EmptyResults")<{ sql: string }> {}