Files
mtusi2026lab4/src/app/migrate.py
T

142 lines
5.0 KiB
Python

import asyncio
import logging
import sys
from pathlib import Path
from sqlalchemy import text
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.ext.asyncio import AsyncEngine
from app.config import load_settings
from app.db import build_engine, is_dependency_error
logger = logging.getLogger("app.migrate")
# Один номер на все процессы, чтобы две миграции не применяли DDL одновременно.
_ADVISORY_LOCK = 842021
def sql_statements(script: str) -> list[str]:
lines = []
for line in script.splitlines():
if line.strip().startswith("--"):
continue
lines.append(line)
statements = []
for part in "\n".join(lines).split(";"):
statement = part.strip()
if statement:
statements.append(statement)
return statements
def expected_versions(directory: Path) -> list[str]:
files = sorted(path for path in directory.glob("*.sql") if path.is_file())
if not files:
raise SystemExit("no SQL migrations found")
return [path.name for path in files]
async def apply_migrations(engine: AsyncEngine, directory: Path) -> int:
files = sorted(path for path in directory.glob("*.sql") if path.is_file())
if not files:
raise SystemExit("no SQL migrations found")
applied = 0
async with engine.begin() as conn:
await conn.execute(text("SELECT pg_advisory_xact_lock(:lock_id)"), {"lock_id": _ADVISORY_LOCK})
await conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
)
"""
)
)
for path in files:
version = path.name
exists = await conn.scalar(
text("SELECT version FROM schema_migrations WHERE version = :version"),
{"version": version},
)
if exists:
continue
for statement in sql_statements(path.read_text(encoding="utf-8")):
await conn.execute(text(statement))
await conn.execute(
text("INSERT INTO schema_migrations (version) VALUES (:version)"),
{"version": version},
)
applied += 1
logger.info("migration applied version=%s", version)
return applied
async def wait_for_database(engine: AsyncEngine) -> None:
for attempt in range(40):
try:
async with engine.connect() as conn:
await conn.execute(text("SELECT 1"))
return
except Exception as exc:
if not is_dependency_error(exc):
logger.error("database probe failed type=%s", type(exc).__name__)
raise SystemExit("database probe failed") from None
logger.warning("database not ready type=%s attempt=%s", type(exc).__name__, attempt + 1)
if attempt == 39:
raise SystemExit("database is not ready") from None
await asyncio.sleep(0.5)
async def wait_for_schema(engine: AsyncEngine, directory: Path) -> None:
needed = set(expected_versions(directory))
for attempt in range(40):
try:
async with engine.connect() as conn:
rows = (await conn.execute(text("SELECT version FROM schema_migrations"))).all()
found = {row[0] for row in rows}
if needed <= found:
logger.info("schema ready versions=%s", ",".join(sorted(found)))
return
logger.warning("schema incomplete attempt=%s", attempt + 1)
except ProgrammingError:
logger.warning("schema_migrations missing attempt=%s", attempt + 1)
except Exception as exc:
if not is_dependency_error(exc):
logger.error("schema probe failed type=%s", type(exc).__name__)
raise SystemExit("schema probe failed") from None
logger.warning("schema probe unavailable type=%s attempt=%s", type(exc).__name__, attempt + 1)
if attempt == 39:
raise SystemExit("schema is not ready")
await asyncio.sleep(0.5)
async def run_job() -> None:
settings = load_settings()
engine = build_engine(settings)
try:
await wait_for_database(engine)
applied = await apply_migrations(engine, settings.migrations_dir)
logger.info("migrations newly_applied=%s", applied)
except ProgrammingError as exc:
sqlstate = getattr(getattr(exc, "orig", None), "sqlstate", "")
logger.error("migration rejected type=%s sqlstate=%s", type(exc).__name__, sqlstate)
raise SystemExit("migration failed") from None
finally:
await engine.dispose()
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s %(message)s",
stream=sys.stdout,
force=True,
)
asyncio.run(run_job())
if __name__ == "__main__":
main()