From 5f003a0aeb03367a4dd07004b471d6fe24c46eab Mon Sep 17 00:00:00 2001 From: Automated Action Date: Sat, 31 May 2025 21:19:53 +0000 Subject: [PATCH] Fix database migration import path issue Modified migrations/env.py to properly handle Python import paths in containerized environments, fixing the 'No module named app' error by adding project root and container paths to sys.path. --- migrations/env.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/migrations/env.py b/migrations/env.py index 9b22d44..a151ee9 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,12 +1,39 @@ +import os +import sys from logging.config import fileConfig - -from sqlalchemy import engine_from_config -from sqlalchemy import pool +from pathlib import Path from alembic import context +from sqlalchemy import engine_from_config, pool -from app.models import Base -from app.core.config import settings +# Add the project root directory to the Python path +# This ensures that the 'app' module can be imported correctly +project_root = str(Path(__file__).parents[1].absolute()) +if project_root not in sys.path: + sys.path.insert(0, project_root) + +# For containerized environments where the app might be in a different location +container_paths = [ + "/app/repo", # Common container path + "/app", # Another common container path +] + +for container_path in container_paths: + if os.path.exists(container_path) and container_path not in sys.path: + sys.path.insert(0, container_path) + +# Now we can import our application modules +try: + from app.core.config import settings # noqa: F401 - Used in later operations + from app.models import Base +except ImportError as e: + print(f"Error importing app modules: {e}") + print(f"Current sys.path: {sys.path}") + print("Looking for app module in these directories:") + for path in sys.path: + if os.path.isdir(path): + print(f" - {path}: {os.listdir(path)}") + raise # this is the Alembic Config object, which provides # access to the values within the .ini file in use.