Skip to content

[wrangler] fix: throw clear error when D1 migration execution returns null#13882

Open
matingathani wants to merge 3 commits into
cloudflare:mainfrom
matingathani:fix/d1-migrations-null-response
Open

[wrangler] fix: throw clear error when D1 migration execution returns null#13882
matingathani wants to merge 3 commits into
cloudflare:mainfrom
matingathani:fix/d1-migrations-null-response

Conversation

@matingathani
Copy link
Copy Markdown
Contributor

@matingathani matingathani commented May 11, 2026

wrangler d1 migrations apply silently exited when executeSql returned null, leaving the migration table in an inconsistent state with no user feedback. The source file even had a // TODO: return error comment marking this as a known gap.

Fix: Replace the silent return with a UserError that names the migration and explains it was cancelled, so users always know what happened.


  • Tests
    • Automated tests not possible - manual testing has been completed as follows: the code path (executeSql returning null from a command-based invocation) is currently unreachable through the automated test infrastructure — executeSql only returns null when given a file input and the user declines an interactive prompt, which cannot be triggered from apply.ts's command-only call site. The fix is defensive programming that resolves the existing TODO and ensures correct behaviour if the call site ever changes.
  • Public documentation
    • Documentation not necessary because: this is an internal bug fix for an error message; no user-visible API or config surface changed.

Open in Devin Review

Copilot AI review requested due to automatic review settings May 11, 2026 02:57
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 11, 2026

🦋 Changeset detected

Latest commit: 0de5eee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
wrangler Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk May 11, 2026
@workers-devprod workers-devprod requested review from a team and NuroDev and removed request for a team May 11, 2026 02:57
@workers-devprod
Copy link
Copy Markdown
Contributor

workers-devprod commented May 11, 2026

Codeowners approval required for this PR:

  • ✅ @cloudflare/d1
  • ✅ @cloudflare/wrangler
Show detailed file reviewers

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Wrangler’s D1 migrations apply flow to surface a user-facing error when executeSql() returns null (cancellation), instead of silently returning and leaving migration state unclear.

Changes:

  • Replace the previous silent return on null SQL execution response with a UserError indicating the migration was cancelled.
  • Add a Changeset entry to publish the fix as a patch release.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/wrangler/src/d1/migrations/apply.ts Throws a UserError when a migration execution returns null to avoid silent exits.
.changeset/fix-d1-migrations-null-response.md Records the behavior change as a patch Changeset for wrangler.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/wrangler/src/d1/migrations/apply.ts
Comment thread packages/wrangler/src/d1/migrations/apply.ts Outdated
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 1 additional finding in Devin Review.

Open in Devin Review

Comment thread packages/wrangler/src/d1/migrations/apply.ts
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 11, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13882

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13882

miniflare

npm i https://pkg.pr.new/miniflare@13882

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13882

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13882

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13882

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13882

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@13882

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@13882

wrangler

npm i https://pkg.pr.new/wrangler@13882

commit: 0de5eee

Copy link
Copy Markdown
Member

@NuroDev NuroDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add some tests for this to make sure this improved error handling actually works as expected.

Comment thread packages/wrangler/src/d1/migrations/apply.ts Outdated
@github-project-automation github-project-automation Bot moved this from Untriaged to In Review in workers-sdk May 12, 2026
@matingathani
Copy link
Copy Markdown
Contributor Author

Addressed NuroDev's review — refactored the cancelled flag approach: the UserError is now thrown directly inside the try block when executeSql returns null, and the catch block re-throws if e instanceof UserError so the error propagates cleanly. Also added a regression test (migrate.test.ts) that mocks executeSql to return null and asserts the clear error message is surfaced.

@matingathani
Copy link
Copy Markdown
Contributor Author

CI failure (Tests, Windows, packages-and-tools) is the pre-existing miniflare browser plugin flake (test/plugins/browser/index.spec.ts) — browser session times out on the Windows runner. Unrelated to the D1 null-response changes. Could a maintainer re-run the failed job?

@matingathani matingathani requested a review from NuroDev May 13, 2026 01:16
@NuroDev NuroDev enabled auto-merge (squash) May 14, 2026 09:26
Copy link
Copy Markdown
Contributor

@workers-devprod workers-devprod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codeowners reviews satisfied

@github-project-automation github-project-automation Bot moved this from In Review to Approved in workers-sdk May 14, 2026
… null

The apply loop had a TODO: return error comment for the case where
executeSql returns null. Replace the silent return with a UserError
that names the migration and explains it was cancelled, so users are
never left without feedback.
- Throw UserError directly inside try block when executeSql returns null,
  then rethrow if instanceof UserError in catch — removes the `cancelled`
  flag approach suggested by NuroDev
- Add regression test: mocks executeSql to return null and asserts the
  clear UserError message is surfaced to the user
@petebacondarwin petebacondarwin force-pushed the fix/d1-migrations-null-response branch from 014a270 to 0de5eee Compare May 14, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

5 participants