Skip to content

Lesson 7 of 9 · 27m

SQLite Migrations That Survive Reality

Use user_version, transactional migration steps, additive changes, and validation rehearsals to evolve application databases without bricking installs.

A migration system for SQLite is not optional infrastructure. Embedded databases live on machines you do not control, under application versions you cannot force users to run, sometimes interrupted by laptops closing or phones being killed mid-update. The migration has to be boring, ordered, transactional, and recoverable.

Use PRAGMA user_version as the coarse schema marker

SQLite reserves a user-controlled integer in the database header:

PRAGMA user_version;
PRAGMA user_version = 7;

This is not a complete migration framework, but it is a robust version marker when used consistently. Each application release knows the latest schema version. Startup compares the database version and runs missing migrations in order.

Every migration should be atomic

A migration is a transaction, not a vibes-based sequence of DDL statements:

BEGIN IMMEDIATE;

ALTER TABLE jobs ADD COLUMN priority INTEGER NOT NULL DEFAULT 0;
CREATE INDEX jobs_pending_priority
ON jobs(priority, created_at)
WHERE status = 'pending';

PRAGMA user_version = 8;
COMMIT;

SQLite supports transactional DDL better than many people expect. Use it. If the migration fails, roll back and leave the database at the previous coherent version.

Prefer additive migration paths

Additive changes are easier to deploy across rolling application versions: add nullable columns, add columns with defaults, create new tables, create indexes. Destructive changes are risky because older application versions may still run against the file, especially in desktop and mobile environments.

When a destructive change is necessary, stage it. First stop writing to the old shape. Backfill and validate the new shape. Only then remove the old column or table in a later release after rollback compatibility is no longer required.

ALTER TABLE has limits

SQLite's ALTER TABLE supports useful operations such as adding columns, renaming tables, and renaming columns. It is not a general arbitrary schema transformation engine. For complex changes, the reliable pattern is:

BEGIN IMMEDIATE;
CREATE TABLE jobs_new (...);
INSERT INTO jobs_new SELECT ... FROM jobs;
DROP TABLE jobs;
ALTER TABLE jobs_new RENAME TO jobs;
PRAGMA user_version = 9;
COMMIT;

Do this only after checking foreign keys, triggers, indexes, views, and application SQL. SQLite foreign key enforcement is controlled per connection by PRAGMA foreign_keys; historically many applications accidentally ran with it off. Migration assumptions should say which mode is active.

Test migration paths, not just fresh schemas

A fresh database created by version 10 is not proof that version 6 upgrades correctly. Keep fixture databases at older versions and test real upgrade paths:

cp fixtures/app-v6.sqlite /tmp/upgrade-test.sqlite
./myapp migrate /tmp/upgrade-test.sqlite
sqlite3 /tmp/upgrade-test.sqlite 'PRAGMA integrity_check; PRAGMA user_version;'

Also test interruption. Kill the process mid-migration in a lab and verify recovery behavior. SQLite transactions protect atomicity, but your orchestration still needs to retry or fail safely rather than half-registering application-level state elsewhere.

Field notes

  • user_version is a marker, not proof that every index and trigger is correct; verify critical structure too.
  • Run PRAGMA foreign_keys = ON on every connection that needs enforcement.
  • Keep old fixture databases in the repository or artifact store for upgrade testing.
  • Do not assume a mobile app updates atomically with its database migration.
  • Interruption testing is part of migration design, not exotic paranoia.
  • A migration that takes minutes needs progress, locking, and operational thought — especially on user devices.

Was this lesson helpful?

Sign in to rate