|
| 1 | +import asyncio |
| 2 | +import pathlib |
| 3 | +import sys |
| 4 | +from logging.config import fileConfig |
| 5 | + |
| 6 | +from alembic import context |
| 7 | +from sqlalchemy import create_engine |
| 8 | +from sqlalchemy.engine import Connection |
| 9 | +from sqlalchemy.ext.asyncio import AsyncEngine |
| 10 | +from sqlmodel import SQLModel |
| 11 | + |
| 12 | + |
| 13 | +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) |
| 14 | + |
| 15 | +from src.db.utils import get_db_url # noqa |
| 16 | +from src.models import * # noqa |
| 17 | + |
| 18 | + |
| 19 | +POSTGRES_URL = get_db_url() |
| 20 | + |
| 21 | +# this is the Alembic Config object, which provides |
| 22 | +# access to the values within the .ini file in use. |
| 23 | +config = context.config |
| 24 | + |
| 25 | +# Interpret the config file for Python logging. |
| 26 | +# This line sets up loggers basically. |
| 27 | +if config.config_file_name is not None: |
| 28 | + fileConfig(config.config_file_name) |
| 29 | + |
| 30 | +# add your model's MetaData object here |
| 31 | +# for 'autogenerate' support |
| 32 | +# from myapp import mymodel |
| 33 | +# target_metadata = mymodel.Base.metadata |
| 34 | +target_metadata = SQLModel.metadata |
| 35 | + |
| 36 | +# other values from the config, defined by the needs of env.py, |
| 37 | +# can be acquired: |
| 38 | +# my_important_option = config.get_main_option("my_important_option") |
| 39 | +# ... etc. |
| 40 | + |
| 41 | + |
| 42 | +def run_migrations_offline() -> None: |
| 43 | + """Run migrations in 'offline' mode. |
| 44 | +
|
| 45 | + This configures the context with just a URL |
| 46 | + and not an Engine, though an Engine is acceptable |
| 47 | + here as well. By skipping the Engine creation |
| 48 | + we don't even need a DBAPI to be available. |
| 49 | +
|
| 50 | + Calls to context.execute() here emit the given string to the |
| 51 | + script output. |
| 52 | +
|
| 53 | + """ |
| 54 | + context.configure( |
| 55 | + url=POSTGRES_URL, |
| 56 | + target_metadata=target_metadata, |
| 57 | + literal_binds=True, |
| 58 | + dialect_opts={"paramstyle": "named"}, |
| 59 | + ) |
| 60 | + |
| 61 | + with context.begin_transaction(): |
| 62 | + context.run_migrations() |
| 63 | + |
| 64 | + |
| 65 | +def do_run_migrations(connection: Connection) -> None: |
| 66 | + context.configure(connection=connection, target_metadata=target_metadata) |
| 67 | + |
| 68 | + with context.begin_transaction(): |
| 69 | + context.run_migrations() |
| 70 | + |
| 71 | + |
| 72 | +async def run_migrations_online() -> None: |
| 73 | + """Run migrations in 'online' mode. |
| 74 | +
|
| 75 | + In this scenario we need to create an Engine |
| 76 | + and associate a connection with the context. |
| 77 | +
|
| 78 | + """ |
| 79 | + # connectable = AsyncEngine( |
| 80 | + # engine_from_config( |
| 81 | + # config.get_section(config.config_ini_section), |
| 82 | + # prefix="sqlalchemy.", |
| 83 | + # poolclass=pool.NullPool, |
| 84 | + # future=True, |
| 85 | + # ) |
| 86 | + # ) |
| 87 | + connectable = AsyncEngine(create_engine(POSTGRES_URL, echo=True, future=True)) |
| 88 | + |
| 89 | + async with connectable.connect() as connection: |
| 90 | + await connection.run_sync(do_run_migrations) |
| 91 | + |
| 92 | + await connectable.dispose() |
| 93 | + |
| 94 | + |
| 95 | +if context.is_offline_mode(): |
| 96 | + run_migrations_offline() |
| 97 | +else: |
| 98 | + asyncio.run(run_migrations_online()) |
0 commit comments