| 1 | from logging.config import fileConfig |
| 2 | |
| 3 | from alembic import context |
| 4 | from sqlalchemy import engine_from_config, pool |
| 5 | |
| 6 | from app.core.config import settings |
| 7 | from app.db.base import Base |
| 8 | import app.models |
| 9 | |
| 10 | config = context.config |
| 11 | config.set_main_option("sqlalchemy.url", settings.database_url) |
| 12 | if config.config_file_name is not None: |
| 13 | fileConfig(config.config_file_name) |
| 14 | |
| 15 | target_metadata = Base.metadata |
| 16 | |
| 17 | |
| 18 | def run_migrations_offline() -> None: |
| 19 | context.configure(url=settings.database_url, target_metadata=target_metadata, |
| 20 | literal_binds=True, dialect_opts={"paramstyle": "named"}) |
| 21 | with context.begin_transaction(): |
| 22 | context.run_migrations() |
| 23 | |
| 24 | |
| 25 | def run_migrations_online() -> None: |
| 26 | connectable = engine_from_config( |
| 27 | config.get_section(config.config_ini_section, {}), |
| 28 | prefix="sqlalchemy.", poolclass=pool.NullPool) |
| 29 | with connectable.connect() as connection: |
| 30 | context.configure(connection=connection, target_metadata=target_metadata) |
| 31 | with context.begin_transaction(): |
| 32 | context.run_migrations() |
| 33 | |
| 34 | |
| 35 | if context.is_offline_mode(): |
| 36 | run_migrations_offline() |
| 37 | else: |
| 38 | run_migrations_online() |