Skip to content

[wrangler] Fix local D1 migration parsing after CASE expressions - #15163

Closed
atiqrahmanx wants to merge 2 commits into
cloudflare:mainfrom
atiqrahmanx:fix/d1-splitter-case-end-boundary
Closed

atiqrahmanx wants to merge 2 commits into
cloudflare:mainfrom
atiqrahmanx:fix/d1-splitter-case-end-boundary

Conversation

@atiqrahmanx

@atiqrahmanx atiqrahmanx commented Aug 13, 2026 •

Copy link
Copy Markdown

Fixes #15162.

Wrangler splits SQL files on semicolons before sending statements to D1. The previous implementation tried to protect trigger bodies by tracking both BEGIN blocks and CASE expressions with whitespace-sensitive regular expressions. A CASE ... END followed by punctuation could therefore leave the stack unbalanced and combine the rest of a local migration into one query, producing SQLITE_LOCKED.

This change tracks SQLite CREATE [TEMP|TEMPORARY] TRIGGER bodies directly. SQLite requires every command in a trigger body to end with a semicolon, so the trigger-closing END is the one that follows a completed body command. The splitter can keep the trigger intact without interpreting CASE expressions or confusing legal identifiers named begin and end with control-flow markers.

Validation:

  • Added regression coverage for CASE ... END,, parenthesized and compact CASE expressions, block comments adjacent to boundaries, temporary triggers, and unquoted begin/end identifiers.
  • Rebuilt Wrangler and applied the issue reproduction through local D1/workerd. All five migrations succeed with statement counts 211, 4, 11, 199, 194, matching remote D1.
  • Applied six additional adversarial migrations through wrangler d1 migrations apply --local, including unquoted end columns and CASE ... END/* comment */,.
  • Wrangler splitter tests, type-checking, type-aware linting, formatting, and the production build pass locally.

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: this corrects internal SQL splitting without changing public APIs or configuration.

A picture of a cute animal (not mandatory, but encouraged)

Note

This is a contribution from an AI agent: OpenAI Codex, GPT-5.

@changeset-bot

changeset-bot Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4cbc5ff

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

@workers-devprod
workers-devprod requested review from a team and edmundhung and removed request for a team August 13, 2026 03:49
@workers-devprod

workers-devprod commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/tidy-d1-trigger-splitting.md: [@cloudflare/wrangler]
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1 @cloudflare/wrangler]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1 @cloudflare/wrangler]

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@15163

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@15163

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@15163

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@15163

create-cloudflare

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

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15163

@cloudflare/kv-asset-handler

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

miniflare

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

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@15163

@cloudflare/pages-shared

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

@cloudflare/unenv-preset

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

@cloudflare/vite-plugin

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

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@15163

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@15163

@cloudflare/workers-editor-shared

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

@cloudflare/workers-utils

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

wrangler

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

commit: 4cbc5ff

@petebacondarwin
petebacondarwin force-pushed the fix/d1-splitter-case-end-boundary branch from 4a0ff6b to c257f21 Compare August 14, 2026 12:30
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@atiqrahmanx
atiqrahmanx force-pushed the fix/d1-splitter-case-end-boundary branch from 43ccec2 to 27377db Compare August 14, 2026 12:59
devin-ai-integration[bot]

This comment was marked as resolved.

atiqrahmanx added a commit to atiqrahmanx/workers-sdk that referenced this pull request Aug 14, 2026
The start matcher used an allow-list of preceding characters, so a CASE
written directly after an operator (`SET a=CASE ... END, b=2`) was never
pushed onto the compound statement stack while its `END,` still matched
the end marker. That popped the enclosing trigger's BEGIN frame and split
the trigger body, emitting a bare `END` statement SQLite rejects.

Recognize BEGIN/CASE unless preceded by a word character, `.` or a quote,
which keeps identifiers such as `showcase`, `t.case` and `"case"` from
being treated as keywords while staying at least as permissive as the end
marker.

Reported by Devin review on cloudflare#15163.

Co-Authored-By: Claude Opus 5 <[email protected]>
atiqrahmanx added a commit to atiqrahmanx/workers-sdk that referenced this pull request Aug 14, 2026
The start matcher accepted a marker at position 0. trimSqlQuery only strips the
exact uppercase `BEGIN TRANSACTION;`, so a file opening with `begin transaction;`
or `BEGIN DEFERRED;` had that BEGIN pushed onto the compound statement stack. No
matching END ever follows, so every subsequent `;` was swallowed and the whole
file was sent as one query. Dropping the start-of-input alternative restores the
previous behaviour; a leading CASE still splits correctly because its END finds
an empty stack.

Tests now cover the boundary space rather than individual reported inputs: CASE
preceded by each operator form, and files opening with each transaction keyword.
Also rewrites the changeset in user-facing terms per REVIEW.md.

