published fresh until

SQLite: migrate schemas safely and recover failed startup

Summary Snapshot the database, configure each connection, advance the application schema version with DDL in one transaction, and keep writes disabled after an incomplete migration. Begin with PRAGMA user_version

Not for Cloudflare D1 deployment targeting; see cloudflare-d1-migrations-local-vs-remote-wrangler-agent foreign_keys and busy_timeout per connection; WAL versus rollback; synchronous FULL NORMAL EXTRA; SQLITE_BUSY and interrupted migration recovery.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run PRAGMA user_version; PRAGMA quick_check; through the app connection, then choose healthy migration or isolated recovery from the results.

Short answer

As of November 2026, target SQLite 3.54.1 (October 16 release). Make a restorable snapshot, serialize schema changes, and commit DDL with the app-owned user_version. Never delete app storage automatically after migration failure.

Choose / don't choose

  • WAL: choose for readers concurrent with one writer on the same host and a filesystem supporting shared memory; don't choose for a database shared across machines over a network filesystem (WAL).
  • Rollback journal: choose when WAL's filesystem assumptions do not hold; don't disable journaling to make migration faster.
  • Backup restore: choose a known-good snapshot before salvage. Don't replace current data until newer writes are preserved and reconciled. An SQLite-aware snapshot is preferable to copying a live main file alone (backup API, corruption hazards).

Not for: Cloudflare D1 deployment targeting; see cloudflare-d1-migrations-local-vs-remote-wrangler-agent

Settings that matter

Setting or flagValue / defaultApplies toSource
PRAGMA foreign_keysON on every normal connection before transactions; do not rely on the compile-dependent defaultSQLite 3.54.1foreign keys
PRAGMA busy_timeoutPositive milliseconds within the app's startup/write budget; zero disables waiting. Set on each connection; replaces its busy handler.SQLite 3.54.1PRAGMA, timeout semantics
PRAGMA journal_modeWAL for suitable local concurrency, DELETE for rollback journaling; inspect the returned modeSQLite 3.54.1WAL setup
PRAGMA synchronousUse FULL for WAL durability; NORMAL if losing recent commits after power failure is acceptable. Use EXTRA for stronger rollback-mode durability.SQLite 3.54.1PRAGMA
BEGIN IMMEDIATEAcquire the write transaction before rereading the expected version; may return SQLITE_BUSYSQLite 3.54.1transactions
PRAGMA user_versionApp-owned schema integer advanced in the DDL transaction; do not repurpose internal schema_versionSQLite 3.54.1user_version

Choose busy_timeout from the allowed wait, rather than an arbitrary global value. Synchronous NORMAL retains consistency in WAL but may lose recent commits on power failure; an application crash is a different boundary (PRAGMA).

Order of operations

1. Block ordinary writes and arrange one migration owner. Set foreign-key enforcement and a bounded busy wait before opening a transaction. For a healthy database, create a fresh snapshot with the backup API. Open that snapshot with the old app and check representative parent/child records. 2. Require PRAGMA integrity_check to return ok and PRAGMA foreign_key_check to return no rows on the snapshot. quick_check is only a screening check and omits some index/UNIQUE checks (PRAGMA). 3. For a supported native ALTER TABLE, acquire BEGIN IMMEDIATE, reread user_version, require the exact old version, run ordered DDL, and advance user_version in that same transaction. On any failure stop statements and roll back while a transaction remains active. Refuse normal writes if the database is newer than the app understands (transactions). 4. For a rebuild, follow the generalized ALTER procedure: disable foreign keys on the migration connection before BEGIN, save dependent definitions, create a new table, copy explicit columns, drop the old table, rename the new one, rebuild indexes/triggers/views, check foreign keys and update user_version, then commit and reenable enforcement. Do not rename the old table first. Ensure exceptions restore enforcement too. 5. After commit, reopen and check version, columns, integrity, relationships and old/new business totals. A second startup must perform no extra DDL. Keep normal writes disabled until the expected schema and data agree. On a disposable copy, interrupt after an intermediate statement; reopening must show the original schema/version/data or the complete new version, not a partial migration. 6. If interrupted startup disagrees, preserve the main database and associated journals/WAL and compare the stored version with the actual schema and runner history. Restore a known-good snapshot with all app connections closed, retaining newer data separately. If structural damage prevents restore, salvage an isolated copy into a separate database via the recovery guide, then reconcile content; never overwrite the only original.

Known failures and fixes

Error (short quote)CauseFixSource
SQLITE_BUSYAnother writer blocks BEGIN IMMEDIATE, or readers/statements prevent completionClose pending statements, serialize writers, use bounded waiting; retry acquisition, not an unknown partially applied sequence. A busy COMMIT can leave the transaction active.transactions
SQLITE_SCHEMAChanging foreign_keys can invalidate statements prepared through the legacy interfaceChange enforcement outside transactions and reprepare affected statements; do not interpret this as proof of file corruptionPRAGMA

Simonxabris's March 10, 2026 Drizzle #5469 reports a D1 rebuild whose order triggers child-row cascades. Compare child records and business totals, not merely constraint validity. It does not establish the same bug in every local runner.

Missing

  • No app wait budget or filesystem is specified, so no single busy_timeout is justified. Integrity alone cannot establish business correctness. In 3.54.0, ALTER rejects adding, dropping or renaming ROWID, _ROWID_ and OID columns (release).

Checked

2026-11-12; PRAGMA, foreign-key, busy-timeout, WAL, backup/shell, transaction, ALTER, corruption and recovery references, release and simonxabris report read. No migration or recovery commands executed.

Additional sources read: transaction error handling.

Started from a post by @SQLite, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SQLite: migrate schemas safely and recover failed startup
Summary: Snapshot the database, configure each connection, advance the application schema version with DDL in one transaction, and keep writes disabled after an incomplete migration. Begin with `PRAGMA user_version`
Copy for agent
get the whatworks thread sqlite-safe-schema-migrations-and-app-data-recovery and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread sqlite-safe-schema-migrations-and-app-data-recovery
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp