Skip to content
Open
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
17 changes: 17 additions & 0 deletions .changeset/d1-splitter-lowercase-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"wrangler": patch
---

Fixes D1 SQL statements not handling lowercase `end`s correctly

`wrangler d1 execute` and `wrangler d1 migrations apply` split a SQL file into statements before running them. A `BEGIN` or `CASE` block closed with a lowercase `end` was not recognised as closed, so every statement after it was folded into that block instead of being run on its own. SQLite accepts either case, so a file like this applied only the trigger and silently skipped the table:

```sql
CREATE TRIGGER IF NOT EXISTS update_trigger AFTER UPDATE ON items
begin
DELETE FROM updates WHERE item_id=old.id;
end;
CREATE TABLE after_the_trigger (id TEXT PRIMARY KEY);
```

Files written with an uppercase `END` were unaffected. Both cases now behave the same.
21 changes: 21 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,27 @@ describe("splitSqlQuery()", () => {
`);
});

it("should handle a lowercase end closing a compound statement", ({
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 after_the_trigger (id TEXT PRIMARY KEY);`)
).toMatchInlineSnapshot(`
[
"CREATE TRIGGER IF NOT EXISTS update_trigger AFTER UPDATE ON items
begin
DELETE FROM updates WHERE item_id=old.id;
end",
"CREATE TABLE after_the_trigger (id TEXT 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