Skip to content

Commit

Permalink
fix(wrangler): ignore comments when splitting sql into statements (#8175
Browse files Browse the repository at this point in the history
)
  • Loading branch information
edmundhung authored and emily-shen committed Feb 20, 2025
1 parent 87029e0 commit 3970640
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-falcons-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: `unstable_splitSqlQuery` should ignore comments when splitting sql into statements
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ CREATE TABLE posts (
body TEXT NOT NULL,
FOREIGN KEY (author) REFERENCES users(username)
);
-- Migration ends here
42 changes: 27 additions & 15 deletions packages/wrangler/src/__tests__/d1/splitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ describe("splitSqlQuery()", () => {
AND "col;name" = \`other;col\`; -- or identifiers (Postgres or MySQL style)`
)
).toMatchInlineSnapshot(`
Array [
"SELECT * FROM my_table -- semicolons; in; comments; don't count;
WHERE val = 'foo;bar'
AND \\"col;name\\" = \`other;col\`",
"-- or identifiers (Postgres or MySQL style)",
]
`);
Array [
"SELECT * FROM my_table
WHERE val = 'foo;bar'
AND \\"col;name\\" = \`other;col\`",
]
`);
});

it("should handle block comments", () => {
Expand All @@ -131,14 +130,11 @@ describe("splitSqlQuery()", () => {
WHERE val = 'foo;bar' AND count / 2 > 0`
)
).toMatchInlineSnapshot(`
Array [
"/****
* Block comments are ignored;
****/
SELECT * FROM my_table /* semicolons; in; comments; don't count; */
WHERE val = 'foo;bar' AND count / 2 > 0",
]
`);
Array [
"SELECT * FROM my_table
WHERE val = 'foo;bar' AND count / 2 > 0",
]
`);
});

it("should split multiple statements", () => {
Expand All @@ -157,6 +153,22 @@ describe("splitSqlQuery()", () => {
`);
});

it("should ignore comment at the end", () => {
expect(
splitSqlQuery(
`
-- This is a comment
SELECT * FROM my_table WHERE id = 42 - 10;
-- This is a comment
`
)
).toMatchInlineSnapshot(`
Array [
"SELECT * FROM my_table WHERE id = 42 - 10",
]
`);
});

it("should handle whitespace between statements", () => {
expect(
splitSqlQuery(`
Expand Down
12 changes: 8 additions & 4 deletions packages/wrangler/src/d1/splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,25 @@ function splitSqlIntoStatements(sql: string): string[] {
break;
}
case `-`:
str += char;
next = iterator.next();
if (!next.done && next.value === "-") {
str += next.value + consumeUntilMarker(iterator, "\n");
// Skip to the end of the comment
consumeUntilMarker(iterator, "\n");
// Maintain the newline character
str += "\n";
break;
} else {
str += char;
continue;
}
case `/`:
str += char;
next = iterator.next();
if (!next.done && next.value === "*") {
str += next.value + consumeUntilMarker(iterator, "*/");
// Skip to the end of the comment
consumeUntilMarker(iterator, "*/");
break;
} else {
str += char;
continue;
}
case `;`:
Expand Down

0 comments on commit 3970640

Please sign in to comment.