50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
import asyncio
|
|
|
|
from alembic import context
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
|
|
|
|
|
config = context.config
|
|
|
|
DATABASE_URL_FILE = "/run/secrets/database_url"
|
|
|
|
|
|
def read_database_url() -> str:
|
|
with open(DATABASE_URL_FILE, encoding="utf-8") as file:
|
|
return file.read().strip()
|
|
|
|
|
|
config.set_main_option("sqlalchemy.url", read_database_url())
|
|
|
|
target_metadata = None
|
|
|
|
|
|
def do_run_migrations(connection) -> None:
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
)
|
|
|
|
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()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
asyncio.run(run_async_migrations())
|
|
|
|
|
|
run_migrations_online() |