diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07ec73c --- /dev/null +++ b/.gitignore @@ -0,0 +1,51 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# Virtual Environment +venv/ +env/ +ENV/ + +# VS Code +.vscode/ +.history + +# Logs +logs/ +*.log + +# Local database +storage/ + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Jupyter Notebook +.ipynb_checkpoints \ No newline at end of file diff --git a/README.md b/README.md index 22b7d38..911abb9 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,19 @@ pip install -r requirements.txt ## Database Setup +The application uses SQLite as the database. The database file will be created automatically in the following location: +- In production: `/app/storage/db/db.sqlite` +- In development: `./storage/db/db.sqlite` (relative to the project root) + 1. Run database migrations: ```bash +# Make sure you're in the project root directory alembic upgrade head ``` +Note: If you encounter import errors with Alembic, make sure you're running the command from the project root directory so that the `app` module can be found. + ## Running the Application Start the application with Uvicorn: diff --git a/alembic.ini b/alembic.ini index 612fb12..53a4565 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 +# The SQLAlchemy connection string will be dynamically set in env.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/alembic/env.py b/alembic/env.py index d297f64..d160e24 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,10 +1,18 @@ 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 +# Append the parent directory to sys.path to allow imports from the app package +# This is crucial for Alembic to find the app module +BASE_DIR = Path(__file__).resolve().parent.parent +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 @@ -13,10 +21,14 @@ config = context.config # This line sets up loggers basically. fileConfig(config.config_file_name) -# add your model's MetaData object here -# for 'autogenerate' support -from app.database import Base +# Import the database configuration +from app.database import Base, SQLALCHEMY_DATABASE_URL from app.models.todo import Todo + +# Set the SQLAlchemy URL in Alembic configuration +config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL) + +# Set the target metadata target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, diff --git a/app/database/__init__.py b/app/database/__init__.py index 9d66c3a..4c47a2e 100644 --- a/app/database/__init__.py +++ b/app/database/__init__.py @@ -1,10 +1,18 @@ +import os from pathlib import Path from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker # Create database directory if it doesn't exist -DB_DIR = Path("/app") / "storage" / "db" +# Check if /app/storage is available, otherwise use a local directory +if os.path.exists("/app"): + DB_DIR = Path("/app") / "storage" / "db" +else: + # Fallback to a local directory for development + DB_DIR = Path(__file__).resolve().parent.parent.parent / "storage" / "db" + +# Ensure the directory exists DB_DIR.mkdir(parents=True, exist_ok=True) # Database URL