diff --git a/alembic.ini b/alembic.ini index 75005f9..7110e88 100644 --- a/alembic.ini +++ b/alembic.ini @@ -35,7 +35,8 @@ script_location = migrations # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = sqlite:////app/storage/db/db.sqlite +# SQLite URL - use a relative path that will be resolved at runtime +sqlalchemy.url = sqlite:///db.sqlite [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/app/core/config.py b/app/core/config.py index 20bdc16..ed4f837 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -21,7 +21,12 @@ class Settings(BaseSettings): # Database DB_DIR: Path = Path("/app") / "storage" / "db" - SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite" + + @property + def SQLALCHEMY_DATABASE_URL(self) -> str: + # Ensure the database directory exists + self.DB_DIR.mkdir(parents=True, exist_ok=True) + return f"sqlite:///{self.DB_DIR}/db.sqlite" # CORS BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] diff --git a/app/db/session.py b/app/db/session.py index 142da3b..7a20ffc 100644 --- a/app/db/session.py +++ b/app/db/session.py @@ -3,8 +3,7 @@ from sqlalchemy.orm import sessionmaker from app.core.config import settings -# Ensure the DB directory exists -settings.DB_DIR.mkdir(parents=True, exist_ok=True) +# Note: Directory creation is now handled in the settings property engine = create_engine( settings.SQLALCHEMY_DATABASE_URL, diff --git a/migrations/env.py b/migrations/env.py index 8794797..b2a4b71 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,17 +1,28 @@ +import sys from logging.config import fileConfig +from pathlib import Path from sqlalchemy import engine_from_config from sqlalchemy import pool from alembic import context +# Add the project root directory to the Python path +project_root = Path(__file__).parent.parent +sys.path.insert(0, str(project_root)) + # Import from the application models -from app.db.base import Base +from app.db.base import Base # noqa +from app.core.config import settings # noqa # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config +# Override the SQLAlchemy URL with the one from our settings +# This ensures we use the same database URL as the application +config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URL) + # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name)