|
| 1 | +import asyncio |
| 2 | +import importlib |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from logging.config import fileConfig |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# Add the project root directory to the Python path |
| 9 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 10 | + |
| 11 | +from sqlalchemy import pool |
| 12 | +from sqlalchemy.engine import Connection |
| 13 | +from sqlalchemy.ext.asyncio import async_engine_from_config |
| 14 | + |
| 15 | +from alembic import context |
| 16 | +from api.core.config import settings |
| 17 | +from api.core.database import Base |
| 18 | + |
| 19 | +# Automatically import all models |
| 20 | +src_path = Path(__file__).parent.parent / "api" / "src" |
| 21 | +for path in src_path.rglob("*.py"): |
| 22 | + if path.name != "__init__.py": |
| 23 | + module_path = str(path.relative_to(Path(__file__).parent.parent)).replace( |
| 24 | + os.sep, "." |
| 25 | + )[:-3] |
| 26 | + try: |
| 27 | + importlib.import_module(module_path) |
| 28 | + except Exception as e: |
| 29 | + print(f"Failed to import {module_path}: {e}") |
| 30 | + |
| 31 | +# this is the Alembic Config object |
| 32 | +config = context.config |
| 33 | + |
| 34 | +# Interpret the config file for Python logging |
| 35 | +if config.config_file_name is not None: |
| 36 | + fileConfig(config.config_file_name) |
| 37 | + |
| 38 | +# Set sqlalchemy.url from settings |
| 39 | +config.set_main_option("sqlalchemy.url", settings.OSM_DATABASE_URL) |
| 40 | + |
| 41 | +# Add your model's MetaData object here for 'autogenerate' support |
| 42 | +target_metadata = Base.metadata |
| 43 | + |
| 44 | + |
| 45 | +def run_migrations_offline() -> None: |
| 46 | + """Run migrations in 'offline' mode.""" |
| 47 | + url = config.get_main_option("sqlalchemy.url") |
| 48 | + context.configure( |
| 49 | + url=url, |
| 50 | + target_metadata=target_metadata, |
| 51 | + literal_binds=True, |
| 52 | + dialect_opts={"paramstyle": "named"}, |
| 53 | + ) |
| 54 | + |
| 55 | + with context.begin_transaction(): |
| 56 | + context.run_migrations() |
| 57 | + |
| 58 | + |
| 59 | +def do_run_migrations(connection: Connection) -> None: |
| 60 | + context.configure(connection=connection, target_metadata=target_metadata) |
| 61 | + |
| 62 | + with context.begin_transaction(): |
| 63 | + context.run_migrations() |
| 64 | + |
| 65 | + |
| 66 | +async def run_async_migrations() -> None: |
| 67 | + """In this scenario we need to create an Engine |
| 68 | + and associate a connection with the context.""" |
| 69 | + |
| 70 | + connectable = async_engine_from_config( |
| 71 | + config.get_section(config.config_ini_section, {}), |
| 72 | + prefix="sqlalchemy.", |
| 73 | + poolclass=pool.NullPool, |
| 74 | + ) |
| 75 | + |
| 76 | + async with connectable.connect() as connection: |
| 77 | + await connection.run_sync(do_run_migrations) |
| 78 | + |
| 79 | + await connectable.dispose() |
| 80 | + |
| 81 | + |
| 82 | +def run_migrations_online() -> None: |
| 83 | + """Run migrations in 'online' mode.""" |
| 84 | + |
| 85 | + asyncio.run(run_async_migrations()) |
| 86 | + |
| 87 | + |
| 88 | +if context.is_offline_mode(): |
| 89 | + run_migrations_offline() |
| 90 | +else: |
| 91 | + run_migrations_online() |
0 commit comments