Skip to content

Commit

Permalink
[D1] fix batch splitting to handle CASE as compound statement starts (#…
Browse files Browse the repository at this point in the history
…4951)

* fix: add CASE to compound statement starts

* chore: add changeset

* chore: cleanup and add tests

* chore: improve test coverage

* chore: delete leftover file

* chore: add missed commit

* Update packages/wrangler/src/d1/splitter.ts

Co-authored-by: Max Rozen <3822106+rozenmd@users.noreply.github.com>

---------

Co-authored-by: Max Rozen <3822106+rozenmd@users.noreply.github.com>
  • Loading branch information
nora-soderlund and rozenmd authored Feb 9, 2024
1 parent a71afaf commit ffafe8a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-kiwis-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: D1 batch splitting to handle CASE as compound statement starts
48 changes: 47 additions & 1 deletion packages/wrangler/src/__tests__/d1/splitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe("splitSqlQuery()", () => {
`);
});

it("should handle compound statements", () => {
it("should handle compound statements for BEGINs", () => {
expect(
splitSqlQuery(`
CREATE TRIGGER IF NOT EXISTS update_trigger AFTER UPDATE ON items
Expand Down Expand Up @@ -275,4 +275,50 @@ describe("splitSqlQuery()", () => {
]
`);
});

it("should handle compound statements for CASEs", () => {
expect(
splitSqlQuery(`
CREATE TRIGGER test_after_insert_trigger AFTER
INSERT ON test BEGIN
SELECT CASE
WHEN NOT EXISTS
(SELECT 1
FROM pragma_table_list(new."table")) THEN RAISE (
ABORT,
'Exception, table does not exist')
END ; END ;
CREATE TRIGGER test_after_insert_trigger AFTER
INSERT ON test BEGIN
SELECT CASE
WHEN NOT EXISTS
(SELECT 1
FROM pragma_table_list(new."table")) THEN RAISE (
ABORT,
'Exception, table does not exist')
END ; END ;`)
).toMatchInlineSnapshot(`
Array [
"CREATE TRIGGER test_after_insert_trigger AFTER
INSERT ON test BEGIN
SELECT CASE
WHEN NOT EXISTS
(SELECT 1
FROM pragma_table_list(new.\\"table\\")) THEN RAISE (
ABORT,
'Exception, table does not exist')
END ; END",
"CREATE TRIGGER test_after_insert_trigger AFTER
INSERT ON test BEGIN
SELECT CASE
WHEN NOT EXISTS
(SELECT 1
FROM pragma_table_list(new.\\"table\\")) THEN RAISE (
ABORT,
'Exception, table does not exist')
END ; END",
]
`);
});
});
4 changes: 2 additions & 2 deletions packages/wrangler/src/d1/splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ function isDollarQuoteIdentifier(str: string) {
}

/**
* Returns true if the `str` ends with a compound statement `BEGIN` marker.
* Returns true if the `str` ends with a compound statement `BEGIN` or `CASE` marker.
*/
function isCompoundStatementStart(str: string) {
return /\sBEGIN\s$/.test(str);
return /\s(BEGIN|CASE)\s$/.test(str);
}

/**
Expand Down

0 comments on commit ffafe8a

Please sign in to comment.