diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bb50d11..6cd8818 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -108,6 +108,7 @@ jobs: - name: "Apply Alembic Migrations against Live PostgreSQL 17" env: DATABASE_URL: postgresql://atb:atb_ci_test_password@localhost:5432/atb + PYTHONPATH: ${{ github.workspace }}/project_code run: | cd project_code/backend alembic upgrade head @@ -126,7 +127,7 @@ jobs: - name: "Run Live E2E Integration Test Suite" env: DATABASE_URL: postgresql://atb:atb_ci_test_password@localhost:5432/atb - PYTHONPATH: project_code + PYTHONPATH: ${{ github.workspace }}/project_code USE_IDENTITY_MOCK: "True" run: | python3 -m unittest discover -s project_code/tests/e2e -p "test_*.py" diff --git a/project_code/backend/alembic.ini b/project_code/backend/alembic.ini index 7f71dd3..e04f5f8 100644 --- a/project_code/backend/alembic.ini +++ b/project_code/backend/alembic.ini @@ -1,6 +1,6 @@ [alembic] script_location = alembic -prepend_sys_path = . +prepend_sys_path = . .. [loggers] keys = root,sqlalchemy,alembic diff --git a/project_code/backend/alembic/env.py b/project_code/backend/alembic/env.py index 290f9e8..0a32c66 100644 --- a/project_code/backend/alembic/env.py +++ b/project_code/backend/alembic/env.py @@ -1,8 +1,18 @@ import os +import sys +from pathlib import Path from logging.config import fileConfig from sqlalchemy import engine_from_config, pool from alembic import context +# Ensure project_code root is in sys.path so 'backend.app' can be imported anywhere +backend_dir = Path(__file__).resolve().parents[1] +code_root = backend_dir.parent +if str(code_root) not in sys.path: + sys.path.insert(0, str(code_root)) +if str(backend_dir) not in sys.path: + sys.path.insert(0, str(backend_dir)) + # Alembic Config object config = context.config @@ -12,7 +22,7 @@ if config.config_file_name is not None: # Import models target metadata from backend.app.models.user import Base -target_metadata = Base.metadata +target_metadata = Base.metadata if Base is not None else None def get_url(): return os.getenv( diff --git a/project_code/backend/app/models/user.py b/project_code/backend/app/models/user.py index 68b6728..80a73fb 100644 --- a/project_code/backend/app/models/user.py +++ b/project_code/backend/app/models/user.py @@ -28,6 +28,7 @@ try: except ImportError: # Lightweight fallback model when SQLAlchemy is not loaded in current environment + Base = None class User: def __init__(self, subject: str, email: str, full_name: Optional[str] = None, is_active: bool = True, id: Optional[str] = None): diff --git a/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md b/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md index d5938b8..a2cdb7f 100644 --- a/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md +++ b/project_sdlc/prompts/20261013T073920Z-session-ci-coverage-fix.md @@ -101,3 +101,23 @@ - `python3 process/scripts/verify_traceability_gates.py --all`: **Quality Gates 5.1, 9.1, and 14.1 (across Iterations 01-07) all PASSED** with 0 Critical, 0 Major, 0 Minor issues. - `bash process/scripts/run_tests.sh --scope all`: Full test runner executed cleanly; all 50 Bruno collections detected in `project_code/`. - Directory hygiene verified: exactly three top-level non-hidden directories (`process/`, `project_sdlc/`, `project_code/`). + +--- + +### Turn 6: 2026-10-13T10:27:00Z +- **User:** + Reported that in the CI pipeline on GitHub, Step 3 ("Migrations & Live Container E2E") failed with `ModuleNotFoundError: No module named 'backend'` when executing `alembic upgrade head`. + Asked: + 1. Should ideally `.github` be moved under `project_code` or kept at root? + 2. What is logical: move `.github` or add `project_code` in the path in `ci.yaml`? +- **Agent Analysis & Implementation:** + - **Directory Placement Decision (.github):** Clarified that `.github/` **must remain at the repository root** because GitHub Actions strictly scans `/.github/workflows/` for repository workflows. Moving it inside `project_code` would completely disable workflow triggering on this repository. + - **Module Resolution Diagnosis:** The failure was caused because `alembic upgrade head` ran with working directory `project_code/backend`. In `alembic/env.py`, `from backend.app.models.user import Base` requires `project_code` in Python's import search path (`sys.path`). Unlike the subsequent E2E step, the Alembic migration step had no `PYTHONPATH` declared. + - **Implementation Fixes:** + 1. In `.github/workflows/ci.yml`: Added `PYTHONPATH: ${{ github.workspace }}/project_code` to the Alembic migration step and normalized the E2E step to use `${{ github.workspace }}/project_code`. + 2. In `project_code/backend/alembic/env.py`: Dynamically prepended `Path(__file__).resolve().parents[2]` (`project_code`) to `sys.path` so Alembic runs standalone regardless of invocation context. + 3. In `project_code/backend/alembic.ini`: Configured `prepend_sys_path = . ..` to ensure Alembic loads the parent package directory. + 4. In `project_code/backend/app/models/user.py`: Added `Base = None` in the `except ImportError` fallback block. + - **Verification:** + - Simulated `env.py` and model imports from `project_code/backend`: successfully imported `Base` and `User`. + - Executed unit suite: 197/197 tests passed, 98.4% statement coverage.