51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
import asyncio
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
from sqlmodel import SQLModel
|
|
|
|
from utils.db import models # noqa: F401
|
|
from utils.env import env
|
|
|
|
config = context.config
|
|
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = SQLModel.metadata
|
|
|
|
|
|
def _async_url() -> str:
|
|
return env.db.connection_url.replace("postgresql://", "postgresql+asyncpg://", 1)
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=_async_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: # noqa: ANN001
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_migrations_online() -> None:
|
|
connectable = create_async_engine(_async_url())
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(_do_run_migrations)
|
|
await connectable.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
asyncio.run(run_migrations_online())
|