# Schema migrations across thousands of per-user SQLite databases

Source: https://crabtalk.ai/blog/schema-migrations-per-user-databases
Published: 2026-11-25

> How to run schema migrations across thousands of per-tenant SQLite databases: at deploy, lazily on next open, or from a central schema, and what breaks.

A database per user or per tenant isolates each customer's data, and it turns one schema migration into thousands. A deploy that adds a column now has to change every tenant's database, some of which haven't been opened in months, and any one of which can fail. There are three ways to do it: migrate every database at deploy, migrate each one lazily the next time it's opened, or push changes from a central schema. This post compares them, with the SQLite details that decide what a migration can do.

## Track the version in the database

Each database has to know which migrations it has. SQLite gives every database a spare integer for this: "The user-version is an integer that is available to applications to use however they want", and "SQLite makes no use of the user-version itself" ([PRAGMA user_version](https://www.sqlite.org/pragma.html#pragma_user_version)).

```sql
PRAGMA user_version;        -- 3
-- apply 004.sql, then:
PRAGMA user_version = 4;
```

Run the migration and the version update in one transaction, so a database is never half-migrated with a version that says otherwise.

## Know what SQLite's ALTER TABLE can do

[ALTER TABLE](https://www.sqlite.org/lang_altertable.html) renames a table, renames a column, adds a column and drops a column, with limits: an added column "may not have a PRIMARY KEY or UNIQUE constraint", and a `NOT NULL` column needs a non-null default. Since SQLite 3.53.0, `ALTER COLUMN` can also set or drop `NOT NULL` on an existing column. Most other changes, such as changing a column's type, reordering columns or adding a `UNIQUE` constraint, take SQLite's twelve-step rebuild: create the new table, copy the rows, drop the old one and rename the new one, inside a transaction with foreign keys off.

A rebuild copies every row. On one large database that is a pause; across thousands of small ones it is a lot of writes, so prefer migrations that add rather than reshape.

## Strategy 1: migrate everything at deploy

A deploy step loops over every tenant database and migrates each one, and the new code serves traffic once it finishes. Schema-per-tenant frameworks provide the loop as a command; ordering it before the traffic switch is up to your pipeline. In Rails' [apartment](https://github.com/influitive/apartment), `rake db:migrate` runs migrations "for each tenant name supplied from `Apartment.tenant_names`"; [django-tenants](https://django-tenants.readthedocs.io/en/latest/use.html)'s `migrate_schemas` runs on all tenants, optionally across processes, with a maximum of 2 by default "to avoid exhausting the database connection pool."

- **Good:** once the run finishes, every database is on the new schema, and the code needs to handle only one.
- **Costs:** the deploy takes time proportional to the number of tenants, and opens databases that would otherwise stay idle. One tenant's failure has to either stop the deploy or leave that tenant behind.

## Strategy 2: migrate lazily on next open

The code checks the version when it opens a tenant's database and applies whatever is missing. A tenant who never comes back is never migrated, and the deploy has no per-tenant migration loop.

SQLite-backed Durable Objects suit this pattern: each object has its own database, and an object that starts on new code can migrate it before serving requests, using `blockConcurrencyWhile()`, which "ensures no requests are delivered until initialization completes" ([Durable Objects storage](https://developers.cloudflare.com/durable-objects/best-practices/access-durable-objects-storage/)). That call resets the object if it runs past 30 seconds ([blockConcurrencyWhile](https://developers.cloudflare.com/durable-objects/api/state/#blockconcurrencywhile)), which matters for a migration that rebuilds a large table.

- **Good:** migration work follows use. Deploy time no longer grows with the number of tenants, idle tenants are never migrated, and a failure affects one tenant at the moment they return.
- **Costs:** at any time the fleet runs several schema versions. Anything that reads databases without opening them through the migration path, such as a report or an export, has to handle old versions.

## Strategy 3: push from a central schema

One schema database is the source of truth, and the platform applies each change to every database that references it. Turso's multi-DB schemas worked this way: a change runs as a dry run first, then as a task per child database, and "if all tasks succeed, the migration is applied to the schema database itself." While it runs, "all databases are locked to write operations." The feature is now "deprecated for all new users" ([Turso docs](https://docs.turso.tech/features/multi-db-schemas)).

- **Good:** one change, applied by the platform to every database, with a dry run first.
- **Costs:** writes stop everywhere for the duration, and the fleet moves at the pace of its slowest database. The docs tell applications to handle child databases "not yet updated with the schema", so several versions can still be live during a change.

## Comparing the three

| | At deploy | Lazily on open | Central schema |
| --- | --- | --- | --- |
| Deploy time | Grows with tenants | Constant | Grows with tenants |
| Idle tenants | Migrated anyway | Never touched | Migrated anyway |
| Versions running at once | One, once the run finishes | Several | Several, during a change |
| A failure affects | The deploy, or one tenant left behind | One tenant, when they return | The change; rollback isn't documented |

## Rules that hold for all three

- **Make every migration backward-compatible with the code before it.** Add columns and tables; drop them in a later release once nothing reads them.
- **One migration, one transaction**, version update included.
- **A failed migration must not lose data.** Leave the database readable at its old version, and report which tenant failed.
- **Watch the versions.** You should be able to answer how many tenants are on each schema version.

## Migrations for caller storage on CrabTalk

CrabTalk's [caller storage](/product/caller-storage) gives each calling account its own SQLite database, and migrates them lazily. A service keeps numbered migrations beside its `crab.toml`:

```toml
[storage.caller]
migrations = "caller"
```

```sql
-- caller/002.sql
ALTER TABLE preferences ADD COLUMN updated_at INTEGER;
```

- **On next writable open.** After a deploy, the host applies new migrations when a caller's database is next opened with writes allowed, which can be a call that only reads. A call opened under a read-only storage allowance, such as an account over its storage limit, uses the schema the database already has, so read paths must handle the previous version.
- **Failures stay contained.** A migration whose SQL fails leaves that caller's data available for them to download, and sends the error to the service's logs.
- **Versions are visible.** The service's Callers tab, and `crab callers owner/service`, show each caller's database size, last call and schema version.

The full guide is in [Caller storage](/docs/development/caller-storage). For choosing between a shared database and one per user in the first place, see [Database per tenant](/blog/database-per-tenant-shared-schema-or-database).
