Fix app module import in Alembic configuration

- Added improved Python path handling in env.py
- Added fallback import method for app.models
- Ensured database URL consistency

generated with BackendIM... (backend.im)
This commit is contained in:
Automated Action 2025-05-13 16:05:11 +00:00
parent b2cf3ca746
commit 848c5b628b
3 changed files with 16 additions and 5 deletions

View File

@ -35,8 +35,8 @@ script_location = alembic
# are written from script.py.mako
# output_encoding = utf-8
# SQLite URL example
sqlalchemy.url = sqlite:////projects/genericrestapiservice-w6od1p/db.sqlite
# SQLite URL example - Make sure it matches the path in app/database.py
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run

View File

@ -4,7 +4,11 @@ import os
from pathlib import Path
# Add the project root directory to Python's path
sys.path.insert(0, str(Path(__file__).parent.parent))
project_root = Path(__file__).parent.parent.absolute()
sys.path.insert(0, str(project_root))
# Also add the project root to the PYTHONPATH environment variable
os.environ["PYTHONPATH"] = str(project_root) + os.pathsep + os.environ.get("PYTHONPATH", "")
from sqlalchemy import engine_from_config
from sqlalchemy import pool
@ -23,7 +27,14 @@ fileConfig(config.config_file_name)
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from app.models import Base
try:
# Try importing directly
from app.models import Base
except ImportError:
# If that fails, try with an explicit import of the app module
import app.models
Base = app.models.Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,

View File

@ -4,7 +4,7 @@ from sqlalchemy.orm import sessionmaker
from pathlib import Path
# Create the database directory if it doesn't exist
DB_DIR = Path("/projects/genericrestapiservice-w6od1p")
DB_DIR = Path("/app/storage/db")
DB_DIR.mkdir(parents=True, exist_ok=True)
# Define the SQLite URL