43 lines
1.0 KiB
Python
43 lines
1.0 KiB
Python
import asyncio
|
|
import os
|
|
|
|
from alembic import context
|
|
|
|
from app.models import Base
|
|
from app.postgres import create_postgres_engine
|
|
|
|
config = context.config
|
|
url = os.environ["BITRIX_DATABASE_URL"]
|
|
config.set_main_option("sqlalchemy.url", url.replace("%", "%%"))
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_offline() -> None:
|
|
context.configure(
|
|
url=config.get_main_option("sqlalchemy.url"),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
include_schemas=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run(connection) -> None:
|
|
context.configure(connection=connection, target_metadata=target_metadata, include_schemas=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_online() -> None:
|
|
engine = create_postgres_engine(url)
|
|
async with engine.connect() as connection:
|
|
await connection.run_sync(do_run)
|
|
await engine.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_offline()
|
|
else:
|
|
asyncio.run(run_online())
|