42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
|
|
"""Alembic конфигурация для async migrations."""
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
from logging.config import fileConfig
|
||
|
|
|
||
|
|
from alembic import context
|
||
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||
|
|
|
||
|
|
from app.core.database import Base
|
||
|
|
from app.models import * # noqa: F401,F403 — все модели должны быть импортированы
|
||
|
|
|
||
|
|
config = context.config
|
||
|
|
if config.config_file_name is not None:
|
||
|
|
fileConfig(config.config_file_name)
|
||
|
|
|
||
|
|
target_metadata = Base.metadata
|
||
|
|
|
||
|
|
|
||
|
|
def run_migrations_offline() -> None:
|
||
|
|
url = config.get_main_option("sqlalchemy.url")
|
||
|
|
context.configure(url=url, target_metadata=target_metadata, literal_binds=True)
|
||
|
|
with context.begin_transaction():
|
||
|
|
context.run_migrations()
|
||
|
|
|
||
|
|
|
||
|
|
async def run_migrations_online() -> None:
|
||
|
|
connectable = create_async_engine(config.get_main_option("sqlalchemy.url"))
|
||
|
|
async with connectable.connect() as connection:
|
||
|
|
await connection.run_sync(do_run_migrations)
|
||
|
|
|
||
|
|
|
||
|
|
def do_run_migrations(connection):
|
||
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
||
|
|
with context.begin_transaction():
|
||
|
|
context.run_migrations()
|
||
|
|
|
||
|
|
|
||
|
|
if context.is_offline_mode():
|
||
|
|
run_migrations_offline()
|
||
|
|
else:
|
||
|
|
asyncio.run(run_migrations_online())
|