@sqlite-development

Design, debug, and migrate production SQLite databases. Configure WAL concurrency, enforce foreign keys and STRICT tables, sync FTS5 indexes, and rebuild schemas safely.

SQLite Development

When to Use

  • Configure SQLite for concurrent web, CLI, or desktop workloads and diagnose SQLITE_BUSY failures.
  • Enforce relational integrity and storage types with PRAGMA foreign_keys = ON and STRICT tables.
  • Maintain FTS5 full-text search indexes or execute SQLite schema migrations that require table rebuilds.

Prerequisites

Identify the SQLite library version (sqlite3.sqlite_version or SELECT sqlite_version()), driver/binding, concurrency pattern (single process vs multi-process, read/write ratio), and storage medium (local filesystem vs network mount). Inspect existing connection setup code, pragma defaults, and migration scripts. Avoid placing active WAL-mode SQLite databases on network filesystems where shared-memory locking does not work reliably.

Configure every connection intentionally

  1. Enable PRAGMA foreign_keys = ON on every new connection. SQLite defaults this setting to OFF per connection even when tables declare REFERENCES constraints. Run PRAGMA foreign_key_check after bulk loads or legacy repairs.
  2. Enable PRAGMA journal_mode = WAL and set a per-connection PRAGMA busy_timeout that fits the workload latency budget. With WAL and PRAGMA synchronous = NORMAL, an OS crash or power failure can roll back recently committed transactions. Use FULL when those commits must survive.
  3. Use BEGIN IMMEDIATE for transactions that will write. A default BEGIN DEFERRED transaction that reads first and upgrades to a write while another writer holds the lock fails immediately with SQLITE_BUSY without waiting for busy_timeout.
  4. Keep write transactions short and keep long read transactions from starving WAL checkpoints. Monitor WAL file growth and run explicit PRAGMA wal_checkpoint(TRUNCATE) during maintenance windows when needed.

Enforce types, indexes, and FTS5 sync

  • Declare new tables STRICT (SQLite 3.37+) when dynamic type affinity is undesirable, so string values cannot silently enter INTEGER columns.
  • Store timestamps and money consistently (for example ISO-8601 UTC strings or Unix epoch integers, and integer minor units such as cents) and enforce invariants with CHECK constraints.
  • For FTS5 external-content tables (content='table_name'), install matching AFTER INSERT, AFTER DELETE, and AFTER UPDATE triggers to keep the search index synchronized. When attaching FTS5 to an already-populated table, run an initial backfill (INSERT INTO <fts_table>(<fts_table>) VALUES('rebuild')) inside the setup transaction so pre-existing rows are indexed and later updates/deletes do not fail with SQLITE_CORRUPT_VTAB, then verify search and update behavior on a pre-existing row.

Run table-rebuild migrations safely

  • SQLite ALTER TABLE supports renaming tables/columns, adding columns, and dropping simple columns, but changing column types, nullability, CHECK constraints, or foreign keys requires the documented 12-step table-rebuild pattern.
  • Back up the database before destructive DDL (VACUUM INTO or SQLite's online backup API rather than copying a live WAL database with raw file copy while writers are active).
  • SQLite ignores PRAGMA foreign_keys changes inside an active transaction, so disable foreign keys (PRAGMA foreign_keys = OFF) before BEGIN. Inside the transaction, record existing indexes, triggers, and dependent views from sqlite_schema, drop dependent views before dropping the old table and renaming the replacement (otherwise ALTER TABLE ... RENAME TO fails view validation with no such table), recreate the table, copy rows explicitly by column list, drop the old table, rename the new table, recreate indexes/triggers/views, and run PRAGMA foreign_key_check before COMMIT. Restore PRAGMA foreign_keys = ON after COMMIT or ROLLBACK (outside the transaction).

Example

An application using SQLite silently stored orphan rows, accepted string prices in an integer column, and hit SQLITE_BUSY under concurrent writes.

Run the worked WAL concurrency and integrity test to reproduce each failure mode and verify per-connection foreign_keys, STRICT tables, and BEGIN IMMEDIATE serialization under WAL mode.

Limitations

  • Single-writer concurrency scales well for read-heavy and moderate-write single-node workloads, not multi-node horizontal write scaling.
  • STRICT tables require SQLite 3.37.0 or newer; check sqlite_version() before emitting STRICT DDL.
  • WAL mode requires shared memory on a local filesystem and is unsuitable for NFS or SMB shares.

Safety and Side Effects

Test schema rebuilds, checkpoints, and VACUUM on a copy of the database first. Use VACUUM INTO or the online backup API for a consistent live copy. For a raw file copy, close every connection and keep writers stopped until the copy is complete. Keep production paths and user records out of examples and logs.