53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
import asyncio
|
|
from logging.config import fileConfig
|
|
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
|
|
|
from alembic import context
|
|
from app.db import Base
|
|
from app.settings import get_settings
|
|
|
|
config = context.config
|
|
if config.config_file_name:
|
|
fileConfig(config.config_file_name)
|
|
config.set_main_option("sqlalchemy.url", get_settings().database_url)
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def do_run_migrations(connection) -> None:
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
include_schemas=True,
|
|
version_table_schema="han_app",
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_async_migrations() -> None:
|
|
connectable = async_engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
await connectable.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
context.configure(
|
|
url=config.get_main_option("sqlalchemy.url"),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
include_schemas=True,
|
|
version_table_schema="han_app",
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
else:
|
|
asyncio.run(run_async_migrations())
|