diff --git a/README.md b/README.md index 395b3dc..3ae2275 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,17 @@ A simple Todo API built with FastAPI and SQLite. pip install -r requirements.txt ``` -3. Apply the database migrations: +3. Initialize the database directory: + ``` + python init_db.py + ``` + +4. Apply the database migrations: ``` alembic upgrade head ``` -4. Run the application: +5. Run the application: ``` uvicorn main:app --reload ``` diff --git a/alembic.ini b/alembic.ini index 4d8e9aa..784e1d5 100644 --- a/alembic.ini +++ b/alembic.ini @@ -35,7 +35,7 @@ script_location = alembic # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = sqlite:////app/storage/db/db.sqlite +# We'll set this dynamically in env.py to match the application's config [post_write_hooks] diff --git a/alembic/env.py b/alembic/env.py index 85ad1e7..a4d8ca9 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,14 +1,24 @@ 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 parent directory to Python path so that 'app' can be imported +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config +# Import the application config to use the same database URL +from app.core.config import settings +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) diff --git a/app/core/config.py b/app/core/config.py index 525fc52..4c8fbd5 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -7,7 +7,9 @@ class Settings(BaseSettings): PROJECT_NAME: str = "Simple Todo App" # Database settings - DB_DIR = Path("/app") / "storage" / "db" + # Using a local path that will have proper permissions + BASE_DIR = Path(__file__).resolve().parent.parent.parent + DB_DIR = BASE_DIR / "db" DB_DIR.mkdir(parents=True, exist_ok=True) SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite" diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..bf7ae54 --- /dev/null +++ b/init_db.py @@ -0,0 +1,11 @@ +"""Initialize database directories and create initial tables.""" +import os +from pathlib import Path + +# Create directory for database +BASE_DIR = Path(__file__).resolve().parent +DB_DIR = BASE_DIR / "db" +DB_DIR.mkdir(parents=True, exist_ok=True) + +print(f"Database directory created at: {DB_DIR}") +print("Now you can run: alembic upgrade head") \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 8225ef6..21ffe95 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,10 @@ +[build-system] +requires = ["setuptools>=42", "wheel"] +build-backend = "setuptools.build_meta" + +[tool.setuptools] +packages = ["app"] + [tool.ruff] line-length = 88 target-version = "py37"