Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/d1-splitter-lowercase-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

Recognise a lowercase `end` when splitting D1 SQL into statements

`wrangler d1 execute --file` and `wrangler d1 migrations apply` only treated an uppercase `END` as the terminator of a `BEGIN`/`CASE` compound statement, even though the opening `BEGIN`/`CASE` marker is matched case-insensitively. A trigger body closed with `end;` therefore never ended, and every following statement in the file was swallowed into it and executed as one statement.

The closing marker is now matched case-insensitively too.
19 changes: 19 additions & 0 deletions packages/wrangler/src/__tests__/d1/splitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,25 @@ describe("splitSqlQuery()", () => {
`);
});

it("should handle a lowercase compound statement END", ({ expect }) => {
expect(
splitSqlQuery(`
CREATE TRIGGER IF NOT EXISTS update_trigger AFTER UPDATE ON items
begin
DELETE FROM updates WHERE item_id=old.id;
end;
CREATE TABLE tasks (id INTEGER PRIMARY KEY);`)
Comment on lines +334 to +338

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.

Could you indent this SQL text? 🙏

).toMatchInlineSnapshot(`
[
"CREATE TRIGGER IF NOT EXISTS update_trigger AFTER UPDATE ON items
begin
DELETE FROM updates WHERE item_id=old.id;
end",
"CREATE TABLE tasks (id INTEGER PRIMARY KEY)",
]
`);
});

it("should handle compound statements for CASEs", ({ expect }) => {
expect(
splitSqlQuery(`
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/d1/splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,5 @@ function isCompoundStatementStart(str: string) {
* Returns true if the `str` ends with a compound statement `END` marker.
*/
function isCompoundStatementEnd(str: string) {
return /\sEND[;\s]$/.test(str);
return /\sEND[;\s]$/i.test(str);
}
Loading