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, 04:45 UTC
| 1 | import type { RuntimeServices } from "@alchemy.run/cloudflare-runtime/core"; |
| 2 | import { |
| 3 | diffMigrations, |
| 4 | migrationsAttrs, |
| 5 | migrationsInputOf, |
| 6 | runMigrations, |
| 7 | stampedOf, |
| 8 | type MigrationsInput, |
| 9 | } from "@alchemy.run/sql/Migrations"; |
| 10 | import { applySqlImports, hashImports, splitSqliteStatements } from "@alchemy.run/sql/SqlFile"; |
| 11 | import * as d1 from "@distilled.cloud/cloudflare/d1"; |
| 12 | import { isResolved } from "alchemy/Diff"; |
| 13 | import * as ProviderLayer from "alchemy/Local/ProviderLayer"; |
| 14 | import * as RpcProvider from "alchemy/Local/RpcProvider"; |
| 15 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 16 | import * as Provider from "alchemy/Provider"; |
| 17 | import { isResourceOfType, Resource } from "alchemy/Resource"; |
| 18 | import { recordsEqual } from "alchemy/Util/equal"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as HttpClient from "effect/http/HttpClient"; |
| 21 | import * as Layer from "effect/Layer"; |
| 22 | import * as Option from "effect/Option"; |
| 23 | import * as Stream from "effect/Stream"; |
| 24 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 25 | import { localAccountId } from "../LocalAccount.ts"; |
| 26 | import { generateLocalId, LOCAL_PROVIDERS_URL, localRuntimeServices } from "../LocalRuntime.ts"; |
| 27 | import type { Providers } from "../Providers.ts"; |
| 28 | import { makeD1MigrationExecutor } from "./ApplyMigrations.ts"; |
| 29 | import { cloneDatabase } from "./CloneDatabase.ts"; |
| 30 | import { importD1Database } from "./ImportDatabase.ts"; |
| 31 | import { withLocalD1Executor, withLocalD1Query } from "./LocalD1Gateway.ts"; |
| 32 | |
| 33 | export const isDatabase = (value: unknown): value is Database => |
| 34 | isResourceOfType(value, "Cloudflare.D1Database"); |
| 35 | |
| 36 | export type Jurisdiction = "default" | "eu" | "fedramp"; |
| 37 | export type PrimaryLocationHint = "wnam" | "enam" | "weur" | "eeur" | "apac" | "oc"; |
| 38 | |
| 39 | export type CloneSource = Database | { databaseId: string } | { name: string }; |
| 40 | |
| 41 | export type DatabaseProps = { |
| 42 | /** |
| 43 | * Name of the database. If omitted, a unique name will be generated. |
| 44 | * |
| 45 | * D1 names are unique per account. When an explicit name is kept across a |
| 46 | * replacement (e.g. `primaryLocationHint` or `jurisdiction` changes), the |
| 47 | * existing database and its data are deleted before the new one is created. |
| 48 | * Generated names get a new name on replacement, so the new database is |
| 49 | * created first. |
| 50 | * |
| 51 | * @default ${app}-${stage}-${id} |
| 52 | */ |
| 53 | name?: string; |
| 54 | /** |
| 55 | * Region in which the primary copy of the data is stored. Cannot be |
| 56 | * changed after creation — updating this property triggers a replacement. |
| 57 | * |
| 58 | * - `wnam` — Western North America |
| 59 | * - `enam` — Eastern North America |
| 60 | * - `weur` — Western Europe |
| 61 | * - `eeur` — Eastern Europe |
| 62 | * - `apac` — Asia Pacific |
| 63 | * - `oc` — Oceania |
| 64 | */ |
| 65 | primaryLocationHint?: PrimaryLocationHint; |
| 66 | /** |
| 67 | * Read replication configuration. The only mutable property after |
| 68 | * creation; toggling `mode` triggers an in-place update. |
| 69 | * |
| 70 | * @default { mode: "disabled" } |
| 71 | */ |
| 72 | readReplication?: { mode: "auto" | "disabled" }; |
| 73 | /** |
| 74 | * Jurisdiction in which the database data is guaranteed to be stored. |
| 75 | * Cannot be changed after creation. |
| 76 | * |
| 77 | * @default "default" |
| 78 | */ |
| 79 | jurisdiction?: Jurisdiction; |
| 80 | /** |
| 81 | * SQL migrations to apply on deploy. Accepts a directory path, a |
| 82 | * `Drizzle.Schema` resource, or a `{ dir, table? }` object. |
| 83 | * |
| 84 | * Bookkeeping always lives in Alchemy's `__alchemy_migrations` table. A |
| 85 | * database previously migrated with `drizzle-kit migrate` or |
| 86 | * `wrangler d1 migrations apply` is adopted by a ONE-WAY conversion on |
| 87 | * first deploy: the old tool's applied history is copied into Alchemy's |
| 88 | * table (validated against the local files) and the old table is left |
| 89 | * frozen — never written, never dropped. From then on Alchemy owns the |
| 90 | * migration state. |
| 91 | * |
| 92 | * Pending migrations are detected on each deploy and applied in order as |
| 93 | * part of `update`. |
| 94 | */ |
| 95 | migrations?: MigrationsInput; |
| 96 | /** |
| 97 | * Paths to additional `.sql` files to import after migrations are |
| 98 | * applied. Each file is uploaded via Cloudflare's D1 import API and |
| 99 | * hashed; only files whose contents change are re-imported on subsequent |
| 100 | * deploys. |
| 101 | * |
| 102 | * @see https://developers.cloudflare.com/d1/best-practices/import-export-data/ |
| 103 | */ |
| 104 | importFiles?: string[]; |
| 105 | /** |
| 106 | * Clone data from an existing database during creation by exporting the |
| 107 | * source and importing it into the new database. Only applied during the |
| 108 | * `create` phase. |
| 109 | * |
| 110 | * Accepts: |
| 111 | * - another `D1Database` resource (uses its `databaseId`) |
| 112 | * - `{ databaseId }` — clone by explicit UUID |
| 113 | * - `{ name }` — look up the source by name and clone it |
| 114 | */ |
| 115 | clone?: CloneSource; |
| 116 | }; |
| 117 | |
| 118 | export type Database = Resource< |
| 119 | "Cloudflare.D1Database", |
| 120 | DatabaseProps, |
| 121 | { |
| 122 | databaseId: string; |
| 123 | databaseName: string; |
| 124 | jurisdiction: Jurisdiction; |
| 125 | readReplication: { mode: "auto" | "disabled" } | undefined; |
| 126 | accountId: string; |
| 127 | migrationsDir: string | undefined; |
| 128 | migrationsTable: string | undefined; |
| 129 | migrationsHashes: Record<string, string>; |
| 130 | importHashes: Record<string, string>; |
| 131 | }, |
| 132 | never, |
| 133 | Providers |
| 134 | >; |
| 135 | |
| 136 | /** |
| 137 | * A Cloudflare D1 serverless SQL database built on SQLite. |
| 138 | * |
| 139 | * D1 is a serverless relational database that runs at the edge. Create a |
| 140 | * database as a resource, then bind it to a Worker to run SQL queries. |
| 141 | * ### Creating a Database |
| 142 | * **Example:** Basic database |
| 143 | * ```typescript |
| 144 | * const db = yield* Cloudflare.D1.Database("my-db"); |
| 145 | * ``` |
| 146 | * |
| 147 | * **Example:** Database with location hint |
| 148 | * The primary copy of the data is stored in the chosen region; reads can be |
| 149 | * served closer to users when read replication is enabled. |
| 150 | * ```typescript |
| 151 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 152 | * primaryLocationHint: "wnam", |
| 153 | * }); |
| 154 | * ``` |
| 155 | * |
| 156 | * **Example:** Database with read replication |
| 157 | * Read replication is the only mutable property after creation — toggling it |
| 158 | * triggers an update rather than a replacement. |
| 159 | * ```typescript |
| 160 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 161 | * readReplication: { mode: "auto" }, |
| 162 | * }); |
| 163 | * ``` |
| 164 | * |
| 165 | * **Example:** Database in a specific jurisdiction |
| 166 | * ```typescript |
| 167 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 168 | * jurisdiction: "eu", |
| 169 | * }); |
| 170 | * ``` |
| 171 | * |
| 172 | * ### Migrations |
| 173 | * Point `migrations` at a folder of migration files. Already-applied |
| 174 | * migrations are skipped on subsequent deploys; new files are detected |
| 175 | * automatically and applied as part of the next update. |
| 176 | * |
| 177 | * Bookkeeping always lives in Alchemy's `__alchemy_migrations` table — |
| 178 | * one format, owned by Alchemy. A database previously migrated with |
| 179 | * drizzle-kit, Prisma, or wrangler is adopted by a one-way conversion on |
| 180 | * first deploy: the old tool's applied history is copied into Alchemy's |
| 181 | * table and the old table is left frozen (never written, never dropped). |
| 182 | * No baselining required. Legacy Alchemy tracking tables are detected by |
| 183 | * column shape and upgraded in place. |
| 184 | * |
| 185 | * **Example:** Apply migrations from a directory |
| 186 | * ```typescript |
| 187 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 188 | * migrations: "./migrations", |
| 189 | * }); |
| 190 | * ``` |
| 191 | * |
| 192 | * **Example:** Drizzle migrations (adopts an existing drizzle-kit-migrated database) |
| 193 | * ```typescript |
| 194 | * const schema = yield* Drizzle.Schema("app-schema", { |
| 195 | * schema: "./src/schema.ts", |
| 196 | * dialect: "sqlite", |
| 197 | * }); |
| 198 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 199 | * migrations: schema, |
| 200 | * }); |
| 201 | * ``` |
| 202 | * |
| 203 | * **Example:** Custom bookkeeping table name |
| 204 | * ```typescript |
| 205 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 206 | * migrations: { dir: "./migrations", table: "my_migrations" }, |
| 207 | * }); |
| 208 | * ``` |
| 209 | * |
| 210 | * ### Importing SQL |
| 211 | * Use `importFiles` to seed the database with raw `.sql` files via Cloudflare's |
| 212 | * D1 import API. Each file is hashed; only files whose contents change are |
| 213 | * re-imported on subsequent deploys. |
| 214 | * |
| 215 | * **Example:** Seed a database with SQL files |
| 216 | * ```typescript |
| 217 | * const db = yield* Cloudflare.D1.Database("my-db", { |
| 218 | * importFiles: ["./seed/users.sql", "./seed/posts.sql"], |
| 219 | * }); |
| 220 | * ``` |
| 221 | * |
| 222 | * ### Cloning a Database |
| 223 | * `clone` performs a full export → import from a source database during |
| 224 | * creation. It accepts a `D1Database` resource, a `{ databaseId }`, or a |
| 225 | * `{ name }` to look up by name. |
| 226 | * |
| 227 | * **Example:** Clone by passing the source resource directly |
| 228 | * ```typescript |
| 229 | * const source = yield* Cloudflare.D1.Database("source-db"); |
| 230 | * const cloned = yield* Cloudflare.D1.Database("cloned-db", { |
| 231 | * clone: source, |
| 232 | * }); |
| 233 | * ``` |
| 234 | * |
| 235 | * **Example:** Clone by databaseId |
| 236 | * ```typescript |
| 237 | * const cloned = yield* Cloudflare.D1.Database("cloned-db", { |
| 238 | * clone: { databaseId: "abcdef12-3456-7890-abcd-ef1234567890" }, |
| 239 | * }); |
| 240 | * ``` |
| 241 | * |
| 242 | * **Example:** Clone by name |
| 243 | * ```typescript |
| 244 | * const cloned = yield* Cloudflare.D1.Database("cloned-db", { |
| 245 | * clone: { name: "source-db" }, |
| 246 | * }); |
| 247 | * ``` |
| 248 | * |
| 249 | * ### Binding to a Worker |
| 250 | * **Example:** Using D1 inside a Worker |
| 251 | * ```typescript |
| 252 | * const db = yield* Cloudflare.D1.QueryDatabase(MyDatabase); |
| 253 | * |
| 254 | * // Run a query |
| 255 | * const results = yield* db.prepare("SELECT * FROM users WHERE id = ?") |
| 256 | * .bind(userId) |
| 257 | * .all(); |
| 258 | * |
| 259 | * // Execute a mutation |
| 260 | * yield* db.prepare("INSERT INTO users (id, name) VALUES (?, ?)") |
| 261 | * .bind(newId, name) |
| 262 | * .run(); |
| 263 | * ``` |
| 264 | * |
| 265 | * @see https://developers.cloudflare.com/d1/ |
| 266 | * |
| 267 | * @resource |
| 268 | * @product D1 |
| 269 | * @category Storage & Databases |
| 270 | */ |
| 271 | export const Database = Resource<Database>("Cloudflare.D1Database"); |
| 272 | |
| 273 | /** |
| 274 | * Keep the persisted replication shape when the live mode matches. An omitted |
| 275 | * persisted configuration means disabled, as it does during reconcile. |
| 276 | */ |
| 277 | const resolveObservedD1ReadReplication = ( |
| 278 | observed: Database["Attributes"]["readReplication"], |
| 279 | persisted: Database["Attributes"]["readReplication"], |
| 280 | ): Database["Attributes"]["readReplication"] => |
| 281 | (observed?.mode ?? "disabled") === (persisted?.mode ?? "disabled") ? persisted : observed; |
| 282 | |
| 283 | export const ProviderLive = () => |
| 284 | Provider.succeed(Database, { |
| 285 | stables: ["databaseId", "accountId"], |
| 286 | diff: Effect.fn(function* ({ id, olds = {}, news = {}, output }) { |
| 287 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 288 | if (!isResolved(news)) return undefined; |
| 289 | if ((output?.accountId ?? accountId) !== accountId) { |
| 290 | return { action: "replace" } as const; |
| 291 | } |
| 292 | const oldName = output?.databaseName ?? (yield* createDatabaseName(id, olds.name)); |
| 293 | // Auto-generated names are engine-owned: the deployed name stays |
| 294 | // authoritative even if the generator would name this id differently |
| 295 | // today. Only an explicit user-provided name can force a replace. |
| 296 | const name = news.name ?? oldName; |
| 297 | const oldJurisdiction = output?.jurisdiction ?? olds.jurisdiction ?? "default"; |
| 298 | if ( |
| 299 | oldName !== name || |
| 300 | oldJurisdiction !== (news.jurisdiction ?? "default") || |
| 301 | (olds.primaryLocationHint !== news.primaryLocationHint && |
| 302 | news.primaryLocationHint !== undefined) |
| 303 | ) { |
| 304 | // D1 names are unique per account, so a replacement that keeps an |
| 305 | // explicit name cannot coexist with its predecessor: create-first |
| 306 | // would adopt the existing database by name and garbage collection |
| 307 | // would then delete it. Delete the old generation first instead. |
| 308 | // Generated names change with the new instance id, so they stay |
| 309 | // create-first. |
| 310 | return news.name !== undefined && news.name === oldName |
| 311 | ? ({ action: "replace", deleteFirst: true } as const) |
| 312 | : ({ action: "replace" } as const); |
| 313 | } |
| 314 | const oldReplicationMode = |
| 315 | output?.readReplication?.mode ?? olds.readReplication?.mode ?? "disabled"; |
| 316 | const newReplicationMode = news.readReplication?.mode ?? "disabled"; |
| 317 | if (oldReplicationMode !== newReplicationMode) { |
| 318 | return { action: "update" } as const; |
| 319 | } |
| 320 | // Detect migration/import file drift. |
| 321 | if (yield* diffMigrations({ news, output })) { |
| 322 | return { action: "update" } as const; |
| 323 | } |
| 324 | if (news.importFiles?.length) { |
| 325 | const newHashes = yield* hashImports(news.importFiles, yield* rootDir); |
| 326 | const oldHashes = output?.importHashes ?? {}; |
| 327 | if (!recordsEqual(newHashes, oldHashes)) { |
| 328 | return { action: "update" } as const; |
| 329 | } |
| 330 | } |
| 331 | return undefined; |
| 332 | }), |
| 333 | // Account-scoped collection — D1 databases live under an account and the |
| 334 | // distilled `listDatabases` op paginates them. Hydrate each row into the |
| 335 | // same Attributes shape `read`'s name-lookup branch returns. |
| 336 | list: Effect.fn(function* () { |
| 337 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 338 | return yield* d1.listDatabases.pages({ accountId }).pipe( |
| 339 | Stream.runCollect, |
| 340 | Effect.map((chunk) => |
| 341 | Array.from(chunk).flatMap((page) => |
| 342 | (page.result ?? []) |
| 343 | .filter( |
| 344 | (db): db is (typeof page.result)[number] & { uuid: string } => db.uuid != null, |
| 345 | ) |
| 346 | .map((db) => ({ |
| 347 | databaseId: db.uuid, |
| 348 | databaseName: db.name ?? db.uuid, |
| 349 | jurisdiction: (db.jurisdiction ?? "default") as Jurisdiction, |
| 350 | readReplication: undefined, |
| 351 | accountId, |
| 352 | migrationsDir: undefined, |
| 353 | migrationsTable: undefined, |
| 354 | migrationsHashes: {}, |
| 355 | importHashes: {}, |
| 356 | })), |
| 357 | ), |
| 358 | ), |
| 359 | ); |
| 360 | }), |
| 361 | read: Effect.fn(function* ({ id, output, olds }) { |
| 362 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 363 | if (output?.databaseId) { |
| 364 | return yield* d1 |
| 365 | .getDatabase({ accountId: output.accountId, databaseId: output.databaseId }) |
| 366 | .pipe( |
| 367 | Effect.map((db) => ({ |
| 368 | databaseId: db.uuid ?? output.databaseId, |
| 369 | databaseName: db.name ?? output.databaseName, |
| 370 | jurisdiction: output.jurisdiction, |
| 371 | // Distilled widened generated string enums to open unions. |
| 372 | readReplication: resolveObservedD1ReadReplication( |
| 373 | (db.readReplication ?? undefined) as { mode: "auto" | "disabled" } | undefined, |
| 374 | output.readReplication, |
| 375 | ), |
| 376 | accountId: output.accountId, |
| 377 | migrationsDir: output.migrationsDir, |
| 378 | migrationsTable: output.migrationsTable, |
| 379 | migrationsHashes: output.migrationsHashes, |
| 380 | importHashes: output.importHashes, |
| 381 | })), |
| 382 | Effect.catchTag("DatabaseNotFound", () => Effect.undefined), |
| 383 | ); |
| 384 | } |
| 385 | const name = yield* createDatabaseName(id, olds?.name); |
| 386 | const match = yield* d1.listDatabases.items({ accountId, name }).pipe( |
| 387 | Stream.filter((db) => db.name === name), |
| 388 | Stream.runHead, |
| 389 | Effect.map(Option.getOrUndefined), |
| 390 | ); |
| 391 | if (match) { |
| 392 | return { |
| 393 | databaseId: match.uuid!, |
| 394 | databaseName: match.name ?? name, |
| 395 | jurisdiction: (olds?.jurisdiction ?? "default") as Jurisdiction, |
| 396 | readReplication: olds?.readReplication, |
| 397 | accountId, |
| 398 | migrationsDir: (olds && migrationsInputOf(olds))?.dir, |
| 399 | migrationsTable: (olds && migrationsInputOf(olds))?.table, |
| 400 | migrationsHashes: {}, |
| 401 | importHashes: {}, |
| 402 | }; |
| 403 | } |
| 404 | return undefined; |
| 405 | }), |
| 406 | reconcile: Effect.fn(function* ({ id, news = {}, output }) { |
| 407 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 408 | const name = yield* createDatabaseName(id, news.name); |
| 409 | const jurisdiction = news.jurisdiction ?? "default"; |
| 410 | const acct = output?.accountId ?? accountId; |
| 411 | |
| 412 | // Observe — re-fetch the cached database; fall back to a name |
| 413 | // lookup so we recover from out-of-band deletes or partial |
| 414 | // state-persistence failures (the create call may have written |
| 415 | // the database but lost the result before persist). |
| 416 | let observed: |
| 417 | | { |
| 418 | uuid?: string | null; |
| 419 | name?: string | null; |
| 420 | // Distilled widened generated string enums to open unions. |
| 421 | readReplication?: { mode: string } | null; |
| 422 | } |
| 423 | | undefined; |
| 424 | if (output?.databaseId) { |
| 425 | observed = yield* d1 |
| 426 | .getDatabase({ accountId: acct, databaseId: output.databaseId }) |
| 427 | .pipe(Effect.catchTag("DatabaseNotFound", () => Effect.undefined)); |
| 428 | } |
| 429 | if (!observed) { |
| 430 | observed = yield* d1.listDatabases.items({ accountId: acct, name }).pipe( |
| 431 | Stream.filter((db) => db.name === name), |
| 432 | Stream.runHead, |
| 433 | Effect.map(Option.getOrUndefined), |
| 434 | ); |
| 435 | } |
| 436 | |
| 437 | // Ensure — create if missing. Cloudflare returns |
| 438 | // `InvalidProperty` when a database with the same name already |
| 439 | // exists; we tolerate the race by re-listing to find it. |
| 440 | let databaseId: string; |
| 441 | let databaseName: string; |
| 442 | const isFirstCreation = !observed; |
| 443 | if (!observed) { |
| 444 | const db = yield* d1 |
| 445 | .createDatabase({ |
| 446 | accountId: acct, |
| 447 | name, |
| 448 | jurisdiction: jurisdiction !== "default" ? jurisdiction : undefined, |
| 449 | primaryLocationHint: news.primaryLocationHint, |
| 450 | }) |
| 451 | .pipe( |
| 452 | Effect.catchTag("InvalidProperty", () => |
| 453 | Effect.gen(function* () { |
| 454 | const match = yield* d1.listDatabases.items({ accountId: acct, name }).pipe( |
| 455 | Stream.filter((db) => db.name === name), |
| 456 | Stream.runHead, |
| 457 | Effect.map(Option.getOrUndefined), |
| 458 | ); |
| 459 | if (match) { |
| 460 | return match; |
| 461 | } |
| 462 | return yield* Effect.die( |
| 463 | `Database with name "${name}" already exists but could not be found`, |
| 464 | ); |
| 465 | }), |
| 466 | ), |
| 467 | ); |
| 468 | databaseId = db.uuid!; |
| 469 | databaseName = db.name ?? name; |
| 470 | } else { |
| 471 | databaseId = observed.uuid ?? output!.databaseId; |
| 472 | databaseName = observed.name ?? name; |
| 473 | } |
| 474 | |
| 475 | // Sync read replication — the only mutable property on the |
| 476 | // database resource itself. Always patch with the desired mode |
| 477 | // so adoption converges drifted state. |
| 478 | const desiredReplicationMode = news.readReplication?.mode ?? "disabled"; |
| 479 | const observedReplicationMode = observed?.readReplication?.mode ?? "disabled"; |
| 480 | if ( |
| 481 | isFirstCreation |
| 482 | ? desiredReplicationMode !== "disabled" |
| 483 | : observedReplicationMode !== desiredReplicationMode |
| 484 | ) { |
| 485 | const updated = yield* d1.patchDatabase({ |
| 486 | accountId: acct, |
| 487 | databaseId, |
| 488 | readReplication: { mode: desiredReplicationMode }, |
| 489 | }); |
| 490 | databaseId = updated.uuid ?? databaseId; |
| 491 | databaseName = updated.name ?? databaseName; |
| 492 | } |
| 493 | |
| 494 | // Clone is a one-shot seed performed only on first creation. |
| 495 | // Re-running it on an existing database would clobber data. |
| 496 | if (isFirstCreation && news.clone) { |
| 497 | const sourceId = yield* resolveCloneSource(news.clone, acct); |
| 498 | yield* cloneDatabase({ |
| 499 | accountId: acct, |
| 500 | sourceDatabaseId: sourceId, |
| 501 | targetDatabaseId: databaseId, |
| 502 | }); |
| 503 | } |
| 504 | |
| 505 | // Sync migrations — the shared pipeline is idempotent |
| 506 | // (already-applied entries are skipped), so this works for both |
| 507 | // first create and ongoing updates. Foreign (drizzle/prisma/ |
| 508 | // wrangler) or legacy history is converted into |
| 509 | // __alchemy_migrations on first contact. |
| 510 | const migrationsInput = migrationsInputOf(news); |
| 511 | const migrations = migrationsInput |
| 512 | ? yield* runMigrations({ |
| 513 | input: migrationsInput, |
| 514 | stamped: stampedOf(output), |
| 515 | withExecutor: (apply) => |
| 516 | Effect.gen(function* () { |
| 517 | const queryDb = yield* d1.queryDatabase; |
| 518 | yield* apply( |
| 519 | makeD1MigrationExecutor((sql) => queryDb({ accountId: acct, databaseId, sql })), |
| 520 | ); |
| 521 | }), |
| 522 | }) |
| 523 | : undefined; |
| 524 | |
| 525 | const importHashes = yield* applySqlImports({ |
| 526 | importFiles: news.importFiles, |
| 527 | rootDir: yield* rootDir, |
| 528 | previous: output?.importHashes, |
| 529 | apply: (file) => |
| 530 | importD1Database({ |
| 531 | accountId: acct, |
| 532 | databaseId, |
| 533 | sqlData: file.sql, |
| 534 | filename: file.id, |
| 535 | }).pipe(Effect.asVoid), |
| 536 | }); |
| 537 | |
| 538 | return { |
| 539 | databaseId, |
| 540 | databaseName, |
| 541 | jurisdiction: (output?.jurisdiction ?? jurisdiction) as Jurisdiction, |
| 542 | readReplication: news.readReplication, |
| 543 | accountId: acct, |
| 544 | // On first creation prior state is meaningless; otherwise removing |
| 545 | // `migrations` keeps the stamp and hashes for a later re-add. |
| 546 | ...migrationsAttrs({ |
| 547 | input: migrationsInput, |
| 548 | run: migrations, |
| 549 | output: isFirstCreation ? undefined : output, |
| 550 | }), |
| 551 | importHashes, |
| 552 | }; |
| 553 | }), |
| 554 | delete: Effect.fn(function* ({ output }) { |
| 555 | yield* d1 |
| 556 | .deleteDatabase({ accountId: output.accountId, databaseId: output.databaseId }) |
| 557 | .pipe(Effect.catchTag("DatabaseNotFound", () => Effect.void)); |
| 558 | }), |
| 559 | }); |
| 560 | |
| 561 | /** |
| 562 | * Local (dev) provider — the database is purely virtual: a `dev:` id keyed |
| 563 | * into the local workerd D1 simulator (DO SQLite under `.alchemy/local/d1`). |
| 564 | * `toRuntimeBinding` lowers a `d1` binding whose id is `dev:`-prefixed onto |
| 565 | * the local D1 service. |
| 566 | * |
| 567 | * Migrations ARE applied locally: reconcile boots an ephemeral gateway |
| 568 | * workerd (see `LocalD1Gateway.ts`) and drives the same executor-agnostic |
| 569 | * migration flow the live provider uses, against the simulator's storage. |
| 570 | * |
| 571 | * RPC-backed: under `alchemy dev` (an `RpcProviderProxy` in context) the |
| 572 | * whole lifecycle runs in the Cloudflare sidecar process — where |
| 573 | * `localRuntimeServices()` is real and shared with the Worker/Queue/ |
| 574 | * Container local providers — instead of in the user's process where that |
| 575 | * layer is gated empty (the root cause of #1007). In-process runs (no |
| 576 | * proxy: `sidecar: false` tests, a plain deploy deleting a local-mode row) |
| 577 | * build the provider directly with the un-gated runtime from the `dual` |
| 578 | * registration. |
| 579 | */ |
| 580 | export const ProviderLocal = () => |
| 581 | RpcProvider.effect( |
| 582 | Database, |
| 583 | LOCAL_PROVIDERS_URL, |
| 584 | Effect.gen(function* () { |
| 585 | // The local runtime services (workerd `Runtime`, binding plugins) and |
| 586 | // the HTTP client are resolved once at layer build and closed over — |
| 587 | // lifecycle effects run with the engine's call-time context, which |
| 588 | // doesn't include them. |
| 589 | const runtimeContext = yield* Effect.context<RuntimeServices | HttpClient.HttpClient>(); |
| 590 | |
| 591 | return { |
| 592 | stables: ["accountId"], |
| 593 | diff: Effect.fn(function* ({ news = {}, output }) { |
| 594 | const accountId = yield* localAccountId; |
| 595 | if (!output?.databaseId) return { action: "update" } as const; |
| 596 | if (!isResolved(news)) return undefined; |
| 597 | if (output.accountId !== accountId) { |
| 598 | return { action: "replace" } as const; |
| 599 | } |
| 600 | // Detect migration/import file drift — same rules as the live |
| 601 | // provider. |
| 602 | if (yield* diffMigrations({ news, output })) { |
| 603 | return { action: "update" } as const; |
| 604 | } |
| 605 | if (news.importFiles?.length) { |
| 606 | const newHashes = yield* hashImports(news.importFiles, yield* rootDir); |
| 607 | if (!recordsEqual(newHashes, output.importHashes ?? {})) { |
| 608 | return { action: "update" } as const; |
| 609 | } |
| 610 | } |
| 611 | // Fall through to the engine's default prop diff. |
| 612 | }), |
| 613 | read: Effect.fn(function* ({ output }) { |
| 614 | // Purely virtual — the persisted state row is the source of truth. |
| 615 | return output ?? undefined; |
| 616 | }), |
| 617 | reconcile: Effect.fn(function* ({ id, news = {}, output }) { |
| 618 | const accountId = yield* localAccountId; |
| 619 | const databaseId = output?.databaseId ?? generateLocalId(); |
| 620 | |
| 621 | // Sync migrations — the shared pipeline, driven through the |
| 622 | // ephemeral local gateway so the SAME bookkeeping applies |
| 623 | // locally as in the cloud. |
| 624 | const migrationsInput = migrationsInputOf(news); |
| 625 | const migrations = migrationsInput |
| 626 | ? yield* runMigrations({ |
| 627 | input: migrationsInput, |
| 628 | stamped: stampedOf(output), |
| 629 | withExecutor: (apply) => |
| 630 | withLocalD1Executor(databaseId, (executor) => |
| 631 | apply(makeD1MigrationExecutor(executor)), |
| 632 | ).pipe(Effect.provideContext(runtimeContext)), |
| 633 | }) |
| 634 | : undefined; |
| 635 | |
| 636 | // Locally an import file is just multi-statement SQL, executed |
| 637 | // through the same gateway. |
| 638 | const importHashes = yield* applySqlImports({ |
| 639 | importFiles: news.importFiles, |
| 640 | rootDir: yield* rootDir, |
| 641 | previous: output?.importHashes, |
| 642 | apply: (file) => |
| 643 | withLocalD1Query(databaseId, (query) => { |
| 644 | // One statement per batch entry: the simulator's SQLite |
| 645 | // rejects a whole file over its statement length limit. |
| 646 | const batch = splitSqliteStatements(file.sql).map((sql) => ({ sql })); |
| 647 | return batch.length > 0 ? query({ batch }) : Effect.void; |
| 648 | }).pipe(Effect.asVoid, Effect.provideContext(runtimeContext)), |
| 649 | }); |
| 650 | |
| 651 | return { |
| 652 | databaseId, |
| 653 | databaseName: yield* createDatabaseName(id, news.name), |
| 654 | jurisdiction: (news.jurisdiction ?? "default") as Jurisdiction, |
| 655 | readReplication: news.readReplication, |
| 656 | accountId: output?.accountId ?? accountId, |
| 657 | ...migrationsAttrs({ input: migrationsInput, run: migrations, output }), |
| 658 | importHashes, |
| 659 | }; |
| 660 | }), |
| 661 | delete: Effect.fn(function* () { |
| 662 | // The simulator's on-disk data is keyed by the dev id; dropping |
| 663 | // the state row is enough — orphaned data is reclaimed with |
| 664 | // `.alchemy`. |
| 665 | }), |
| 666 | }; |
| 667 | }), |
| 668 | ); |
| 669 | |
| 670 | export const DatabaseProvider = () => |
| 671 | ProviderLayer.dual(Database, { |
| 672 | // The local provider's reconcile boots an ephemeral workerd gateway to |
| 673 | // apply migrations, so it needs the shared local runtime layer. Under |
| 674 | // `alchemy dev` the provider is an RPC stub (this gated layer is empty |
| 675 | // and unused) and the provider group (`../Local.ts`) supplies the real |
| 676 | // runtime; without the proxy the provider builds in-process and this |
| 677 | // layer is real. |
| 678 | local: () => ProviderLocal().pipe(Layer.provide(localRuntimeServices())), |
| 679 | live: () => ProviderLive(), |
| 680 | }); |
| 681 | |
| 682 | const createDatabaseName = (id: string, name: string | undefined) => |
| 683 | Effect.gen(function* () { |
| 684 | return name ?? (yield* createPhysicalName({ id })); |
| 685 | }); |
| 686 | |
| 687 | const rootDir = Effect.sync(() => process.cwd()); |
| 688 | |
| 689 | /** |
| 690 | * Resolve a clone source spec into a concrete database UUID. Looks up by |
| 691 | * name through `listDatabases` when only a name is provided. |
| 692 | */ |
| 693 | const resolveCloneSource = (source: CloneSource, accountId: string) => |
| 694 | Effect.gen(function* () { |
| 695 | if ("databaseId" in source && source.databaseId) { |
| 696 | // At lifecycle time, Output<string> attributes have resolved to strings. |
| 697 | return source.databaseId as unknown as string; |
| 698 | } |
| 699 | if ("name" in source && source.name) { |
| 700 | const name = source.name as unknown as string; |
| 701 | const match = yield* d1.listDatabases.items({ accountId, name }).pipe( |
| 702 | Stream.filter((db) => db.name === name), |
| 703 | Stream.runHead, |
| 704 | Effect.map(Option.getOrUndefined), |
| 705 | ); |
| 706 | if (!match?.uuid) { |
| 707 | return yield* Effect.die(`Source database "${name}" not found for cloning`); |
| 708 | } |
| 709 | return match.uuid; |
| 710 | } |
| 711 | return yield* Effect.die("Invalid clone source: must provide databaseId or name"); |
| 712 | }); |