app.storage.postgres.migrations

Apply the Alembic migrations under a PostgreSQL advisory lock.

Used by adriano migrate and adriano serve (migrate-on-start). Several replicas or an init container may start at the same time: each one first takes the session-level advisory lock MIGRATION_LOCK_KEY on a dedicated connection, holds it while alembic upgrade head runs on its own connection, and releases it afterwards. A process that waits for the lock finds the schema already at head and upgrades nothing.

Public API: MIGRATION_LOCK_KEY, alembic_ini_path(), upgrade_to_head().

app.storage.postgres.migrations.MIGRATION_LOCK_KEY = 7017859916281835297

Single-key (bigint) advisory lock serialising migrations: the ASCII bytes of adriano! read as a big-endian integer. Single-key and two-key advisory locks never conflict in PostgreSQL, so this is independent of the registration locks (app.storage.postgres.store.LOCK_NAMESPACE).

app.storage.postgres.migrations.alembic_ini_path()

Locate alembic.ini.

It sits next to the app package in the repository (backend/) and in the container image (/app); the working directory is tried as a fallback.

Return type:

Path

Returns:

The file.

Raises:

ConfigurationError – If it is in neither place.

app.storage.postgres.migrations.upgrade_to_head(database_url)

Run alembic upgrade head while holding the migration lock.

Blocks until the lock is free. The lock is released when the upgrade ends, also on failure (and by PostgreSQL if the process dies, since it belongs to the lock connection’s session).

Parameters:

database_url (str) – Plain postgresql:// DSN.

Raises:
  • sqlalchemy.exc.SQLAlchemyError – If the database is unreachable or a migration fails.

  • ConfigurationError – If alembic.ini cannot be found.

Return type:

None