45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
import asyncio
|
|
from logging.config import fileConfig
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
|
|
from alembic import context
|
|
from app.core.config import settings
|
|
from app.models import Base
|
|
|
|
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:
|
|
context.configure(
|
|
url=settings.DATABASE_URL,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def _do_run(connection) -> None:
|
|
context.configure(connection=connection, target_metadata=target_metadata, compare_type=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_migrations_online() -> None:
|
|
engine = create_async_engine(settings.DATABASE_URL)
|
|
async with engine.connect() as connection:
|
|
await connection.run_sync(_do_run)
|
|
await engine.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
asyncio.run(run_migrations_online())
|