forked from mtusi2026/mtusi2026lab2
Лабораторная работа 2
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""Пул соединений PostgreSQL и версионируемые миграции."""
|
||||
import logging
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import psycopg
|
||||
from psycopg_pool import ConnectionPool
|
||||
|
||||
from .config import Settings
|
||||
|
||||
log = logging.getLogger("messenger.db")
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations"
|
||||
|
||||
# Произвольная константа: один процесс за раз применяет миграции,
|
||||
# даже если одновременно стартуют несколько реплик API (lab3).
|
||||
MIGRATION_LOCK_ID = 2026_0002
|
||||
|
||||
|
||||
def conninfo_kwargs(settings: Settings) -> dict:
|
||||
return {
|
||||
"connect_timeout": settings.db_connect_timeout,
|
||||
# Все timestamptz приходят в UTC; любой запрос ограничен по времени.
|
||||
"options": "-c TimeZone=UTC -c statement_timeout=%d"
|
||||
% settings.db_statement_timeout_ms,
|
||||
"application_name": "messenger-api",
|
||||
}
|
||||
|
||||
|
||||
def create_pool(settings: Settings) -> ConnectionPool:
|
||||
pool = ConnectionPool(
|
||||
conninfo=settings.database_url,
|
||||
kwargs=conninfo_kwargs(settings),
|
||||
min_size=settings.db_pool_min,
|
||||
max_size=settings.db_pool_max,
|
||||
# Сколько запрос ждёт свободное соединение, прежде чем получить 503.
|
||||
timeout=settings.db_pool_timeout,
|
||||
# Битые после рестарта БД соединения отбрасываются при выдаче из пула.
|
||||
check=ConnectionPool.check_connection,
|
||||
max_idle=300,
|
||||
reconnect_timeout=3600,
|
||||
open=False,
|
||||
name="messenger",
|
||||
)
|
||||
# wait=False: процесс поднимается, даже если БД ещё/уже недоступна;
|
||||
# readiness это покажет, а запросы получат 503.
|
||||
pool.open(wait=False)
|
||||
return pool
|
||||
|
||||
|
||||
def _migration_files():
|
||||
files = sorted(MIGRATIONS_DIR.glob("*.sql"))
|
||||
if not files:
|
||||
raise RuntimeError("no migrations found in %s" % MIGRATIONS_DIR)
|
||||
return files
|
||||
|
||||
|
||||
def apply_migrations(settings: Settings) -> list:
|
||||
"""Применяет недостающие миграции; возвращает список применённых версий."""
|
||||
applied_now = []
|
||||
with psycopg.connect(
|
||||
settings.database_url, autocommit=True, **conninfo_kwargs(settings)
|
||||
) as conn:
|
||||
conn.execute("SELECT pg_advisory_lock(%s)", (MIGRATION_LOCK_ID,))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version text PRIMARY KEY,
|
||||
applied_at timestamptz NOT NULL DEFAULT now()
|
||||
)
|
||||
"""
|
||||
)
|
||||
done = {
|
||||
row[0]
|
||||
for row in conn.execute("SELECT version FROM schema_migrations")
|
||||
}
|
||||
for path in _migration_files():
|
||||
version = path.stem
|
||||
if version in done:
|
||||
continue
|
||||
sql = path.read_text(encoding="utf-8")
|
||||
# DDL и отметка о версии — одна транзакция: либо всё, либо ничего.
|
||||
with conn.transaction():
|
||||
conn.execute(sql)
|
||||
conn.execute(
|
||||
"INSERT INTO schema_migrations (version) VALUES (%s)",
|
||||
(version,),
|
||||
)
|
||||
applied_now.append(version)
|
||||
log.info("applied migration %s", version)
|
||||
finally:
|
||||
conn.execute("SELECT pg_advisory_unlock(%s)", (MIGRATION_LOCK_ID,))
|
||||
return applied_now
|
||||
|
||||
|
||||
def wait_and_migrate(settings: Settings) -> list:
|
||||
deadline = time.monotonic() + settings.migrate_wait_seconds
|
||||
last_error: Optional[Exception] = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
return apply_migrations(settings)
|
||||
except psycopg.OperationalError as exc:
|
||||
last_error = exc
|
||||
log.warning("database not ready for migrations, retrying")
|
||||
time.sleep(1)
|
||||
raise RuntimeError("database unavailable for migrations") from last_error
|
||||
Reference in New Issue
Block a user