workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

wrangler d1 migrations apply --local fails with SQLITE_LOCKED while remote D1 succeeds

Open atiqrahmanx opened this issue 3 months ago • 3 comments

What versions & operating system are you using?

System:
  OS: macOS 26.6.1
  CPU: (8) arm64 Apple M1
  Memory: 1.97 GB / 8.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 26.1.0
  npm: 11.17.0
  pnpm: 11.7.0
  bun: 1.3.14
  Deno: 2.9.2
npmPackages:
  wrangler: 4.122.0

The local failure also reproduces on Wrangler 4.112.0 and 4.121.0.

Please provide a link to a minimal reproduction

https://github.com/atiqrahmanx/wrangler-d1-local-sqlite-locked-repro

Describe the Bug

wrangler d1 migrations apply --local deterministically fails with database table is locked: SQLITE_LOCKED on a clean local database, while the same migration files apply successfully to a fresh remote D1 database.

Steps to reproduce:

git clone https://github.com/atiqrahmanx/wrangler-d1-local-sqlite-locked-repro.git
cd wrangler-d1-local-sqlite-locked-repro
pnpm install
pnpm repro

The config uses a dummy all-zero database UUID and a nested Drizzle-style migrations/*/migration.sql layout via migrations_pattern.

On a clean local persistence directory, Wrangler applies and records the first four migrations, then fails on 20260810185351_marvelous_korvac/migration.sql. Repeating the command leaves the first four recorded and fails on that fifth migration again.

For comparison, I created a brand-new remote D1 database in APAC and ran the same five files through wrangler d1 migrations apply --remote using Wrangler 4.121.0. All five succeeded without modification. The final migration executed 194 commands remotely. The resulting remote schema had all 203 tables/indexes declared by the latest Drizzle snapshot and no PRAGMA foreign_key_check violations.

Expected behavior: local D1 should either apply SQL that remote D1 accepts or report a specific documented local incompatibility. A deterministic table lock on a single-process clean migration makes local migration validation report a false deployment failure.

This may share local-D1 locking internals with #14916, but it is not the same failure mode: #14916 concerns concurrent writes causing SQLITE_BUSY and crashing wrangler dev; this reproduction is serial, uses d1 migrations apply --local, and fails deterministically with SQLITE_LOCKED.

Please provide any relevant error logs

⛅️ wrangler 4.122.0
Resource location: local

20260722013352_stale_pestilence/migration.sql       ✅ 211 commands
20260722024523_white_malice/migration.sql           ✅ 4 commands
20260726183531_safe_hellfire_club/migration.sql     ✅ 11 commands
20260801213046_narrow_black_tarantula/migration.sql ✅ 49 commands
20260810185351_marvelous_korvac/migration.sql       pending

✘ [ERROR] database table is locked: SQLITE_LOCKED

Remote comparison:

⛅️ wrangler 4.121.0
Resource location: remote

20260722013352_stale_pestilence/migration.sql       ✅ 211 commands
20260722024523_white_malice/migration.sql           ✅ 4 commands
20260726183531_safe_hellfire_club/migration.sql     ✅ 11 commands
20260801213046_narrow_black_tarantula/migration.sql ✅ 199 commands
20260810185351_marvelous_korvac/migration.sql       ✅ 194 commands

atiqrahmanx avatar Aug 13 '26 03:08 atiqrahmanx

  • Reproduction: ✅ Successfully reproduced. Reproduced exactly as reported. First four migrations apply successfully, then the fifth fails with database table is locked: SQLITE_LOCKED. Re-running fails on the fifth migration again deterministically.

workers-devprod avatar Aug 13 '26 03:08 workers-devprod

[!NOTE] This is an automated, advisory triage report generated by workers-devprod. It is not an official maintainer response — a maintainer will follow up.

🤖 Automated triage report

Issue Triage: cloudflare/workers-sdk#15162

Summary

wrangler d1 migrations apply --local deterministically fails with database table is locked: SQLITE_LOCKED on a clean local database, while the identical migration files apply successfully against a fresh remote D1 database.

Classification

  • Type: Bug
  • Component: packages/wrangler (D1 migrations) / packages/miniflare (local D1 simulation)
  • Severity: Medium
  • Has Reproduction: Yes (with link) — https://github.com/atiqrahmanx/wrangler-d1-local-sqlite-locked-repro
  • Quality: Complete

Findings

  • Created: 2026-08-13
  • Author: atiqrahmanx
  • Version: wrangler 4.122.0 (also reproduces on 4.112.0 and 4.121.0); Node 26.1.0; macOS 26.6.1 (arm64 M1)
  • Labels: none
  • Comments: 1

Analysis

  • High-quality report. Includes full environment output, a clonable minimal reproduction (git clone … && pnpm install && pnpm repro), exact failing migration file (20260810185351_marvelous_korvac/migration.sql), and side-by-side local vs. remote command output. Meets the bug template requirements — no additional info is needed from the reporter.
  • Reproduction already confirmed by the automated triage bot (comment by workers-devprod): the first four migrations apply, the fifth fails with SQLITE_LOCKED, and it fails deterministically on re-runs. This means the issue is verified, not merely reported.
  • Local vs. remote divergence is the core signal: remote D1 accepts all five migrations (the fifth runs 194 commands), so the SQL itself is valid. The failure is specific to the local D1 path — this points at wrangler's d1 migrations apply --local execution and/or Miniflare's local D1/SQLite simulation, rather than user SQL error.
  • Component ownership: D1 migration orchestration lives in packages/wrangler (D1 code is in wrangler), while the underlying local SQLite execution is driven through packages/miniflare's D1 simulation. The root cause could be in either layer (e.g. how the migration transaction/connection is held while applying a large batch of statements).
  • Related but distinct issue: The reporter references #14916 and explicitly distinguishes it — #14916 concerns concurrent writes causing SQLITE_BUSY and crashing wrangler dev, whereas this is a serial d1 migrations apply --local run failing deterministically with SQLITE_LOCKED. Not a duplicate, but likely shares local-D1 locking internals and is worth cross-linking.

Recommendation

Status: KEEP OPEN

Reasoning: This is a well-formed, reproduced bug with a minimal repro, and the automated triage already confirmed the failure. The local-only failure (remote succeeds) indicates a genuine defect in wrangler/Miniflare's local D1 migration path rather than user error, so it warrants investigation.

Action: Route to the D1 / local-dev owners. Reproduce from the linked repo, determine whether the lock originates in wrangler's migration apply logic (transaction/connection handling for --local) or in Miniflare's local D1/SQLite layer, and cross-link with #14916 as a possibly-related locking bug.

Suggested Labels: none

Suggested Comment

Thanks for the detailed report and the minimal reproduction — that's very helpful. We've confirmed this reproduces exactly as described: the first four migrations apply and the fifth fails deterministically with database table is locked: SQLITE_LOCKED on --local, while the same files succeed against remote D1. This looks like a genuine issue in the local D1 migration path rather than a problem with your SQL, so we'll keep this open to investigate. We'll also cross-reference #14916 since the two may share local-D1 locking internals, even though the failure modes differ.

Structured summary (JSON)
{
  "issueNumber": 15162,
  "title": "wrangler d1 migrations apply --local fails with SQLITE_LOCKED while remote D1 succeeds",
  "githubUrl": "https://github.com/cloudflare/workers-sdk/issues/15162",
  "recommendation": "KEEP OPEN",
  "difficulty": "medium",
  "reasoning": "Well-formed, reproduced bug with a minimal repro; the automated triage confirmed the deterministic SQLITE_LOCKED failure. Local fails while remote succeeds, indicating a genuine defect in wrangler/Miniflare's local D1 migration path rather than user error.",
  "suggestedAction": "Route to D1/local-dev owners; reproduce from the linked repo, determine whether the lock originates in wrangler's migration apply logic or Miniflare's local D1/SQLite layer, and cross-link with #14916.",
  "hasSuggestedComment": true,
  "suggestedLabels": []
}

workers-devprod avatar Aug 13 '26 03:08 workers-devprod

Fixed in #15712

cjol avatar Sep 25 '26 14:09 cjol