Coverage for app / migrations.py: 33%
21 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1import logging
2from alembic import command
3from alembic.config import Config
4from sqlalchemy import text
5from app.db.postgres.engine import engine
7logger = logging.getLogger(__name__)
10def run_migrations() -> None:
11 alembic_cfg = Config("alembic.ini")
12 # Keep the service's own logging: alembic/env.py configures it from
13 # alembic.ini only when run from the command line.
14 alembic_cfg.attributes["configure_logger"] = False
15 logger.info("Attempting to acquire advisory lock for migrations...")
17 with engine.connect() as conn:
18 got_lock = conn.execute(text("SELECT pg_try_advisory_lock(123456789)")).scalar()
20 if not got_lock:
21 logger.warning(
22 "Another process holds the migration lock. Skipping migrations."
23 )
24 return
26 try:
27 logger.info("Running Alembic migrations...")
28 command.upgrade(alembic_cfg, "head")
29 logger.info("Migrations complete")
30 finally:
31 conn.execute(text("SELECT pg_advisory_unlock(123456789)"))
32 logger.info("Released advisory lock")