diff --git a/alembic.ini b/alembic.ini index 48cc44c..917b25b 100644 --- a/alembic.ini +++ b/alembic.ini @@ -35,7 +35,8 @@ script_location = alembic # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = sqlite:////app/storage/db/db.sqlite +# Using a relative path with SQLite - will be replaced by env.py +sqlalchemy.url = driver://user:pass@localhost/dbname [post_write_hooks] diff --git a/alembic/env.py b/alembic/env.py index d08f1ec..c908228 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,14 +1,26 @@ from logging.config import fileConfig +import os +import sys +from pathlib import Path from sqlalchemy import engine_from_config from sqlalchemy import pool from alembic import context +# Add the project root directory to Python's path +BASE_DIR = Path(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(BASE_DIR)) + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config +# Import the db session configuration +from app.db.session import SQLALCHEMY_DATABASE_URL +# Override the sqlalchemy.url with our dynamic configuration +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) diff --git a/app/db/session.py b/app/db/session.py index fdbba60..5c4108d 100644 --- a/app/db/session.py +++ b/app/db/session.py @@ -1,8 +1,11 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from pathlib import Path +import os -DB_DIR = Path("/app") / "storage" / "db" +# Use relative path for development environment +BASE_DIR = Path(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +DB_DIR = BASE_DIR / "app" / "storage" / "db" DB_DIR.mkdir(parents=True, exist_ok=True) SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"