diff --git a/.changeset/d1-crlf-remote-query.md b/.changeset/d1-crlf-remote-query.md new file mode 100644 index 00000000000..780b2a38585 --- /dev/null +++ b/.changeset/d1-crlf-remote-query.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +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. 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 2ced79fc5e7..aad4d26ce27 100644 --- a/packages/wrangler/src/__tests__/d1/execute.test.ts +++ b/packages/wrangler/src/__tests__/d1/execute.test.ts @@ -302,6 +302,60 @@ To continue without logging in, rerun this command with \`--temporary\`. Wrangle expect(std.out).toMatch("🚣 Executed 1 command in 123.46ms"); }); + it("should preserve quoted CRLF when normalizing 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\nchange');\r\nEND;\"" + ); + + 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 ({ expect, }) => { 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 43d2627e678..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, @@ -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 + // structural line endings while preserving quoted SQL values. + const sql = input.command && normalizeSqlLineEndings(input.command); const result = await d1ApiPost( config, accountId, db, "query", - { - sql: input.command, - } + { sql } ); logResult(result); return result; 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 = "";