mirror of
https://git.victorphan.net/basketballcantho/ten-project.git
synced 2026-08-05 16:43:12 +07:00
59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""Alembic environment — wires SQLAlchemy models into the migration engine."""
|
|
|
|
import os
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
# Load app models so Alembic can inspect metadata
|
|
from app.database import Base # noqa: F401 — registers Base.metadata
|
|
import app.models # noqa: F401 — registers all ORM classes
|
|
|
|
# ── Alembic Config object ─────────────────────────────────────────────────────
|
|
config = context.config
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# Inject DATABASE_URL from environment so it is never hard-coded
|
|
database_url = os.environ["DATABASE_URL"]
|
|
config.set_main_option("sqlalchemy.url", database_url)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
# ── Offline migrations (no live DB connection) ────────────────────────────────
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=database_url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
# ── Online migrations (live DB connection) ────────────────────────────────────
|
|
def run_migrations_online() -> None:
|
|
connectable = engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|