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.
This commit is contained in:
parent
e01f3a4d57
commit
5f003a0aeb
@ -1,12 +1,39 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
|
from pathlib import Path
|
||||||
from sqlalchemy import engine_from_config
|
|
||||||
from sqlalchemy import pool
|
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
|
from sqlalchemy import engine_from_config, pool
|
||||||
|
|
||||||
from app.models import Base
|
# Add the project root directory to the Python path
|
||||||
from app.core.config import settings
|
# 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
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user