"""PostgreSQL access: bounded pool, timeouts, and versioned migrations. Laboratory 3 separates schema management from request serving. The one-shot ``migrate`` Compose service calls :func:`apply_migrations`; API replicas only open their pool and wait until the newest migration is visible. That prevents N replicas from racing each other on DDL during a deploy. """ from __future__ import annotations import logging import threading import time from pathlib import Path import psycopg from psycopg.rows import dict_row from psycopg_pool import ConnectionPool from .config import settings log = logging.getLogger("messenger.db") MIGRATIONS_DIR = Path(__file__).resolve().parents[1] / "migrations" LOCK_ID = 0x4D545553 # stable application-wide advisory lock _pool: ConnectionPool | None = None _pool_lock = threading.Lock() _ready = threading.Event() _started = False _start_lock = threading.Lock() def _create_pool() -> ConnectionPool: options = ( f"-c statement_timeout={settings.db_statement_timeout_ms} " "-c idle_in_transaction_session_timeout=10000 " "-c lock_timeout=3000" ) return ConnectionPool( conninfo=settings.database_url, min_size=settings.db_pool_min, max_size=settings.db_pool_max, timeout=settings.db_pool_acquire_timeout, open=False, kwargs={ "row_factory": dict_row, "connect_timeout": settings.db_connect_timeout, "application_name": "mtusi-messenger-lab3", "options": options, }, ) def pool() -> ConnectionPool: global _pool with _pool_lock: if _pool is None: _pool = _create_pool() return _pool def required_version() -> str: """Newest migration shipped in this image; readiness waits for it.""" versions = sorted(path.stem for path in MIGRATIONS_DIR.glob("*.sql")) if not versions: raise RuntimeError("no migrations found") return versions[-1] def open_pool(timeout: float = 60.0) -> None: """Open the pool with bounded retries; used by migrate and API startup.""" deadline = time.monotonic() + timeout backoff = 0.5 while True: try: pool().open(wait=True, timeout=min(5.0, max(0.5, deadline - time.monotonic()))) return except Exception as exc: if time.monotonic() >= deadline: raise log.warning("database is not reachable yet: %s", type(exc).__name__) time.sleep(backoff) backoff = min(backoff * 2, 5.0) def apply_migrations() -> list[str]: """Apply every migration exactly once. A session-level advisory lock serialises the one-shot migrate service and any explicit operator run. Each migration and its schema_migrations row share one transaction, so a failed DDL statement leaves no version row. """ applied_now: list[str] = [] with pool().connection() as conn: conn.execute("SELECT pg_advisory_lock(%s)", (LOCK_ID,)) try: conn.execute( """ CREATE TABLE IF NOT EXISTS schema_migrations ( version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT clock_timestamp() ) """ ) applied = { row["version"] for row in conn.execute("SELECT version FROM schema_migrations").fetchall() } for path in sorted(MIGRATIONS_DIR.glob("*.sql")): version = path.stem if version in applied: continue log.info("applying migration %s", version) conn.execute(path.read_text(encoding="utf-8")) conn.execute( "INSERT INTO schema_migrations(version) VALUES (%s)", (version,) ) applied_now.append(version) finally: conn.execute("SELECT pg_advisory_unlock(%s)", (LOCK_ID,)) return applied_now def schema_is_current() -> bool: """Cheap readiness probe: the newest migration is committed and visible.""" if _pool is None: return False try: with pool().connection(timeout=1.0) as conn: row = conn.execute( "SELECT 1 FROM schema_migrations WHERE version = %s", (required_version(),), ).fetchone() return row is not None except Exception: return False def start_background_initialisation() -> None: """Open the pool and wait for the one-shot migrator, without blocking the liveness endpoint. The API never performs DDL itself.""" global _started with _start_lock: if _started: return _started = True def run() -> None: backoff = 0.5 opened = False while True: try: if not opened: open_pool(timeout=5.0) opened = True if schema_is_current(): _ready.set() log.info("database pool is ready; schema %s is current", required_version()) return log.warning("waiting for migrate service: schema %s is not ready", required_version()) except Exception as exc: log.warning("database initialisation is not ready: %s", type(exc).__name__) time.sleep(backoff) backoff = min(backoff * 2, 5.0) threading.Thread(target=run, name="db-init", daemon=True).start() def is_ready() -> bool: return _ready.is_set() def check_ready() -> bool: """Readiness checks the live connection and the migrated schema each time.""" return is_ready() and schema_is_current()