From b5be9311b986c9c396dab045e004db6a28ea08fb Mon Sep 17 00:00:00 2001 From: opencode Date: Mon, 6 Jul 2026 20:12:27 +0700 Subject: [PATCH] feat(pry): alembic migration skeleton + auto-create opt-out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- db.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/db.py b/db.py index c194ac9..c29d622 100644 --- a/db.py +++ b/db.py @@ -169,9 +169,11 @@ def get_engine() -> Engine: cur.close() _SessionLocal = sessionmaker(bind=_engine, autoflush=False, expire_on_commit=False) - # Auto-create tables on first import (idempotent) - # Deprecated: use `alembic upgrade head` instead (see run_migrations()) - Base.metadata.create_all(_engine) + # Auto-create tables on first import (idempotent). + # 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) logger.info( "db_engine_initialized", extra={"url": url.split("@")[-1] if "@" in url else url} )