Skip to content

Commit eb46f98

Browse files
authored
fix(wrangler): ignore comments when splitting sql into statements (#8175)
1 parent ab4dcff commit eb46f98

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

.changeset/ten-falcons-applaud.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: `unstable_splitSqlQuery` should ignore comments when splitting sql into statements

fixtures/vitest-pool-workers-examples/d1/migrations/0000_initial.sql

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ CREATE TABLE posts (
1010
body TEXT NOT NULL,
1111
FOREIGN KEY (author) REFERENCES users(username)
1212
);
13+
-- Migration ends here

packages/wrangler/src/__tests__/d1/splitter.test.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,12 @@ describe("splitSqlQuery()", () => {
112112
AND "col;name" = \`other;col\`; -- or identifiers (Postgres or MySQL style)`
113113
)
114114
).toMatchInlineSnapshot(`
115-
Array [
116-
"SELECT * FROM my_table -- semicolons; in; comments; don't count;
117-
WHERE val = 'foo;bar'
118-
AND \\"col;name\\" = \`other;col\`",
119-
"-- or identifiers (Postgres or MySQL style)",
120-
]
121-
`);
115+
Array [
116+
"SELECT * FROM my_table
117+
WHERE val = 'foo;bar'
118+
AND \\"col;name\\" = \`other;col\`",
119+
]
120+
`);
122121
});
123122

124123
it("should handle block comments", () => {
@@ -131,14 +130,11 @@ describe("splitSqlQuery()", () => {
131130
WHERE val = 'foo;bar' AND count / 2 > 0`
132131
)
133132
).toMatchInlineSnapshot(`
134-
Array [
135-
"/****
136-
* Block comments are ignored;
137-
****/
138-
SELECT * FROM my_table /* semicolons; in; comments; don't count; */
139-
WHERE val = 'foo;bar' AND count / 2 > 0",
140-
]
141-
`);
133+
Array [
134+
"SELECT * FROM my_table
135+
WHERE val = 'foo;bar' AND count / 2 > 0",
136+
]
137+
`);
142138
});
143139

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

156+
it("should ignore comment at the end", () => {
157+
expect(
158+
splitSqlQuery(
159+
`
160+
-- This is a comment
161+
SELECT * FROM my_table WHERE id = 42 - 10;
162+
-- This is a comment
163+
`
164+
)
165+
).toMatchInlineSnapshot(`
166+
Array [
167+
"SELECT * FROM my_table WHERE id = 42 - 10",
168+
]
169+
`);
170+
});
171+
160172
it("should handle whitespace between statements", () => {
161173
expect(
162174
splitSqlQuery(`

packages/wrangler/src/d1/splitter.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,25 @@ function splitSqlIntoStatements(sql: string): string[] {
6767
break;
6868
}
6969
case `-`:
70-
str += char;
7170
next = iterator.next();
7271
if (!next.done && next.value === "-") {
73-
str += next.value + consumeUntilMarker(iterator, "\n");
72+
// Skip to the end of the comment
73+
consumeUntilMarker(iterator, "\n");
74+
// Maintain the newline character
75+
str += "\n";
7476
break;
7577
} else {
78+
str += char;
7679
continue;
7780
}
7881
case `/`:
79-
str += char;
8082
next = iterator.next();
8183
if (!next.done && next.value === "*") {
82-
str += next.value + consumeUntilMarker(iterator, "*/");
84+
// Skip to the end of the comment
85+
consumeUntilMarker(iterator, "*/");
8386
break;
8487
} else {
88+
str += char;
8589
continue;
8690
}
8791
case `;`:

0 commit comments

Comments
 (0)