Repository navigation
fix(wb): migrate local D1 with the selected mechanism everywhere and allow isolated instances - #1696
Conversation
…allow isolated instances wb db deploy/migrate/reset now apply cf-native D1 migrations instead of drizzle-kit when selectD1Migrations selects them, also for packages without an ORM. Every local launch, including the development start and the *-dev e2e modes, applies drizzle-kit D1 migrations too. An exported absolute WB_CLOUDFLARE_STATE_DIR replaces the mode's state directory for wb start and wb db; wb never wipes it nor publishes that server's URL, and gen-dev-vars rewrites .dev.vars.<mode> only atomically and only when it changes, so concurrent instances do not disturb each other. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
ReviewFixLoop run summaryRun Run page: https://reviewfixloop.com/runs/2da421f66d75-2 Fixed (44)
Rejected (12)
Where a human reviewer should look
|
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the D1 database migration workflow in Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Migrations run in a steady stream, Local D1 now part of the dream. Isolated state, a directory choice, Giving the Workers a singular voice. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for running isolated local Cloudflare Worker instances concurrently by allowing callers to specify a custom, absolute state directory via the WB_CLOUDFLARE_STATE_DIR environment variable. It refactors local D1 migration handling, ensures .dev.vars.<mode> is written atomically to prevent race conditions, and updates the documentation and tests accordingly. Feedback was provided to improve the findFreePort helper in tests to prevent potential port collisions and test flakiness by tracking allocated ports.
wb embeds WB_CLOUDFLARE_STATE_DIR in shell scripts, whose quoting and normalization would turn a path with shell metacharacters or repeated spaces into another directory, which a reset would then delete. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…dev vars Instances of one mode share .dev.vars.<mode>, so an instance on a chosen WB_CLOUDFLARE_STATE_DIR stops instead of replacing values another instance wrote; per-instance values go through --var. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…rectory Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…fig targets D1 A Drizzle config for another database kept getting the local D1 URL from wb db deploy/migrate/reset, and its reset removed the local D1; both now follow whether the config targets D1, as the start commands do. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…local D1 URL Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Two instances on chosen state directories that both found no .dev.vars.<mode> could still replace each other's values; such an instance now publishes the file with a hard link, which fails when another instance published first, and then requires equal values. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
The --var argument reaches only the server, so the remedy is equal environment values for every instance plus --var for an instance's own one. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
The seed's database URL cannot tell D1 files apart, so wb db migrate|reset stop before migrating instead of seeding an arbitrary one. The README also names only the server starts that wipe the test state. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
… suggesting --var Passing a secret through --var puts it in the command wb logs, so the guidance now only requires every instance of a mode to get the same values. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…wner-readable Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
… environment Isolated instances shared .dev.vars.<mode>, and every attempt to arbitrate between its writers produced another defect. Both the Wrangler dev server and the Cloudflare Vite plugin read declared secrets from the process environment when no .dev.vars file exists, and the server already inherits wb's effective environment, so wb no longer generates the file: it removes wb gen-dev-vars and stops when a stale .dev.vars or .dev.vars.<mode> is left beside the config. Each instance then gets the secrets of its own environment. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…ronment The runtime lets the process environment override the config's text values, so an exported value (e.g. WB_ENV=development under MISE_ENV=test) replaced the configured one; the server environment now holds the config's values for those keys. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…n keys Writing the config's values into wb's own environment let a text binding named PORT replace the port wb listens on; the values now reach only the server command, through env. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…e config Values in the script text are altered by its normalization, so the local server now runs without those keys and the runtime takes the config's values as they are. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…of their own Removing differing keys made a config that derives a value from that key fall back to its default; the server now gets the config's evaluated value for such a key, passed through a separate variable so the script's normalization cannot alter it. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…own database Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
A scripts/start-production.sh or ecosystem.config.cjs start bypasses the Worker start commands, which now own the local D1 migration. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
The Cloudflare Vite plugin takes only declared secrets from the environment, so replacing exported values there only fed the config transformed inputs. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
The seed may rewrite data, so only production-style starts, which seeded before, run it after migrating a drizzle-kit D1. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Replacing or removing exported keys named like bindings changed the inputs cf dev evaluates the config with. The server now gets the exported environment, as Wrangler run alone does, except WB_ENV, which wb sets to the mode it resolved. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Without the file, Wrangler replaces any text or JSON binding named like an exported variable (e.g. NEXT_PUBLIC_BASE_URL, which wb itself exports) with that variable. Regular starts generate .dev.vars.<mode> again, atomically and only when it changes; instances on a chosen state directory never write it and read it when it exists, else their own environment. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Setting WB_ENV only for cf dev let a config that reads it bind another D1 than the one wb migrated. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Isolated instances read the mode's .dev.vars.<mode> when it exists, so the test no longer expects a per-instance secret value from the environment. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
A drizzle config for D1 selects the d1-http driver from CLOUDFLARE_D1_DATABASE_ID, which wb sets only for remote migrations; the local D1 wrapper now unsets it, so an exported one cannot make a local start migrate the remote database. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…l D1 data Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
It read workspace environments before wb start sets the mode, and a start never resets the directory. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…ures Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Close #1688
Close #1691
Why
Converting Workers from wrangler to the cf CLI hit three gaps in wb:
wb db deploy|migrate|resetchose the migration tool from the ORM dependency alone. A Drizzle package whose D1 binding cf migrates (wb.d1Migrations) therefore randrizzle-kit migrateon the same local D1, applying the SQL a second time under another journal (fix(wb): make wb db follow the cf-native D1 migration mechanism #1688). A package without an ORM could not usewb dbat all.wb start,--mode dev) and the*-deve2e modes applied only cf-native migrations, so a drizzle-kit Worker servedno such tableon fresh state (fix(wb): apply the selected D1 migration mechanism on every local launch #1691).cf d1 raw --persist-to) nor migrate a local D1 into a directory it chose without starting a server. A test-mode start also wiped the mode's directory, which would destroy the state of any other instance of that mode.Requirements
selectD1Migrations) inwb dband on every local launch.WB_CLOUDFLARE_STATE_DIR, the variable wb already hands to Vite, rather than a new CLI option. It is one name for one concept, nested commands inherit it, andwb dbalready takes its mode from the environment._.@+-/and resolved lexically. wb embeds it in shell scripts that it also normalizes, and a mangled path could make a reset delete another directory..dev.vars.<mode>for such an instance. The directory belongs to the caller, and concurrent instances must not write shared files.db-migrate(wb db migrate --check-idempotency), because that command now runs the selected mechanism.Customer Summary
wb db migrate,deployandresetapply cf-native D1 migrations withcf d1 migrations apply --localwhen wb selects them, also in packages without an ORM. They run drizzle-kit only for a D1 that drizzle-kit migrates, or for a database other than D1.migrateandresetrun the seed of a Drizzle package whose config targets that D1.Every local launch, including the development start and the
*-deve2e modes, applies drizzle-kit D1 migrations. A development start does not run the seed; production-style starts and the test startup check do, as before.To run an isolated instance or to migrate a local D1 into a directory you choose, export an absolute
WB_CLOUDFLARE_STATE_DIR(plus aPORT):WB_CLOUDFLARE_STATE_DIR=/tmp/instance-1 WB_ENV=test PORT=20001 bun wb start --mode test -- --inspector-port 20002 WB_CLOUDFLARE_STATE_DIR=/tmp/instance-1 WB_ENV=test bun wb db migrate.dev.vars.<mode>: the server reads the mode's file when a regular start generated it, and its own environment otherwise.wb dbstops when one chosen directory would serve several packages.Technical Summary
src/scripts/localD1Scripts.tsholds three builders:selectLocalD1Migrations: the selection.buildLocalD1MigrationCommands: the start-time migration, either cf-native apply or drizzle-kitmigrate/deploywrapped with the local D1 URL, with aseedsflag.buildCfNativeD1DatabaseCommands: the cf-native commands ofwb db. A reset refuses to run while another binding has no migration mechanism, and the seed is refused when there are several cf-native bindings.buildCloudflareDevCommands,buildCloudflareBuiltStartCommandsand the custom-start branches ofstartProductionProtectedall call the start-time builder.Seeding.
ScriptArgv.seedsLocalD1carries the intent to seed. The built start and the test startup check set it; development and*-devstarts do not.BaseScripts.buildMigrationCommandsno longer migrates a drizzle-kit D1 (the Worker commands do).drizzleScripts.migrateForStartlost its D1 branch, which nothing reaches any more.drizzleScripts.resettakes its D1 branch only when the drizzle config targets D1.commands/prisma.tsbuildMigrationScriptruns the ORM command first, then the cf-native commands. It skips drizzle-kit when its config targets a D1 that cf migrates or when there is no drizzle config. It gives drizzle-kit the local D1 URL only when its config targets D1.wrapWithLocalD1DatabaseUrl:CLOUDFLARE_D1_DATABASE_ID, which wb sets only for remote drizzle-kit migrations, so a local command cannot reach a remote D1.metadata.sqliteby basename.getLocalStateDir(project, mode)returns the validatedWB_CLOUDFLARE_STATE_DIRwhen it is set. With a chosen directory, wb skips the test wipe, the seed, the URL publication (publishLocalServerUrl) and the.dev.vars.<mode>generation.wb gen-dev-varsnow rewrites that file atomically and only when its content changes.What to check hardest: the secret path of an isolated instance.
.dev.vars.<mode>(or leftover.dev.vars) exists, the runtime reads the declared secrets from the process environment.Testing
bun run verifypasses. CI passed on each recent reviewed head.cloudflareWorker.test.ts; both fail againstmain:wb start --mode testinstances, each with its own chosen directory, port and arguments after--, each serve their own migrated D1. A file in.cloudflare/state-testsurvives.wb db migrateon a Drizzle package whose D1 is cf-native migrates the chosen directory with cf. drizzle-kit is not installed there, so running it would fail the test.wb db migrate|reset|deployrun the expected commands for drizzle-kit, cf-native and ORM-free packages.WB_ENV=development MISE_ENV=testuses the test mode.WB_CLOUDFLARE_STATE_DIRas before; the Vite plugin's secret fallback was checked in its source.Notes
wb start --mode testinstances are documented as Wrangler-only, because a Vite-bundled Worker's--mode teststart builds into the package's shared Build Output..dev.vars.<mode>, because Wrangler overlays the raw variable. No WillBooster config does this.