Reported by Devin review on cloudflare#15163.

Co-Authored-By: Claude Opus 5 <[email protected]>
@petebacondarwin

Copy link
Copy Markdown
Contributor

Looks like this needs a rebase

atiqrahmanx added a commit to atiqrahmanx/workers-sdk that referenced this pull request Aug 14, 2026
The start matcher used an allow-list of preceding characters, so a CASE
written directly after an operator (`SET a=CASE ... END, b=2`) was never
pushed onto the compound statement stack while its `END,` still matched
the end marker. That popped the enclosing trigger's BEGIN frame and split
the trigger body, emitting a bare `END` statement SQLite rejects.

Recognize BEGIN/CASE unless preceded by a word character, `.` or a quote,
which keeps identifiers such as `showcase`, `t.case` and `"case"` from
being treated as keywords while staying at least as permissive as the end
marker.

Reported by Devin review on cloudflare#15163.

Co-Authored-By: Claude Opus 5 <[email protected]>
atiqrahmanx added a commit to atiqrahmanx/workers-sdk that referenced this pull request Aug 14, 2026
The start matcher accepted a marker at position 0. trimSqlQuery only strips the
exact uppercase `BEGIN TRANSACTION;`, so a file opening with `begin transaction;`
or `BEGIN DEFERRED;` had that BEGIN pushed onto the compound statement stack. No
matching END ever follows, so every subsequent `;` was swallowed and the whole
file was sent as one query. Dropping the start-of-input alternative restores the
previous behaviour; a leading CASE still splits correctly because its END finds
an empty stack.

Tests now cover the boundary space rather than individual reported inputs: CASE
preceded by each operator form, and files opening with each transaction keyword.
Also rewrites the changeset in user-facing terms per REVIEW.md.

Reported by Devin review on cloudflare#15163.

Co-Authored-By: Claude Opus 5 <[email protected]>
@atiqrahmanx
atiqrahmanx force-pushed the fix/d1-splitter-case-end-boundary branch from fcc6266 to 42d010d Compare August 14, 2026 17:19
@atiqrahmanx

atiqrahmanx commented Aug 14, 2026 •

Copy link
Copy Markdown
Author

Rebased onto main. The conflict was in packages/wrangler/src/d1/splitter.ts from replaying the first commit of this branch without the earlier merge commit — resolved in favour of this PR's END[^\w] boundary. All 23 tests in src/__tests__/d1/splitter.test.ts pass locally.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@atiqrahmanx
atiqrahmanx force-pushed the fix/d1-splitter-case-end-boundary branch from c70c4df to 70ac0f1 Compare August 17, 2026 11:50
devin-ai-integration[bot]

This comment was marked as resolved.

@atiqrahmanx
atiqrahmanx force-pushed the fix/d1-splitter-case-end-boundary branch 2 times, most recently from 2692524 to 2ad48cc Compare August 17, 2026 12:41
@atiqrahmanx
atiqrahmanx force-pushed the fix/d1-splitter-case-end-boundary branch from 2ad48cc to f85b1c6 Compare August 17, 2026 13:15
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • ✅ @cloudflare/wrangler
Show detailed file reviewers
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1]

`);

expect(statements).toHaveLength(2);
expect(statements[0]).toContain("INSERT INTO audit VALUES (NEW.id);");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we assert what the first statement is exactly here? I would expect it to be:

CREATE TRIGGER audit_source AFTER INSERT ON source
BEGIN
  INSERT INTO audit VALUES (NEW.id);
END

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 4cbc5ff. The exact assertion exposed that removing a block comment without preserving whitespace turned CREATE/* ... */TRIGGER into CREATETRIGGER, even though the trigger-state tokenizer recognized the words separately. The splitter now preserves a separator only when comment removal would otherwise join surrounding SQL, and this test asserts the complete first statement. Verified all 21 splitter tests, type-aware oxlint, Wrangler type-checking, formatting, and the focused local D1/Miniflare/workerd path.

Comment thread packages/wrangler/src/d1/splitter.ts Resolved
@github-project-automation github-project-automation Bot moved this from Untriaged to In Review in workers-sdk Sep 3, 2026
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • ✅ @cloudflare/wrangler
Show detailed file reviewers
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1]

@cjol

cjol commented Sep 25, 2026

Copy link
Copy Markdown
Member

Thanks for this. We've since replaced the D1 SQL splitter with a port of SQLite's own statement-completion logic in #15712, so this change no longer applies to the current code. The regression tests from this PR were carried into #15712, and I've confirmed the #15162 reproduction now passes on wrangler 4.140.0. Closing as superseded.

@cjol cjol closed this Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

wrangler d1 migrations apply --local fails with SQLITE_LOCKED while remote D1 succeeds

6 participants