Skip to content

[wrangler] fix(d1): escape migrationsTableName and filenames in SQLite queries#14394

Merged
jamesopstad merged 2 commits into
cloudflare:mainfrom
Partha-Shankar:fix/d1-escape-migration-identifiers
Jun 23, 2026
Merged

[wrangler] fix(d1): escape migrationsTableName and filenames in SQLite queries#14394
jamesopstad merged 2 commits into
cloudflare:mainfrom
Partha-Shankar:fix/d1-escape-migration-identifiers

Conversation

@Partha-Shankar

@Partha-Shankar Partha-Shankar commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Fixes two SQL escaping issues in D1 migrations.

What changed

Escape migrationsTableName

D1 migration queries previously interpolated migrationsTableName directly into SQLite statements.

This change escapes migration table identifiers using SQL-standard double quotes before constructing queries.

Affected queries include:

  • CREATE TABLE
  • SELECT
  • INSERT

Escape migration filenames

Migration filenames are inserted into SQL string literals when recording applied migrations.

Filenames containing apostrophes could generate invalid SQL. This change escapes single quotes before insertion.

Example:

0001_user's_table.sql

becomes:

0001_user''s_table.sql

when used inside SQL string literals.

Why

These changes improve the robustness of D1 migrations by:

  • supporting custom migration table names that contain special characters
  • preventing SQL parsing failures caused by apostrophes in migration filenames

The changes are fully backward compatible and do not alter existing migration behavior.

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: bug fix

🦦


Open in Devin Review

@changeset-bot

changeset-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2f4f748

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
wrangler Patch
@cloudflare/vitest-pool-workers Patch
@cloudflare/vite-plugin Patch

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

@workers-devprod workers-devprod requested review from a team and jamesopstad and removed request for a team June 23, 2026 06:12
@workers-devprod

workers-devprod commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • ✅ @cloudflare/d1
  • ✅ @cloudflare/wrangler
Show detailed file reviewers

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Jun 23, 2026

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@14394

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@14394

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@14394

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@14394

miniflare

npm i https://pkg.pr.new/miniflare@14394

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@14394

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@14394

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@14394

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@14394

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@14394

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@14394

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@14394

wrangler

npm i https://pkg.pr.new/wrangler@14394

commit: 2f4f748

@alsuren alsuren left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems correct, and matches how we're doing things in dumpSql.ts. Also tested locally.

Ship it.

@workers-devprod workers-devprod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codeowners reviews satisfied

@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk Jun 23, 2026
@jamesopstad

Copy link
Copy Markdown
Contributor

Hi @Partha-Shankar. Thanks for the PR. Please could you add a patch changeset and then we'll be able to get this merged?

@jamesopstad jamesopstad self-requested a review June 23, 2026 11:11
@jamesopstad jamesopstad merged commit 8a5cf8c into cloudflare:main Jun 23, 2026
64 of 65 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants