Repository navigation
Conversation
🦋 Changeset detectedLatest commit: a2a85c4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
|
@alsuren Bonk workflow failed. Check the logs for details. View workflow run · To retry, trigger Bonk again. |
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/codemods
@cloudflare/config
@cloudflare/containers-shared
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/runtime-types
@cloudflare/test-server
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-plugin
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
There was a problem hiding this comment.
Note
This report is out of date. Scroll down for Devin Review's latest report on this PR.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
dario-piotrowicz
left a comment
There was a problem hiding this comment.
Generally looks ok to me, besides the one small thing I commented on
Also the CI seems quite red and there's a merge conflict 🥲
|
|
||
| // Taken from SQLite shell.c.in https://github.com/sqlite/sqlite/blob/105c20648e1b05839fd0638686b95f2e3998abcb/src/shell.c.in#L8463-L8469 | ||
| // @ts-expect-error -- SqlStorageStatement needs to be callable | ||
| // @ts-ignore (SqlStorageStatement needs to be callable) |
There was a problem hiding this comment.
why this change? generally @ts-expect-error is preferred
There was a problem hiding this comment.
Oh I see what might happened. This file is vendored into the d1 codebase, and I fixed it there first. I then just copied the file over here and reverted all of the changes that were made by others.
It seems like there are other changes, so I will need to copy them back over at some point.
At the time this file was written, there wasn't a good package to put a file that could be imported into the d1-worker durable object code, so the file was vendored in both places and kept in sync by hand (or not, as I found out when I joined and changes had been made in both places).
I feel like things might have changed since then. Is there somewhere that we could move this file so that we can import it from our DO code and also import it from miniflare, to avoid this?
33217f3 to
0fdb6e7
Compare
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
…es in export If newline and backslash n are both taken, it picks a new string to use
0fdb6e7 to
e8825e6
Compare
|
Codeowners approval required for this PR:
Show detailed file reviewers |
workers-devprod
left a comment
There was a problem hiding this comment.
Codeowners reviews satisfied
There was a problem hiding this comment.
💬 Approved with comments — The ported escaping logic is correct. There are two non-blocking notes: no regression test in this repository, and a small wording tweak in the changeset.
Findings: 2 info (2 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
Logic matches SQLite's algorithm; no regression test in this repository.
Recommendation: 💬 Optional Nits
- [INFO] No regression test in this repository for the newline/backslash-n collision fix; the existing miniflare D1 dumpSql suite could cover it. (
packages/miniflare/src/workers/d1/dumpSql.ts:206)
View full review
The new unusedString and outputQuotedEscapedString logic matches SQLite's algorithm:
- The collision check runs on the original value.
- The fallback format
(\n0)is the same as SQLite's. - Quote doubling does not interfere with the placeholders.
- The
replace()nesting order is the same.
The only note is that this repository has no test for the fix. packages/miniflare/test/plugins/d1/suite.ts already has dumpSql round-trip tests, and a case for this would fit there.
📝 Documentation: 💬 1 info
Minor wording clarification in the changeset.
Recommendation: 💬 Optional Nits
- [INFO] Clarify that
\nin the changeset means the literal two-character sequence. (.changeset/two-lamps-leave.md:7)
View full review
The changeset is accurate. Saying "literal \n" would make it clearer which input was affected.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns. The placeholders contain no single quotes, so the generated SQL literals stay properly quoted.
Reviewed commit: a2a85c4d
| // Ported `output_quoted_escaped_string` from https://github.com/sqlite/sqlite/blob/master/src/shell.c.in#L1799-L1862 | ||
| // Ported from SQLite's pre-unistr() `output_quoted_escaped_string` in shell.c.in. | ||
| // https://github.com/sqlite/sqlite/blob/dd2deecbbd03ec1478701b2029479c4983d8fae7/src/shell.c.in#L1959-L2015 | ||
| function outputQuotedEscapedString(cell: unknown) { |
There was a problem hiding this comment.
ℹ️ INFO · Code Quality
No regression test in this repository for the newline/backslash-n collision fix; the existing miniflare D1 dumpSql suite could cover it.
This changes the SQL emitted for any text cell that contains \n or \r, but none of the tests in this repository cover it. Tests elsewhere don't run in this repository's CI, so a future change to this copy could break it without anyone noticing. Consider adding a round-trip case to the dumpSql tests in packages/miniflare/test/plugins/d1/suite.ts: a real newline together with a literal \n, a value that also contains \012 (to hit the (\n0) fallback), and the same cases for \r.
|
|
||
| Fix newline escaping edge-case for `wrangler d1 export --local`. | ||
|
|
||
| Previously, if you had a text field with both a real newline and a '\n' escaped newline, it would output both as real newlines. |
There was a problem hiding this comment.
ℹ️ INFO · Documentation
Clarify that \n in the changeset means the literal two-character sequence.
"a '\n' escaped newline" could be read as a second real newline rather than the literal backslash-n characters that collide with the placeholder. Wording it as "a literal \n character sequence" would make the fixed edge case clearer to readers of the changelog.
| const newlinePlaceholder = hasNewline | ||
| ? unusedString(value, "\\n", "\\012") | ||
| : ""; | ||
| const carriageReturnPlaceholder = hasCarriageReturn | ||
| ? unusedString(value, "\\r", "\\015") | ||
| : ""; | ||
|
|
||
| // Function to replace ' with '', while also tracking whether the string contains any \r or \n chars | ||
| const escapeQuotesDetectingNewlines = (_: string, lf: string, cr: string) => { | ||
| if (lf) { | ||
| lfs = true; | ||
| return `\\n`; | ||
| } | ||
| if (cr) { | ||
| crs = true; | ||
| return `\\r`; | ||
| } | ||
| return `''`; | ||
| }; | ||
| const escaped = value | ||
| .replaceAll("'", "''") | ||
| .replaceAll("\n", newlinePlaceholder) | ||
| .replaceAll("\r", carriageReturnPlaceholder); |
There was a problem hiding this comment.
🔴 Unrestorable exports for multiline text
For a large multiline value containing literal \n, outputQuotedEscapedString can push the INSERT over workerd’s 100,000-byte limit. Export succeeds, but local restore rejects the file.
Learn more
Local export writes the generated INSERT statements without checking whether they fit the pinned workerd runtime’s 100,000-byte SQL statement limit. A bound value can fit in the database even when writing it out as a SQL literal exceeds that limit. The new fallback uses four or more bytes per newline rather than two. Local restore splits the file into statements and submits each INSERT whole, so the added bytes can make SQLite reject it.
Example: Bind and insert a text value containing 3,700 lines of warning: retrying job 047 followed by format: \n with a literal backslash. The value occupies 96,210 bytes. The old export emitted a 99,972-byte INSERT that ran but changed the literal \n. The new export emits a 107,374-byte INSERT that SQLite rejects with the workerd limit. An unused one-byte marker preserves the same input in a 96,271-byte INSERT.
Recommended fix: Check the full INSERT’s UTF-8 byte length when a collision expands it. If it crosses the statement limit, try a more compact, collision-free representation, such as an unused single-byte printable marker. Verify that the resulting SQL fits and round-trips the exact value through local export and execution.
Was this helpful? React with 👍 or 👎 to provide feedback.
If newline and backslash n are both taken, it picks a new string to use
Fixes #9042
Describe your change...
Ports the sqlite3 .dump algorithm a bit more faithfully, by checking for collisions in proposed escape sequences and then generating new escape sequences until it finds one that doesn't collide.
Extended tests pass in internal d1-worker copy of this change.
A picture of a cute animal (not mandatory, but encouraged)
https://freesvg.org/rabbit-line-art (I hope you like what I did there :D)