feat(pry): alembic migration skeleton + auto-create opt-out

alembic/ already ships with env.py (target_metadata = Base.metadata)
and versions/0001_initial_schema.py covering all 26 tables. Production
deploys run `alembic upgrade head`; this commit gates the existing
`Base.metadata.create_all(_engine)` in db.py behind PRY_ALEMBIC_AUTO.

Default: PRY_ALEMBIC_AUTO=1 (auto-create, current behavior — tests pass).
Set PRY_ALEMBIC_AUTO=0 in production env so schema management lives in
migrations/ only and no surprise DDL happens at import time.

Tests: db tests pass under default; verified opt-out path is wired.
This commit is contained in:
opencode 2026-07-06 20:12:27 +07:00 committed by Rug Munch Media LLC
parent 025bcbbe9e
commit b5be9311b9

6
db.py
View file

@ -169,8 +169,10 @@ def get_engine() -> Engine:
cur.close() cur.close()
_SessionLocal = sessionmaker(bind=_engine, autoflush=False, expire_on_commit=False) _SessionLocal = sessionmaker(bind=_engine, autoflush=False, expire_on_commit=False)
# Auto-create tables on first import (idempotent) # Auto-create tables on first import (idempotent).
# Deprecated: use `alembic upgrade head` instead (see run_migrations()) # Disabled when PRY_ALEMBIC_AUTO=0 — production deploys run
# `alembic upgrade head` instead and should not auto-create.
if os.environ.get("PRY_ALEMBIC_AUTO", "1") == "1":
Base.metadata.create_all(_engine) Base.metadata.create_all(_engine)
logger.info( logger.info(
"db_engine_initialized", extra={"url": url.split("@")[-1] if "@" in url else url} "db_engine_initialized", extra={"url": url.split("@")[-1] if "@" in url else url}