The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { requestWorker } from "../Utils/WorkerRequest.ts"; |
| 12 | import UpgradeWorker from "./fixtures/sql-migrations-upgrade/worker.ts"; |
| 13 | |
| 14 | type State = { id: string; count: number; rows: { value: string }[] }; |
| 15 | |
| 16 | for (const dev of [true, false]) { |
| 17 | describe( |
| 18 | dev ? "local SQL migration updates" : "live SQL migration updates", |
| 19 | { tags: ["provider:cloudflare", "provider:cloudflare:worker"] }, |
| 20 | () => { |
| 21 | const { test } = Test.make({ |
| 22 | providers: Cloudflare.providers(), |
| 23 | dev, |
| 24 | stage: dev ? "sql-migrations-upgrade-local" : "sql-migrations-upgrade-live", |
| 25 | }); |
| 26 | test.provider( |
| 27 | "SQL-only changes update existing objects without replay or replacement", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | yield* stack.destroy(); |
| 31 | const fs = yield* FileSystem.FileSystem; |
| 32 | const path = yield* Path.Path; |
| 33 | const dir = yield* fs.makeTempDirectoryScoped(); |
| 34 | yield* fs.writeFileString( |
| 35 | path.join(dir, "0001_init.sql"), |
| 36 | "CREATE TABLE items (value TEXT NOT NULL); INSERT INTO items VALUES ('seed');", |
| 37 | ); |
| 38 | const config = ConfigProvider.orElse( |
| 39 | ConfigProvider.fromUnknown({ SQL_MIGRATIONS_DIRECTORY: dir }), |
| 40 | yield* ConfigProvider.ConfigProvider, |
| 41 | ); |
| 42 | const deploy = stack |
| 43 | .deploy( |
| 44 | Effect.gen(function* () { |
| 45 | const worker = yield* UpgradeWorker; |
| 46 | return { |
| 47 | url: worker.url.as<string>(), |
| 48 | namespaces: worker.durableObjectNamespaces, |
| 49 | }; |
| 50 | }), |
| 51 | ) |
| 52 | .pipe(Effect.provideService(ConfigProvider.ConfigProvider, config)); |
| 53 | const first = yield* deploy; |
| 54 | // Fresh and updated workers.dev hosts can answer Cloudflare's 404 |
| 55 | // placeholder before the script reaches them; requestWorker retries |
| 56 | // only that pre-invocation placeholder. |
| 57 | const read = (url: string) => |
| 58 | requestWorker(HttpClientRequest.get(url)).pipe( |
| 59 | Effect.flatMap((response) => |
| 60 | Effect.gen(function* () { |
| 61 | if (response.status !== 200) |
| 62 | return yield* Effect.fail( |
| 63 | new Error(`GET ${url}: ${response.status}: ${yield* response.text}`), |
| 64 | ); |
| 65 | return yield* response.json; |
| 66 | }), |
| 67 | ), |
| 68 | Effect.map((value) => value as State), |
| 69 | Effect.timeout("5 seconds"), |
| 70 | ); |
| 71 | // A redeploy reaches a busy live Durable Object only once it goes |
| 72 | // idle: Worker `exports` deploys carry no migration, so Cloudflare |
| 73 | // keeps an activation that receives a request every few seconds on |
| 74 | // the previous version indefinitely. Leave an idle gap between |
| 75 | // probes so the object restarts on the new code. |
| 76 | const probe = { |
| 77 | schedule: Schedule.spaced(dev ? "3 seconds" : "15 seconds"), |
| 78 | times: dev ? 10 : 5, |
| 79 | }; |
| 80 | const ready = (url: string, count: number) => |
| 81 | read(url).pipe( |
| 82 | Effect.filterOrFail( |
| 83 | (state) => state.count === count, |
| 84 | () => new Error("Migration version not ready"), |
| 85 | ), |
| 86 | Effect.retry(probe), |
| 87 | Effect.timeout("90 seconds"), |
| 88 | ); |
| 89 | const original = yield* ready(first.url, 1); |
| 90 | expect(original.rows).toEqual([{ value: "seed" }]); |
| 91 | yield* requestWorker(HttpClientRequest.post(first.url)).pipe( |
| 92 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 93 | ); |
| 94 | yield* fs.writeFileString( |
| 95 | path.join(dir, "0002_append.sql"), |
| 96 | "INSERT INTO items VALUES ('must-rollback'); INSERT INTO missing_table VALUES (1);", |
| 97 | ); |
| 98 | const broken = yield* deploy; |
| 99 | const failed = yield* requestWorker(HttpClientRequest.get(broken.url)).pipe( |
| 100 | Effect.flatMap((response) => |
| 101 | response.text.pipe(Effect.map((body) => ({ status: response.status, body }))), |
| 102 | ), |
| 103 | Effect.timeout("5 seconds"), |
| 104 | Effect.repeat({ |
| 105 | until: (response) => |
| 106 | response.status === 500 && |
| 107 | response.body.includes("Failed to apply SQL migration"), |
| 108 | ...probe, |
| 109 | }), |
| 110 | Effect.timeout("90 seconds"), |
| 111 | ); |
| 112 | expect(failed.status).toBe(500); |
| 113 | expect(failed.body).toContain("Failed to apply SQL migration"); |
| 114 | yield* fs.writeFileString( |
| 115 | path.join(dir, "0002_append.sql"), |
| 116 | "INSERT INTO items VALUES ('migration-two');", |
| 117 | ); |
| 118 | const second = yield* deploy; |
| 119 | expect(second.namespaces).toEqual(first.namespaces); |
| 120 | const upgraded = yield* ready(second.url, 2); |
| 121 | expect(upgraded.id).toBe(original.id); |
| 122 | expect(upgraded.rows).toEqual([ |
| 123 | { value: "seed" }, |
| 124 | { value: "user-data" }, |
| 125 | { value: "migration-two" }, |
| 126 | ]); |
| 127 | const third = yield* deploy; |
| 128 | // Worker and Durable Object versions can propagate independently. |
| 129 | expect(yield* ready(third.url, 2)).toEqual(upgraded); |
| 130 | yield* stack.destroy(); |
| 131 | }), |
| 132 | { tags: [...(dev ? ["local"] : ["live"])], timeout: 240_000 }, |
| 133 | ); |
| 134 | }, |
| 135 | ); |
| 136 | } |