47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import asyncio
|
|
import os
|
|
|
|
from alembic import context
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
|
from sqlalchemy.pool import NullPool
|
|
|
|
from app.database.models import Base
|
|
|
|
config = context.config
|
|
target_metadata = Base.metadata
|
|
database_url = os.environ.get("DATABASE_URL", config.get_main_option("sqlalchemy.url"))
|
|
config.set_main_option("sqlalchemy.url", database_url)
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=config.get_main_option("sqlalchemy.url"),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run_migrations(connection) -> None:
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_migrations_online() -> None:
|
|
connectable = async_engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=NullPool,
|
|
)
|
|
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())
|