Skip to content

[wrangler] D1 SQL splitter does not treat bracket-quoted identifiers as quoted #15228

Description

@MatheusMartinho

Which Cloudflare product(s) does this pertain to?

Wrangler core, D1

What versions are you using?

main (reproduced at 898dc6e)

Describe the Bug

splitSqlIntoStatements() in packages/wrangler/src/d1/splitter.ts scans for quotes so that SQL syntax inside them is not interpreted. It handles ', " and backticks, but not SQLite's bracket-quoted identifiers ([name]).

As a result a trigger body that references such an identifier is split apart. Given:

CREATE TRIGGER t AFTER INSERT ON items
BEGIN
 UPDATE x SET [end] = 1;
 UPDATE y SET z = 2;
END;
SELECT 1;

splitSqlQuery() returns four fragments instead of two:

CREATE TRIGGER t AFTER INSERT ON items\nBEGIN\n UPDATE x SET [end] = 1
UPDATE y SET z = 2
END
SELECT 1

Each fragment is then sent to D1 separately, so wrangler d1 execute --file and wrangler d1 migrations apply fail or apply a migration partially.

Notably normalizeSqlLineEndings(), in the same file, does treat [] as a quote pair, so the two scanners disagree about the same syntax.

Please provide a link to a minimal reproduction

The snippet above reproduces directly through splitSqlQuery(); no account or network access is needed.

Additional context

Found while working on #15226, which fixes a different splitter issue (compound statement markers that are not padded with whitespace). This one is pre-existing and independent — it reproduces identically before and after that change — and needs a fix in the scanner rather than in the marker regexes, so it is filed separately. Happy to send a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting-response:reporterAwaiting response from OP for further clarificationneeds-reproductionNeeds reproduction from OPpackage:wranglerRelating to the `wrangler` packageproduct:d1Relating to Cloudflare D1: https://developers.cloudflare.com/d1/

    Type

    No type

    Projects

    Status
    Untriaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions