| """Alembic environment (async).""" |
|
|
| from __future__ import annotations |
|
|
| import asyncio |
| from logging.config import fileConfig |
|
|
| from alembic import context |
| from sqlalchemy.ext.asyncio import create_async_engine |
|
|
| import app.models |
| from app.config import get_settings |
| from app.db import Base |
|
|
| config = context.config |
| if config.config_file_name is not None: |
| fileConfig(config.config_file_name) |
|
|
| target_metadata = Base.metadata |
|
|
|
|
| def _db_url() -> str: |
| return get_settings().database_url |
|
|
|
|
| def run_migrations_offline() -> None: |
| context.configure( |
| url=_db_url(), |
| target_metadata=target_metadata, |
| literal_binds=True, |
| dialect_opts={"paramstyle": "named"}, |
| ) |
| with context.begin_transaction(): |
| context.run_migrations() |
|
|
|
|
| def _do_run_migrations(connection) -> None: |
| context.configure(connection=connection, target_metadata=target_metadata) |
| with context.begin_transaction(): |
| context.run_migrations() |
|
|
|
|
| async def run_migrations_online() -> None: |
| engine = create_async_engine(_db_url()) |
| async with engine.connect() as connection: |
| await connection.run_sync(_do_run_migrations) |
| await engine.dispose() |
|
|
|
|
| if context.is_offline_mode(): |
| run_migrations_offline() |
| else: |
| asyncio.run(run_migrations_online()) |
|
|