Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/d1-crlf-remote-query.md
Original file line number Diff line number Diff line change
@@ -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.
54 changes: 54 additions & 0 deletions packages/wrangler/src/__tests__/d1/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) => {
Expand Down
19 changes: 18 additions & 1 deletion packages/wrangler/src/__tests__/d1/splitter.test.ts
Original file line number Diff line number Diff line change
@@ -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", ({
Expand Down
11 changes: 7 additions & 4 deletions packages/wrangler/src/d1/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<QueryResult[]>(
config,
accountId,
db,
"query",
{
sql: input.command,
}
{ sql }
);
logResult(result);
return result;
Expand Down
92 changes: 92 additions & 0 deletions packages/wrangler/src/d1/splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
Loading