diff --git a/app/core/config.py b/app/core/config.py index 4e187ba..4505634 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -2,8 +2,13 @@ import os from pathlib import Path from pydantic import field_validator, BaseModel -# Project directories -ROOT_DIR = Path(__file__).parent.parent.parent.resolve() +# Get project root directory +if '/app/repo' in str(Path.cwd()): + # When running in the container + ROOT_DIR = Path('/app/repo') +else: + # When running in development + ROOT_DIR = Path(__file__).parent.parent.parent.resolve() class Settings(BaseModel): diff --git a/migrations/env.py b/migrations/env.py index 1b6e595..dbe23ec 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -2,6 +2,7 @@ from logging.config import fileConfig from sqlalchemy import engine_from_config from sqlalchemy import pool +from pathlib import Path from alembic import context @@ -10,11 +11,15 @@ from app.models.base import Base # Import Secret model to ensure it's registered # noqa: F401 is used to tell linters to ignore the unused import from app.models.secret import Secret # noqa: F401 +from app.core.config import settings # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config +# Set the SQLAlchemy URL from the settings +config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URL) + # Interpret the config file for Python logging. # This line sets up loggers basically. if config.config_file_name is not None: @@ -61,6 +66,9 @@ def run_migrations_online() -> None: and associate a connection with the context. """ + # Ensure the DB directory exists + Path(settings.DB_DIR).mkdir(parents=True, exist_ok=True) + connectable = engine_from_config( config.get_section(config.config_ini_section, {}), prefix="sqlalchemy.",