diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6cd8818..caccc56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,8 +102,9 @@ jobs: - name: "Install Dependencies" run: | + python -m pip install --upgrade pip pip install -r project_code/backend/requirements.txt - pip install psycopg2-binary + pip install "psycopg[binary]" psycopg2-binary - name: "Apply Alembic Migrations against Live PostgreSQL 17" env: diff --git a/project_code/backend/alembic/env.py b/project_code/backend/alembic/env.py index 0a32c66..238d1f7 100644 --- a/project_code/backend/alembic/env.py +++ b/project_code/backend/alembic/env.py @@ -25,10 +25,20 @@ from backend.app.models.user import Base target_metadata = Base.metadata if Base is not None else None def get_url(): - return os.getenv( + url = os.getenv( "DATABASE_URL", "postgresql://atb:atb-local-only-change-me@localhost:5432/atb" ) + if url.startswith("postgresql://"): + try: + import psycopg # noqa: F401 + except ImportError: + try: + import psycopg2 # noqa: F401 + url = url.replace("postgresql://", "postgresql+psycopg2://", 1) + except ImportError: + pass + return url def run_migrations_offline() -> None: url = get_url() diff --git a/project_code/backend/requirements.txt b/project_code/backend/requirements.txt index 6b82630..5f92d00 100644 --- a/project_code/backend/requirements.txt +++ b/project_code/backend/requirements.txt @@ -5,6 +5,7 @@ fastapi>=0.115.0 uvicorn[standard]>=0.30.0 sqlalchemy>=2.0.30 psycopg2-binary>=2.9.9 +psycopg[binary]>=3.2.0 alembic>=1.13.0 pydantic>=2.8.0 requests>=2.32.0 diff --git a/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md b/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md index abdff7c..8d572f6 100644 --- a/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md +++ b/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md @@ -201,4 +201,24 @@ - `python3 scripts/verify_traceability_gates.py --all`: Quality Gates 5.1, 9.1, and 14.1 all PASSED with 0 issues. - Verified zero legacy directory references remain across `process/framework_guides/`, `process/templates/`, and `process/.agents/skills/`. +--- + +### Turn 11: 2026-10-13T13:45:00Z +- **User:** + Reported that in Step 3 of the CI job (Alembic migrations), execution failed with: + `ModuleNotFoundError: No module named 'psycopg'` from `sqlalchemy/dialects/postgresql/psycopg.py`. +- **Agent Analysis & Implementation:** + - **Root Cause Diagnosis:** + - In SQLAlchemy 2.0+ on Python 3.12, when a connection URL starts with `postgresql://` without an explicit driver prefix, SQLAlchemy attempts to load the modern Psycopg 3 (`psycopg`) driver. + - `project_code/backend/requirements.txt` and the CI workflow only had `psycopg2-binary` (v2 driver) installed, leaving `psycopg` (v3) missing. + - Meanwhile, `test_postgresql_live_rls.py` explicitly requires `psycopg2`. + - **Implementation Fixes:** + 1. In `project_code/backend/requirements.txt`: Added `psycopg[binary]>=3.2.0` alongside `psycopg2-binary>=2.9.9` so both v2 and v3 drivers are available in all execution environments and container builds. + 2. In `.github/workflows/ci.yml`: Updated Step 3 dependencies installation to explicitly install `psycopg[binary]` and `psycopg2-binary`. + 3. In `project_code/backend/alembic/env.py`: Added automatic driver fallback in `get_url()`: if a bare `postgresql://` URL is provided and `psycopg` (v3) is not installed, but `psycopg2` is available, it gracefully converts the connection URL to `postgresql+psycopg2://`. + - **Verification:** + - Ran `python3 project_code/tests/run_all_tests.py`: 197/197 tests passed, 98.4% statement coverage. + - Validated that `env.py` handles both driver environments seamlessly. + +