- carry the lab 3 solution over from mtusi2026lab3-safronov (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests, bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts, Dockerfile, compose, pyproject and uv lock - merge Makefile, .gitignore and .dockerignore (the lab 3 versions only add lines to the skeleton, which is the same in lab 4) and .env.example (lab 4 skeleton names listed in the header) - rename the compose project to mtusi-lab4 and the image to :lab4 so the stand does not reuse the lab 3 containers and volumes - update AGENTS.md for lab 4: current lab, origin commit, contract and test files of the new skeleton Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
"""Окружение alembic: асинхронный шаблон, URL и журнал — из приложения.
|
|
|
|
URL берётся из `settings.database.url` и не проходит через ConfigParser
|
|
alembic, поэтому `%` в пароле ничего не ломает. Журнал — JSON через
|
|
`src/infra/logger.py`, как у сервиса. Асинхронное здесь только
|
|
подключение: ревизии остаются синхронным кодом `op.*`.
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
from alembic import context
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.engine import Connection
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
|
|
from src.config import settings
|
|
from src.infra.db.models import Base
|
|
from src.infra.logger import AbstractLogger, get_logger
|
|
|
|
logger: AbstractLogger = get_logger().bind(module="migrations")
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""SQL без подключения: `alembic upgrade head --sql`."""
|
|
context.configure(
|
|
url=settings.database.url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run_migrations(connection: Connection) -> None:
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_async_migrations() -> None:
|
|
database = settings.database
|
|
# адрес и имя базы — чтобы по журналу было видно, какую базу мигрировали
|
|
logger.info(
|
|
"Running alembic",
|
|
host=database.host,
|
|
port=database.port,
|
|
database=database.name,
|
|
)
|
|
engine = create_async_engine(
|
|
database.url,
|
|
poolclass=pool.NullPool,
|
|
connect_args={"timeout": database.connect_timeout_seconds},
|
|
)
|
|
try:
|
|
async with engine.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
finally:
|
|
await engine.dispose()
|
|
logger.info("Finished alembic", database=database.name)
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
asyncio.run(run_async_migrations())
|