| import asyncio |
| import re |
| from logging.config import fileConfig |
| from sqlalchemy import pool |
| from sqlalchemy.ext.asyncio import create_async_engine |
| from alembic import context |
|
|
| config = context.config |
| if config.config_file_name is not None: |
| fileConfig(config.config_file_name) |
|
|
| import sys, os |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
|
|
| |
| from dotenv import load_dotenv |
| load_dotenv(os.path.join(os.path.dirname(__file__), "..", ".env")) |
|
|
| from src.database import Base |
| from src.models import JobDescription, Candidate, MatchResult, Session |
| from src.config import get_settings |
|
|
| target_metadata = Base.metadata |
|
|
|
|
| def _make_async_url(url: str) -> str: |
| url = re.sub(r"^postgresql:", "postgresql+asyncpg:", url) |
| url = re.sub(r"[?&]channel_binding=require", "", url) |
| url = re.sub(r"[?&]sslmode=[^&]*", "", url) |
| url = re.sub(r"[?&]connect_timeout=[^&]*", "", url) |
| |
| url = re.sub(r"[?&]$", "", url) |
| return url |
|
|
|
|
| def run_migrations_offline() -> None: |
| settings = get_settings() |
| context.configure( |
| url=_make_async_url(settings.database_url), |
| target_metadata=target_metadata, |
| literal_binds=True, |
| dialect_opts={"paramstyle": "named"}, |
| ) |
| with context.begin_transaction(): |
| context.run_migrations() |
|
|
|
|
| def do_run_migrations(connection): |
| context.configure(connection=connection, target_metadata=target_metadata) |
| with context.begin_transaction(): |
| context.run_migrations() |
|
|
|
|
| async def run_async_migrations() -> None: |
| settings = get_settings() |
| from src.database import _make_async_url |
| db_url, connect_args = _make_async_url(settings.database_url) |
| connectable = create_async_engine(db_url, poolclass=pool.NullPool, connect_args=connect_args) |
| 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()) |
|
|
|
|
| if context.is_offline_mode(): |
| run_migrations_offline() |
| else: |
| run_migrations_online() |
|
|