Ensured alembic path has proper root

This commit is contained in:
Prashant Shendre 2026-10-13 16:49:37 +05:30
parent 66c8e7f9fd
commit 0ca8cd8765
5 changed files with 35 additions and 3 deletions

View File

@ -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"

View File

@ -1,6 +1,6 @@
[alembic]
script_location = alembic
prepend_sys_path = .
prepend_sys_path = . ..
[loggers]
keys = root,sqlalchemy,alembic

View File

@ -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(

View File

@ -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):

View File

@ -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 `<repo-root>/.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.