Migrations
Run migrations at startup, once across replicas, under a lock the store takes on a dedicated connection.
Migrations never run implicitly. .migrate(migrator) on a store module runs them in the module's init hook, after connecting, under a lock. MongoDB and Redis have no migrator and no .migrate.
m.import(store.migrate(sqlx::migrate!("./migrations")));create table notes (
id bigserial primary key,
title text not null
);The migrator type depends on the store.
| Store | Migrator |
|---|---|
| SQLx | sqlx::migrate::Migrator, from sqlx::migrate! |
| SeaORM | SeaOrmMigrations::<Migrator>::new() over a sea-orm-migration Migrator |
| Diesel | DieselMigrations::new(MIGRATIONS) over a const holding diesel_migrations::embed_migrations!() |
One lock for every library
Two replicas started together would otherwise both run the migrations. SQLx's Migrator::run takes its own lock on Postgres and MySQL and none on SQLite. sea-orm-migration and diesel_migrations take none. On Postgres the second runner then fails on the version table's primary key, and on an engine without transactional DDL a migration body with side effects can run twice.
ulo-db takes one lock, whatever the migrator. The second replica waits and finds nothing pending, rather than failing its start.
| Engine | Lock |
|---|---|
| Postgres | pg_advisory_lock on a connection held for the migration. |
| MySQL | GET_LOCK named for the database, with the wait's bound as its timeout. |
| SQLite | An OS lock on <file>.ulo-migrate.lock, through std::fs::File::lock. The sidecar file is left in place. An in-memory database needs none. |
SQLx's own lock nests inside this one and never waits on it.
Bounds
There are two waits, each with a bound of its own:
- The wait for the lock is bounded by
connect_within, 30 seconds by default, counted from the request. Running out is a retryable failure: another replica is migrating, and a restart after it finishes succeeds. - The migrations are bounded by
.migrate_within(Bound)from the moment the lock is granted.Bound::DefaultandBound::Unboundedset none, andBound::After(d)fails the start permanently when the migrations run pastd. Zero is refused atwire().
The init hook's timeout is set to cover both, so the application's hook_timeout never cuts a migration.
A failure is ulo_db::MigrationFailed { module, source }. is_retryable() says whether retrying the start can mend it, and waited_for_lock() whether it was the lock wait. A migration that fails, a bound that passes and a task that panics are permanent.
Requirements and hazards
- On Postgres and MySQL the lock holds one pooled connection while the migrator takes another, so
max_connectionsmust be at least 2 when.migrate(..)is written.ulo-db-sqlxrefuses a pool of one, naming the setting. - A migration using
CREATE INDEX CONCURRENTLYcan deadlock against a session waiting on the advisory lock. - The SQLite wait polls the file lock and makes its last attempt 10 ms before the bound. A lock freed in those last 10 ms is missed by design: the start fails with the retryable lock timeout, and the restart takes the lock.
.migrate(..)on a.lazy()module is refused atwire(). A lazy store runs no migrations, so the schema is the deployment's.- Two imports of one store with different
.migrate(..)calls are different settings and are refused asDifferingSettings. A store built by one function that both importers call is not.