Skip to content

Migrator: advisory lock, lock timeout and tolerating unknown applied migrations #3265

Closed as not planned

Description

@patrick-immometrica

Motivation

When an app runs its migrations at start-up (common with containers and rolling deploys), Migrator::up has three gaps that every such app has to work around:

  1. No lock. Two instances starting at the same time can both see a migration as pending and run it twice.
  2. No bounded wait. A migration that needs a lock held by a long-running query waits indefinitely and blocks everything queued behind it.
  3. Unknown applied migrations are an error. After a rollback to the previous release, seaql_migrations contains a migration the older binary doesn't know, and up refuses to start, even though expand-only migrations are designed to be compatible with the release before.

Today the workaround is to reimplement the runner on top of MigrationTrait, SchemaManager and seaql_migrations.

Proposal

Options on the migrator, e.g.:

Migrator::up_with(db, MigrateOptions {
    lock: true,                          // Postgres: pg_advisory_lock; MySQL: GET_LOCK
    lock_timeout: Some(Duration::from_secs(5)), // per migration, e.g. SET LOCAL lock_timeout
    allow_unknown_applied: true,         // skip instead of erroring
    ..Default::default()
}).await?;

Defaults keep today's behaviour, so nothing changes for existing users.

Happy to send a PR if this direction is acceptable.

Activity

  1. patrick-immometrica commented on Nov 15, 2026

    @patrick-immometrica
    ContributorAuthor

    Closing as a duplicate: tolerating unknown applied migrations landed in #3246 (ignore_missing_migrations()), and the lock is #3245. The per-migration lock timeout can already be set with SET LOCAL lock_timeout inside the migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions