Repository navigation
[wrangler] Fix local D1 migration parsing after CASE expressions - #15163
atiqrahmanx wants to merge 2 commits into
Conversation
🦋 Changeset detectedLatest commit: 4cbc5ff The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/codemods
@cloudflare/config
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-plugin
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
4a0ff6b to
c257f21
Compare
43ccec2 to
27377db
Compare
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]>
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]>
|
Looks like this needs a rebase |
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]>
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]>
fcc6266 to
42d010d
Compare
|
Rebased onto |
c70c4df to
70ac0f1
Compare
2692524 to
2ad48cc
Compare
2ad48cc to
f85b1c6
Compare
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
| `); | ||
|
|
||
| expect(statements).toHaveLength(2); | ||
| expect(statements[0]).toContain("INSERT INTO audit VALUES (NEW.id);"); |
There was a problem hiding this comment.
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);
ENDThere was a problem hiding this comment.
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.
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
|
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. |
Fixes #15162.
Wrangler splits SQL files on semicolons before sending statements to D1. The previous implementation tried to protect trigger bodies by tracking both
BEGINblocks andCASEexpressions with whitespace-sensitive regular expressions. ACASE ... ENDfollowed by punctuation could therefore leave the stack unbalanced and combine the rest of a local migration into one query, producingSQLITE_LOCKED.This change tracks SQLite
CREATE [TEMP|TEMPORARY] TRIGGERbodies directly. SQLite requires every command in a trigger body to end with a semicolon, so the trigger-closingENDis the one that follows a completed body command. The splitter can keep the trigger intact without interpretingCASEexpressions or confusing legal identifiers namedbeginandendwith control-flow markers.Validation:
CASE ... END,, parenthesized and compact CASE expressions, block comments adjacent to boundaries, temporary triggers, and unquotedbegin/endidentifiers.211, 4, 11, 199, 194, matching remote D1.wrangler d1 migrations apply --local, including unquotedendcolumns andCASE ... END/* comment */,.A picture of a cute animal (not mandatory, but encouraged)
Note
This is a contribution from an AI agent: OpenAI Codex, GPT-5.