From 11a7b43ac5b022eec32e7ff3290ec2d48b85ec66 Mon Sep 17 00:00:00 2001 From: bintangakbarRK Date: Thu, 6 Aug 2026 00:06:14 +0700 Subject: [PATCH 1/3] fix: normalize CRLF before sending D1 commands to the remote query API The D1 query API splits multi-statement SQL on ';' server-side and mishandles CRLF line endings inside compound statements such as a CREATE TRIGGER ... BEGIN ... END; body, producing 'incomplete input: SQLITE_ERROR'. wrangler d1 migrations apply --remote sends the whole migration (plus the tracking INSERT) as one command string, so CRLF migrations with triggers failed while the same bytes via the import-based execute --remote --file succeeded. Normalize CRLF to LF in the command path before sending. Closes #14991. --- .changeset/d1-crlf-remote-query.md | 7 +++ .../wrangler/src/__tests__/d1/execute.test.ts | 59 ++++++++++++++++++- packages/wrangler/src/d1/execute.ts | 9 ++- 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 .changeset/d1-crlf-remote-query.md diff --git a/.changeset/d1-crlf-remote-query.md b/.changeset/d1-crlf-remote-query.md new file mode 100644 index 00000000000..124eba53acd --- /dev/null +++ b/.changeset/d1-crlf-remote-query.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: normalize CRLF line endings before sending D1 commands to the remote query API + +`wrangler d1 migrations apply --remote` and `wrangler d1 execute --remote --command` failed with `incomplete input: SQLITE_ERROR` when the SQL contained CRLF line endings inside a compound statement such as a `CREATE TRIGGER ... BEGIN ... END;` body. The command string is now normalized to LF before it is sent to the D1 query API. diff --git a/packages/wrangler/src/__tests__/d1/execute.test.ts b/packages/wrangler/src/__tests__/d1/execute.test.ts index 2ced79fc5e7..113a9f7fef1 100644 --- a/packages/wrangler/src/__tests__/d1/execute.test.ts +++ b/packages/wrangler/src/__tests__/d1/execute.test.ts @@ -302,9 +302,62 @@ To continue without logging in, rerun this command with \`--temporary\`. Wrangle expect(std.out).toMatch("🚣 Executed 1 command in 123.46ms"); }); - it("should format batch execution duration with 2 decimal places", async ({ - expect, - }) => { + it("should normalize CRLF line endings in commands sent to the remote query API", async ({ + expect, + }) => { + setIsTTY(false); + writeWranglerConfig({ + d1_databases: [ + { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, + ], + }); + + msw.use( + ...getMswSuccessMembershipHandlers([ + { + id: "some-account-id", + name: "test-account", + }, + ]) + ); + + let sentSql: string | undefined; + msw.use( + http.get("*/accounts/:accountId/d1/database", async () => { + return HttpResponse.json( + createFetchResult([ + { uuid: "xxxx", name: "db", created_at: "", version: "alpha" }, + ]) + ); + }), + http.post( + "*/accounts/:accountId/d1/database/:databaseId/query", + async ({ request }) => { + sentSql = ((await request.json()) as { sql: string }).sql; + return HttpResponse.json( + createFetchResult([ + { + results: [{ result: 1 }], + success: true, + meta: { duration: 100 }, + }, + ]) + ); + } + ) + ); + + await runWrangler( + "d1 execute db --remote --command 'CREATE TRIGGER trg BEFORE DELETE ON probe_z\r\nBEGIN\r\n SELECT RAISE(ABORT, '\''no'\'');\r\nEND;'" + ); + + expect(sentSql).toBeDefined(); + expect(sentSql).not.toContain("\r"); + }); + + it("should format batch execution duration with 2 decimal places", async ({ + expect, + }) => { setIsTTY(false); writeWranglerConfig({ d1_databases: [ diff --git a/packages/wrangler/src/d1/execute.ts b/packages/wrangler/src/d1/execute.ts index 43d2627e678..19720e8823f 100644 --- a/packages/wrangler/src/d1/execute.ts +++ b/packages/wrangler/src/d1/execute.ts @@ -505,14 +505,17 @@ async function executeRemotely({ }, ]; } else { + // The D1 query API splits multi-statement SQL on `;` server-side, and + // mishandles CRLF line endings inside compound statements such as a + // `CREATE TRIGGER ... BEGIN ... END;` body (issue #14991). Normalize + // to LF so the server receives the same input that works for LF files. + const sql = input.command?.replace(/\r\n/g, "\n"); const result = await d1ApiPost( config, accountId, db, "query", - { - sql: input.command, - } + { sql } ); logResult(result); return result; From 45336ef3d3bf770f6c514f4bc642e2b2e758e958 Mon Sep 17 00:00:00 2001 From: bintangakbarRK Date: Thu, 6 Aug 2026 00:26:13 +0700 Subject: [PATCH 2/3] chore: fix changeset title and test indentation per review feedback - Remove the conventional-commit prefix from the changeset title (REVIEW.md forbids type prefixes in changeset titles). - Re-indent the new execute test to the describe nesting level and apply oxfmt so the repository formatting check passes. --- .changeset/d1-crlf-remote-query.md | 2 +- .../wrangler/src/__tests__/d1/execute.test.ts | 94 +++++++++---------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/.changeset/d1-crlf-remote-query.md b/.changeset/d1-crlf-remote-query.md index 124eba53acd..6e641c4946a 100644 --- a/.changeset/d1-crlf-remote-query.md +++ b/.changeset/d1-crlf-remote-query.md @@ -2,6 +2,6 @@ "wrangler": patch --- -fix: normalize CRLF line endings before sending D1 commands to the remote query API +Normalize CRLF line endings before sending D1 commands to the remote query API `wrangler d1 migrations apply --remote` and `wrangler d1 execute --remote --command` failed with `incomplete input: SQLITE_ERROR` when the SQL contained CRLF line endings inside a compound statement such as a `CREATE TRIGGER ... BEGIN ... END;` body. The command string is now normalized to LF before it is sent to the D1 query API. diff --git a/packages/wrangler/src/__tests__/d1/execute.test.ts b/packages/wrangler/src/__tests__/d1/execute.test.ts index 113a9f7fef1..d4041276d90 100644 --- a/packages/wrangler/src/__tests__/d1/execute.test.ts +++ b/packages/wrangler/src/__tests__/d1/execute.test.ts @@ -302,62 +302,62 @@ To continue without logging in, rerun this command with \`--temporary\`. Wrangle expect(std.out).toMatch("🚣 Executed 1 command in 123.46ms"); }); - it("should normalize CRLF line endings in commands sent to the remote query API", async ({ - expect, - }) => { - setIsTTY(false); - writeWranglerConfig({ - d1_databases: [ - { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, - ], - }); + it("should normalize CRLF line endings in commands sent to the remote query API", async ({ + expect, + }) => { + setIsTTY(false); + writeWranglerConfig({ + d1_databases: [ + { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, + ], + }); - msw.use( - ...getMswSuccessMembershipHandlers([ - { - id: "some-account-id", - name: "test-account", - }, - ]) - ); + msw.use( + ...getMswSuccessMembershipHandlers([ + { + id: "some-account-id", + name: "test-account", + }, + ]) + ); - let sentSql: string | undefined; - msw.use( - http.get("*/accounts/:accountId/d1/database", async () => { - return HttpResponse.json( - createFetchResult([ - { uuid: "xxxx", name: "db", created_at: "", version: "alpha" }, - ]) - ); - }), - http.post( - "*/accounts/:accountId/d1/database/:databaseId/query", - async ({ request }) => { - sentSql = ((await request.json()) as { sql: string }).sql; + let sentSql: string | undefined; + msw.use( + http.get("*/accounts/:accountId/d1/database", async () => { return HttpResponse.json( createFetchResult([ - { - results: [{ result: 1 }], - success: true, - meta: { duration: 100 }, - }, + { uuid: "xxxx", name: "db", created_at: "", version: "alpha" }, ]) ); - } - ) - ); + }), + http.post( + "*/accounts/:accountId/d1/database/:databaseId/query", + async ({ request }) => { + sentSql = ((await request.json()) as { sql: string }).sql; + return HttpResponse.json( + createFetchResult([ + { + results: [{ result: 1 }], + success: true, + meta: { duration: 100 }, + }, + ]) + ); + } + ) + ); - await runWrangler( - "d1 execute db --remote --command 'CREATE TRIGGER trg BEFORE DELETE ON probe_z\r\nBEGIN\r\n SELECT RAISE(ABORT, '\''no'\'');\r\nEND;'" - ); + await runWrangler( + "d1 execute db --remote --command 'CREATE TRIGGER trg BEFORE DELETE ON probe_z\r\nBEGIN\r\n SELECT RAISE(ABORT, '\''no'\'');\r\nEND;'" + ); - expect(sentSql).toBeDefined(); - expect(sentSql).not.toContain("\r"); - }); + expect(sentSql).toBeDefined(); + expect(sentSql).not.toContain("\r"); + }); - it("should format batch execution duration with 2 decimal places", async ({ - expect, - }) => { + it("should format batch execution duration with 2 decimal places", async ({ + expect, + }) => { setIsTTY(false); writeWranglerConfig({ d1_databases: [ From 1b7cd77ee3b24aa2b4ea00f7c109b12a60a40826 Mon Sep 17 00:00:00 2001 From: bintangakbarRK Date: Wed, 12 Aug 2026 14:31:23 +0700 Subject: [PATCH 3/3] fix: preserve quoted CRLF in D1 remote queries --- .changeset/d1-crlf-remote-query.md | 4 +- .../wrangler/src/__tests__/d1/execute.test.ts | 9 +- .../src/__tests__/d1/splitter.test.ts | 19 +++- packages/wrangler/src/d1/execute.ts | 6 +- packages/wrangler/src/d1/splitter.ts | 92 +++++++++++++++++++ 5 files changed, 120 insertions(+), 10 deletions(-) diff --git a/.changeset/d1-crlf-remote-query.md b/.changeset/d1-crlf-remote-query.md index 6e641c4946a..780b2a38585 100644 --- a/.changeset/d1-crlf-remote-query.md +++ b/.changeset/d1-crlf-remote-query.md @@ -2,6 +2,6 @@ "wrangler": patch --- -Normalize CRLF line endings before sending D1 commands to the remote query API +Normalize structural CRLF line endings before sending D1 commands to the remote query API -`wrangler d1 migrations apply --remote` and `wrangler d1 execute --remote --command` failed with `incomplete input: SQLITE_ERROR` when the SQL contained CRLF line endings inside a compound statement such as a `CREATE TRIGGER ... BEGIN ... END;` body. The command string is now normalized to LF before it is sent to the D1 query API. +`wrangler d1 migrations apply --remote` and `wrangler d1 execute --remote --command` failed with `incomplete input: SQLITE_ERROR` when the SQL contained CRLF line endings inside a compound statement such as a `CREATE TRIGGER ... BEGIN ... END;` body. Structural line endings are now normalized to LF before the command is sent to the D1 query API, while CRLF inside quoted values and identifiers remains unchanged. diff --git a/packages/wrangler/src/__tests__/d1/execute.test.ts b/packages/wrangler/src/__tests__/d1/execute.test.ts index d4041276d90..aad4d26ce27 100644 --- a/packages/wrangler/src/__tests__/d1/execute.test.ts +++ b/packages/wrangler/src/__tests__/d1/execute.test.ts @@ -302,7 +302,7 @@ To continue without logging in, rerun this command with \`--temporary\`. Wrangle expect(std.out).toMatch("🚣 Executed 1 command in 123.46ms"); }); - it("should normalize CRLF line endings in commands sent to the remote query API", async ({ + it("should preserve quoted CRLF when normalizing commands sent to the remote query API", async ({ expect, }) => { setIsTTY(false); @@ -348,11 +348,12 @@ To continue without logging in, rerun this command with \`--temporary\`. Wrangle ); await runWrangler( - "d1 execute db --remote --command 'CREATE TRIGGER trg BEFORE DELETE ON probe_z\r\nBEGIN\r\n SELECT RAISE(ABORT, '\''no'\'');\r\nEND;'" + "d1 execute db --remote --command \"CREATE TRIGGER trg BEFORE DELETE ON probe_z\r\nBEGIN\r\n SELECT RAISE(ABORT, 'no\r\nchange');\r\nEND;\"" ); - expect(sentSql).toBeDefined(); - expect(sentSql).not.toContain("\r"); + expect(sentSql).toBe( + "CREATE TRIGGER trg BEFORE DELETE ON probe_z\nBEGIN\n SELECT RAISE(ABORT, 'no\r\nchange');\nEND;" + ); }); it("should format batch execution duration with 2 decimal places", async ({ diff --git a/packages/wrangler/src/__tests__/d1/splitter.test.ts b/packages/wrangler/src/__tests__/d1/splitter.test.ts index 2a440be3bab..9be78a4fc07 100644 --- a/packages/wrangler/src/__tests__/d1/splitter.test.ts +++ b/packages/wrangler/src/__tests__/d1/splitter.test.ts @@ -1,5 +1,22 @@ import { describe, it } from "vitest"; -import { mayContainMultipleStatements, splitSqlQuery } from "../../d1/splitter"; +import { + mayContainMultipleStatements, + normalizeSqlLineEndings, + splitSqlQuery, +} from "../../d1/splitter"; + +describe("normalizeSqlLineEndings()", () => { + it("should preserve CRLF inside quoted SQL values and identifiers", ({ + expect, + }) => { + const sql = + "SELECT 'single''quote\r\nvalue', \"double\r\nquote\", `backtick\r\nquote`, [bracket\r\nquote]; -- don't stop scanning\r\n/* block\r\ncomment */\r\nSELECT 1;"; + + expect(normalizeSqlLineEndings(sql)).toBe( + "SELECT 'single''quote\r\nvalue', \"double\r\nquote\", `backtick\r\nquote`, [bracket\r\nquote]; -- don't stop scanning\n/* block\ncomment */\nSELECT 1;" + ); + }); +}); describe("mayContainMultipleStatements()", () => { it("should return false if there is only a semi-colon at the end", ({ diff --git a/packages/wrangler/src/d1/execute.ts b/packages/wrangler/src/d1/execute.ts index 19720e8823f..777514ea796 100644 --- a/packages/wrangler/src/d1/execute.ts +++ b/packages/wrangler/src/d1/execute.ts @@ -21,7 +21,7 @@ import { confirm } from "../dialogs"; import { logger } from "../logger"; import { readableRelative } from "../paths"; import { requireAuth } from "../user"; -import { splitSqlQuery } from "./splitter"; +import { normalizeSqlLineEndings, splitSqlQuery } from "./splitter"; import { getDatabaseByNameOrBinding, getDatabaseInfoFromConfig } from "./utils"; import type { Database, @@ -508,8 +508,8 @@ async function executeRemotely({ // The D1 query API splits multi-statement SQL on `;` server-side, and // mishandles CRLF line endings inside compound statements such as a // `CREATE TRIGGER ... BEGIN ... END;` body (issue #14991). Normalize - // to LF so the server receives the same input that works for LF files. - const sql = input.command?.replace(/\r\n/g, "\n"); + // structural line endings while preserving quoted SQL values. + const sql = input.command && normalizeSqlLineEndings(input.command); const result = await d1ApiPost( config, accountId, diff --git a/packages/wrangler/src/d1/splitter.ts b/packages/wrangler/src/d1/splitter.ts index 4f09c327863..733b47cd7af 100644 --- a/packages/wrangler/src/d1/splitter.ts +++ b/packages/wrangler/src/d1/splitter.ts @@ -37,6 +37,98 @@ export function splitSqlQuery(sql: string): string[] { } } +/** + * Normalize structural CRLF line endings without changing quoted SQL values or + * identifiers. + */ +export function normalizeSqlLineEndings(sql: string): string { + let normalized = ""; + let quoteEnd: "'" | '"' | "`" | "]" | undefined; + let inLineComment = false; + let inBlockComment = false; + + for (let index = 0; index < sql.length; index++) { + const char = sql[index]; + const nextChar = sql[index + 1]; + + if (quoteEnd !== undefined) { + normalized += char; + if (char === quoteEnd) { + if (nextChar === quoteEnd) { + normalized += nextChar; + index++; + } else { + quoteEnd = undefined; + } + } + continue; + } + + if (inLineComment) { + if (char === "\r" && nextChar === "\n") { + normalized += "\n"; + index++; + inLineComment = false; + } else { + normalized += char; + inLineComment = char !== "\n"; + } + continue; + } + + if (inBlockComment) { + if (char === "\r" && nextChar === "\n") { + normalized += "\n"; + index++; + } else { + normalized += char; + if (char === "*" && nextChar === "/") { + normalized += nextChar; + index++; + inBlockComment = false; + } + } + continue; + } + + if (char === "-" && nextChar === "-") { + normalized += "--"; + index++; + inLineComment = true; + continue; + } + + if (char === "/" && nextChar === "*") { + normalized += "/*"; + index++; + inBlockComment = true; + continue; + } + + if (char === "'" || char === '"' || char === "`") { + normalized += char; + quoteEnd = char; + continue; + } + + if (char === "[") { + normalized += char; + quoteEnd = "]"; + continue; + } + + if (char === "\r" && nextChar === "\n") { + normalized += "\n"; + index++; + continue; + } + + normalized += char; + } + + return normalized; +} + function splitSqlIntoStatements(sql: string): string[] { const statements: string[] = []; let str = "";