From 24785a8d46557e94c94a75a06b93cd2280497e89 Mon Sep 17 00:00:00 2001 From: konaito <81353953+konaito@users.noreply.github.com> Date: Sat, 5 Dec 2026 00:08:05 +0900 Subject: [PATCH] fix(dev): accept a D1 preview_database_id and use it locally A D1 binding with preview_database_id stopped the dev server and builds with "cf cannot migrate Cloudflare setting d1_databases.0". The cf converter reports the option as unsupported, and Void did not handle it. Deployments keep using database_id. The dev server now binds the preview ID, which local void db commands already use, so migrations and the app read the same local database. --- packages/void/src/cli/cf-config-migrate.ts | 29 +++++++++- packages/void/src/config.ts | 1 + packages/void/src/plugin-cloudflare-config.ts | 34 +++++++++++ packages/void/src/plugin-cloudflare.ts | 5 ++ .../void/test/unit/cf-config-migrate.test.ts | 24 ++++++++ .../unit/plugin-cloudflare-config.test.ts | 58 ++++++++++++++++++- 6 files changed, 148 insertions(+), 3 deletions(-) diff --git a/packages/void/src/cli/cf-config-migrate.ts b/packages/void/src/cli/cf-config-migrate.ts index 78f031027c..1e19d2ddbc 100644 --- a/packages/void/src/cli/cf-config-migrate.ts +++ b/packages/void/src/cli/cf-config-migrate.ts @@ -44,15 +44,40 @@ function normalizeQueueConcurrency(raw: JsonObject) { return result; } +/** + * A deployment uses `database_id`. Void selects the local D1 database, including a + * `preview_database_id`, itself (see `local-d1.ts` and `applyLocalD1PreviewIds()`). + */ +function withoutD1PreviewIds(raw: JsonObject) { + if (!Array.isArray(raw.d1_databases)) { + return raw; + } + return { + ...raw, + d1_databases: raw.d1_databases.map((value: unknown) => { + const database = record(value); + if (!database || !('preview_database_id' in database)) { + return value; + } + const { preview_database_id: _local, ...rest } = database; + return rest; + }), + }; +} + +function normalizeBindings(raw: JsonObject) { + return withoutD1PreviewIds(normalizeQueueConcurrency(raw)); +} + /** Remove build-only fields and settings translated into cf's typed config below. */ function migrationInput(raw: JsonObject): JsonObject { - const result = normalizeQueueConcurrency(raw); + const result = normalizeBindings(raw); const environments = record(result.env); if (environments) { result.env = Object.fromEntries( Object.entries(environments).map(([name, value]) => [ name, - record(value) ? normalizeQueueConcurrency(value as JsonObject) : value, + record(value) ? normalizeBindings(value as JsonObject) : value, ]), ); } diff --git a/packages/void/src/config.ts b/packages/void/src/config.ts index b1c5dcdfd6..6ff18a4958 100644 --- a/packages/void/src/config.ts +++ b/packages/void/src/config.ts @@ -143,6 +143,7 @@ export type CloudflareConfig = Record & { binding: string; database_name?: string; database_id?: string; + preview_database_id?: string; migrations_dir?: string; migrations_table?: string; }> diff --git a/packages/void/src/plugin-cloudflare-config.ts b/packages/void/src/plugin-cloudflare-config.ts index 8472275a4f..dc674b58ba 100644 --- a/packages/void/src/plugin-cloudflare-config.ts +++ b/packages/void/src/plugin-cloudflare-config.ts @@ -157,6 +157,40 @@ export function toNativeCloudflareConfig(raw: WorkerConfig, root: string): Cloud return { ...config, worker: config.worker }; } +/** + * Give the dev server the same local D1 database as `void db` commands. Like Wrangler, they + * use a binding's `preview_database_id` locally; the deployable config keeps `database_id`. + * The first settings that declare a binding decide its local ID, so a selected environment + * comes before the top-level settings. + */ +export function applyLocalD1PreviewIds( + worker: Pick, + settings: ReadonlyArray<{ d1_databases?: unknown } | undefined>, +): void { + const env = worker.env; + if (!env) { + return; + } + const declared = new Set(); + for (const databases of settings.map((item) => item?.d1_databases)) { + if (!Array.isArray(databases)) { + continue; + } + for (const database of databases as Array | null>) { + const name = database?.binding; + if (typeof name !== 'string' || declared.has(name)) { + continue; + } + declared.add(name); + const binding = env[name]; + const previewId = database?.preview_database_id; + if (binding?.type === 'd1' && typeof previewId === 'string' && previewId.length > 0) { + binding.id = previewId; + } + } + } +} + /** * Give Container Durable Objects a regular binding in the dev server. Their unsafe * binding from `migrateBuiltWranglerConfig()` exists only for the Worker upload, and diff --git a/packages/void/src/plugin-cloudflare.ts b/packages/void/src/plugin-cloudflare.ts index 6b672d4102..cff5616225 100644 --- a/packages/void/src/plugin-cloudflare.ts +++ b/packages/void/src/plugin-cloudflare.ts @@ -17,6 +17,7 @@ import { nativeAuxiliaryWorkers, restoreLocalContainerBindings, toNativeCloudflareConfig, + applyLocalD1PreviewIds, } from './plugin-cloudflare-config.ts'; import type { AuxiliaryWorkerConfig, @@ -82,6 +83,8 @@ export function cloudflare(options: { let base = ( options.configPath ? readGeneratedWranglerConfigFile(options.configPath) : {} ) as WorkerConfig; + // The translation below drops local-only D1 preview IDs. Keep the settings that have them. + const localD1Settings: Array<{ d1_databases?: unknown }> = []; if (process.env.CLOUDFLARE_ENV && base.env) { const selected = base.env[process.env.CLOUDFLARE_ENV]; if (!selected) { @@ -90,6 +93,7 @@ export function cloudflare(options: { ); } const original = base; + localD1Settings.push(selected, original); base = convertToWranglerConfig(toNativeCloudflareConfig(base, root)); base.main = selected.main ?? original.main; base.migrations = selected.migrations ?? original.migrations; @@ -112,6 +116,7 @@ export function cloudflare(options: { } } else if (environment.command === 'serve') { restoreLocalContainerBindings(pluginNative.worker); + applyLocalD1PreviewIds(pluginNative.worker, [...localD1Settings, workerConfig]); localQueueBindings = addLocalQueueProducers( pluginNative.worker, options.localQueueProducers, diff --git a/packages/void/test/unit/cf-config-migrate.test.ts b/packages/void/test/unit/cf-config-migrate.test.ts index 1b1da84c40..cd13a5ad61 100644 --- a/packages/void/test/unit/cf-config-migrate.test.ts +++ b/packages/void/test/unit/cf-config-migrate.test.ts @@ -68,6 +68,30 @@ it('rejects unresolved blocking follow-ups instead of silently dropping settings ).toThrow('cf cannot migrate Cloudflare setting config.routes.0'); }); +it.each([false, true])( + 'deploys a D1 binding with a preview ID under its database_id (named=%s)', + (named) => { + const d1_databases = [ + { + binding: 'DB', + database_name: 'app-db', + database_id: 'production-id', + preview_database_id: 'preview-id', + }, + ]; + const migrated = migrateBuiltWranglerConfig( + { + name: 'void-migrate-test', + compatibility_date: '2026-09-29', + ...(named ? { env: { staging: { d1_databases } } } : { d1_databases }), + }, + named ? 'staging' : 'production', + ); + + expect(migrated.worker?.env?.DB).toMatchObject({ type: 'd1', id: 'production-id' }); + }, +); + it.each( [undefined, null, 7].flatMap((maxConcurrency) => [false, true].map((named) => ({ maxConcurrency, named })), diff --git a/packages/void/test/unit/plugin-cloudflare-config.test.ts b/packages/void/test/unit/plugin-cloudflare-config.test.ts index 020f01bec6..254130e3fc 100644 --- a/packages/void/test/unit/plugin-cloudflare-config.test.ts +++ b/packages/void/test/unit/plugin-cloudflare-config.test.ts @@ -1,5 +1,8 @@ import { afterEach, expect, it, vi } from 'vite-plus/test'; -import { addLocalQueueProducers } from '../../src/plugin-cloudflare-config.ts'; +import { + addLocalQueueProducers, + applyLocalD1PreviewIds, +} from '../../src/plugin-cloudflare-config.ts'; afterEach(() => { vi.restoreAllMocks(); @@ -24,3 +27,56 @@ it('warns once for an inferred binding shared by queue files', () => { ), ); }); + +it('gives the dev server the preview D1 database that void db commands use', () => { + const worker = { + env: { + DB: { type: 'd1' as const, id: 'production-id' }, + OTHER: { type: 'd1' as const, id: 'other-id' }, + }, + }; + + applyLocalD1PreviewIds(worker, [ + { + d1_databases: [ + { binding: 'DB', database_id: 'production-id', preview_database_id: 'preview-id' }, + { binding: 'OTHER', database_id: 'other-id' }, + ], + }, + ]); + + expect(worker.env).toEqual({ + DB: { type: 'd1', id: 'preview-id' }, + OTHER: { type: 'd1', id: 'other-id' }, + }); +}); + +it('takes the local D1 ID from the first settings that declare the binding', () => { + const worker = { + env: { + DB: { type: 'd1' as const, id: 'staging-id' }, + PLAIN: { type: 'd1' as const, id: 'plain-staging-id' }, + }, + }; + + applyLocalD1PreviewIds(worker, [ + { + d1_databases: [ + { binding: 'DB', database_id: 'staging-id', preview_database_id: 'staging-preview' }, + { binding: 'PLAIN', database_id: 'plain-staging-id' }, + ], + }, + { + d1_databases: [ + { binding: 'DB', database_id: 'production-id', preview_database_id: 'preview-id' }, + { binding: 'PLAIN', database_id: 'plain-id', preview_database_id: 'plain-preview' }, + ], + }, + undefined, + ]); + + expect(worker.env).toEqual({ + DB: { type: 'd1', id: 'staging-preview' }, + PLAIN: { type: 'd1', id: 'plain-staging-id' }, + }); +});