ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

Integrations

DatabaseConnectingMigrationsSeveral instancesSQLxSeaORMDieselMongoDBRedis

Operating

Reference

Database

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.

examples/35-db-sqlx/src/lib.rs
m.import(store.migrate(sqlx::migrate!("./migrations")));
examples/35-db-sqlx/migrations/0001_notes.sql
create table notes (
    id bigserial primary key,
    title text not null
);

The migrator type depends on the store.

StoreMigrator
SQLxsqlx::migrate::Migrator, from sqlx::migrate!
SeaORMSeaOrmMigrations::<Migrator>::new() over a sea-orm-migration Migrator
DieselDieselMigrations::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.

EngineLock
Postgrespg_advisory_lock on a connection held for the migration.
MySQLGET_LOCK named for the database, with the wait's bound as its timeout.
SQLiteAn 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::Default and Bound::Unbounded set none, and Bound::After(d) fails the start permanently when the migrations run past d. Zero is refused at wire().

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_connections must be at least 2 when .migrate(..) is written. ulo-db-sqlx refuses a pool of one, naming the setting.
  • A migration using CREATE INDEX CONCURRENTLY can 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 at wire(). 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 as DifferingSettings. A store built by one function that both importers call is not.

Connecting

How a store module waits for its database, which failures stop it at once, and when to start without one.

Several instances

Two databases of one kind in one application, told apart by a qualifier type.

On this page

One lock for every libraryBoundsRequirements and hazards