-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[wrangler] Recognise compound statement markers without surrounding whitespace #15226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MatheusMartinho
wants to merge
6
commits into
cloudflare:main
Choose a base branch
from
MatheusMartinho:fix/d1-splitter-compound-delimiters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50bc2ee
[wrangler] Recognise compound statement markers without surrounding w…
MatheusMartinho 898dc6e
[wrangler] Match compound statement END before any non-identifier cha…
MatheusMartinho de80155
[wrangler] Treat bracket-quoted identifiers as quoted in the D1 splitter
MatheusMartinho d543f3a
[wrangler] Use Unicode identifier classes for compound statement markers
MatheusMartinho 0fde4b0
Merge branch 'main' into fix/d1-splitter-compound-delimiters
MatheusMartinho 975aa23
Merge branch 'main' into fix/d1-splitter-compound-delimiters
MatheusMartinho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "wrangler": patch | ||
| --- | ||
|
|
||
| Treat bracket-quoted identifiers as quoted when splitting D1 SQL files | ||
|
|
||
| The D1 SQL splitter handled `'`, `"` and backtick quoting but not SQLite's bracket-quoted identifiers (`[name]`), while `normalizeSqlLineEndings()` in the same file already did. A `;` inside such an identifier, e.g. `CREATE TABLE metrics ([value;unit] TEXT);`, was treated as a statement boundary, so `wrangler d1 execute --file` and `wrangler d1 migrations apply` sent broken fragments to D1. The scanner now skips over `[` … `]` like the other quote styles, which also keeps the new punctuation-delimited compound markers from matching keywords inside brackets, such as a `[end]` column in a trigger body. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "wrangler": patch | ||
| --- | ||
|
|
||
| Recognise compound statement markers that are not padded with whitespace | ||
|
|
||
| `wrangler d1 execute --file` and `wrangler d1 migrations apply` split a SQL file into statements before sending them to D1. The splitter only recognised `BEGIN`, `CASE` and `END` when they were surrounded by whitespace, so SQL that SQLite accepts, such as a trigger body ending in `INSERT ...;END;` or a trigger declared with `WHEN (1=1)BEGIN`, was split incorrectly: statements after the trigger were swallowed into it and silently sent as a single statement. | ||
|
|
||
| Markers are now matched when delimited by punctuation as well, while identifiers that merely end in a keyword, such as a `weekend` table, are still left alone. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,6 +149,10 @@ function splitSqlIntoStatements(sql: string): string[] { | |
| case "`": | ||
| str += char + consumeUntilMarker(iterator, char); | ||
| break; | ||
| case `[`: | ||
| // SQLite bracket-quoted identifiers end at the first `]`; there is no escape sequence. | ||
| str += char + consumeUntilMarker(iterator, `]`); | ||
| break; | ||
| case `$`: { | ||
| const dollarQuote = | ||
| "$" + consumeWhile(iterator, isDollarQuoteIdentifier); | ||
|
|
@@ -247,16 +251,27 @@ function isDollarQuoteIdentifier(str: string) { | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Compound statement markers only need to be delimited from surrounding | ||
| * identifiers, not padded with whitespace: SQLite accepts `WHEN (1=1)BEGIN` and | ||
| * `INSERT ...;END;`. The lookbehind keeps identifiers that merely end in the | ||
| * keyword, such as a `weekend` column, from matching; identifier characters | ||
| * include letters with diacritical marks, as in `isDollarQuoteIdentifier()`, | ||
| * so a `néend` column does not match either. | ||
| */ | ||
| const COMPOUND_STATEMENT_START = /(?<![\p{L}\p{N}_$])(BEGIN|CASE)\s$/iu; | ||
| const COMPOUND_STATEMENT_END = /(?<![\p{L}\p{N}_$])END[^\p{L}\p{N}_$]$/iu; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Decomposed identifiers break trigger splitting With a combining accent before Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| /** | ||
| * Returns true if the `str` ends with a compound statement `BEGIN` or `CASE` marker. | ||
| */ | ||
| function isCompoundStatementStart(str: string) { | ||
| return /\s(BEGIN|CASE)\s$/i.test(str); | ||
| return COMPOUND_STATEMENT_START.test(str); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the `str` ends with a compound statement `END` marker. | ||
| */ | ||
| function isCompoundStatementEnd(str: string) { | ||
| return /\sEND[;\s]$/i.test(str); | ||
| return COMPOUND_STATEMENT_END.test(str); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Parenthesized CASE breaks trigger splitting
When a trigger contains
CASE(,COMPOUND_STATEMENT_STARTmisses the case expression. ItsENDcloses the trigger early, splitting remaining statements apart.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.