95 lines
2.4 KiB
Python
95 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import os
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.engine import Connection
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
|
|
|
from db.models import Base
|
|
|
|
config = context.config
|
|
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def _get_database_url() -> str:
|
|
configured_url = config.get_main_option("sqlalchemy.url")
|
|
if configured_url:
|
|
return configured_url
|
|
|
|
env_url = os.getenv("DATABASE_URL")
|
|
if env_url:
|
|
return env_url
|
|
|
|
user = os.getenv("POSTGRES_USER", "postgres")
|
|
password = os.getenv("POSTGRES_PASSWORD", "postgres")
|
|
host = os.getenv("POSTGRES_HOST", "localhost")
|
|
port = os.getenv("POSTGRES_PORT", "5432")
|
|
db_name = os.getenv("POSTGRES_DB", "postgres")
|
|
return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{db_name}"
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""Run migrations in 'offline' mode."""
|
|
|
|
context.configure(
|
|
url=_get_database_url(),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
compare_type=True,
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run_migrations(connection: Connection) -> None:
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
compare_type=True,
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_async_migrations() -> None:
|
|
configuration = config.get_section(config.config_ini_section) or {}
|
|
configuration["sqlalchemy.url"] = _get_database_url()
|
|
|
|
connectable = async_engine_from_config(
|
|
configuration,
|
|
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:
|
|
"""Run migrations in 'online' mode."""
|
|
|
|
connectable = config.attributes.get("connection", None)
|
|
if connectable is None:
|
|
asyncio.run(run_async_migrations())
|
|
else:
|
|
do_run_migrations(connectable)
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|