diff --git a/.changeset/d1-splitter-lowercase-end.md b/.changeset/d1-splitter-lowercase-end.md new file mode 100644 index 00000000000..8039bcc2021 --- /dev/null +++ b/.changeset/d1-splitter-lowercase-end.md @@ -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. diff --git a/packages/wrangler/src/__tests__/d1/splitter.test.ts b/packages/wrangler/src/__tests__/d1/splitter.test.ts index 2a440be3bab..bde8dc1a0e3 100644 --- a/packages/wrangler/src/__tests__/d1/splitter.test.ts +++ b/packages/wrangler/src/__tests__/d1/splitter.test.ts @@ -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);`) + ).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(` diff --git a/packages/wrangler/src/d1/splitter.ts b/packages/wrangler/src/d1/splitter.ts index 4f09c327863..91e404871f7 100644 --- a/packages/wrangler/src/d1/splitter.ts +++ b/packages/wrangler/src/d1/splitter.ts @@ -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); }