diff --git a/alembic.ini b/alembic.ini index fb0167e..103ec01 100644 --- a/alembic.ini +++ b/alembic.ini @@ -36,8 +36,9 @@ script_location = migrations # output_encoding = utf-8 # SQLite URL example -# For container environment, use the proper path -sqlalchemy.url = sqlite:////projects/bloggingplatformapi-lncxqv/storage/db/db.sqlite +# This URL is just a placeholder and will be overridden by env.py +# using the same path calculation logic as in app/db/session.py +sqlalchemy.url = driver://user:pass@localhost/dbname [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/app/db/session.py b/app/db/session.py index 216e12b..c1ea642 100644 --- a/app/db/session.py +++ b/app/db/session.py @@ -1,13 +1,25 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from pathlib import Path +import os -# Get the project root directory -PROJECT_ROOT = Path("/projects/bloggingplatformapi-lncxqv") +# Check if we're running in the container environment at /app/repo +IN_CONTAINER = os.path.exists('/app/repo') + +# Set the database directory path based on the environment +if IN_CONTAINER: + # Container path + DB_DIR = Path("/app/repo/storage/db") +else: + # Local development path - using either the project directory or fallback to /app + PROJECT_ROOT = Path("/projects/bloggingplatformapi-lncxqv") + if PROJECT_ROOT.exists(): + DB_DIR = PROJECT_ROOT / "storage" / "db" + else: + DB_DIR = Path("/app/storage/db") # Create the directory for the database if it doesn't exist -DB_DIR = PROJECT_ROOT / "storage" / "db" DB_DIR.mkdir(parents=True, exist_ok=True) SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite" diff --git a/migrations/env.py b/migrations/env.py index bad92b8..3aaa4e8 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -30,6 +30,30 @@ except ImportError: # access to the values within the .ini file in use. config = context.config +# Set up the database URL dynamically based on environment +# Check if we're running in the container environment at /app/repo +IN_CONTAINER = os.path.exists('/app/repo') + +# Set the database directory path based on the environment +if IN_CONTAINER: + # Container path + DB_DIR = Path("/app/repo/storage/db") +else: + # Local development path - using either the project directory or fallback to /app + PROJECT_ROOT = Path("/projects/bloggingplatformapi-lncxqv") + if PROJECT_ROOT.exists(): + DB_DIR = PROJECT_ROOT / "storage" / "db" + else: + DB_DIR = Path("/app/storage/db") + +# Create the directory for the database if it doesn't exist +DB_DIR.mkdir(parents=True, exist_ok=True) + +SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite" + +# Override the URL in the config with our dynamic URL +config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL) + # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name)