|
| 1 | +from logging.config import fileConfig |
| 2 | + |
| 3 | +from sqlalchemy import engine_from_config |
| 4 | +from sqlalchemy import pool |
| 5 | + |
| 6 | +from alembic import context |
| 7 | + |
| 8 | +from core.database import Base |
| 9 | +from models import item |
| 10 | +from core.config import DATABASE_URL |
| 11 | + |
| 12 | +config = context.config |
| 13 | + |
| 14 | +if config.get_main_option("sqlalchemy.url") is None: |
| 15 | + config.set_main_option("sqlalchemy.url", DATABASE_URL) |
| 16 | + |
| 17 | +fileConfig(config.config_file_name) |
| 18 | + |
| 19 | +target_metadata = Base.metadata |
| 20 | + |
| 21 | + |
| 22 | +def run_migrations_offline() -> None: |
| 23 | + context.configure( |
| 24 | + url=config.get_main_option("sqlalchemy.url"), |
| 25 | + target_metadata=target_metadata, |
| 26 | + literal_binds=True, |
| 27 | + dialect_opts={"paramstyle": "named"}, |
| 28 | + ) |
| 29 | + |
| 30 | + with context.begin_transaction(): |
| 31 | + context.run_migrations() |
| 32 | + |
| 33 | + |
| 34 | +def run_migrations_online() -> None: |
| 35 | + connectable = engine_from_config( |
| 36 | + config.get_section(config.config_ini_section), |
| 37 | + prefix="sqlalchemy.", |
| 38 | + poolclass=pool.NullPool, |
| 39 | + ) |
| 40 | + |
| 41 | + with connectable.connect() as connection: |
| 42 | + context.configure(connection=connection, target_metadata=target_metadata) |
| 43 | + |
| 44 | + with context.begin_transaction(): |
| 45 | + context.run_migrations() |
| 46 | + |
| 47 | + |
| 48 | +if context.is_offline_mode(): |
| 49 | + run_migrations_offline() |
| 50 | +else: |
| 51 | + run_migrations_online() |
0 commit comments