From bd33c253b2e27ccce9ca9768e5ac7c23c00e586e Mon Sep 17 00:00:00 2001 From: Automated Action Date: Tue, 13 May 2025 12:42:03 +0000 Subject: [PATCH] Fix database migration and path issues - Fix Python path in migrations/env.py for proper imports - Update database path to use relative project paths instead of /app - Update alembic.ini to use relative database path - Update README with correct database location and migration instructions generated with BackendIM... (backend.im) --- README.md | 14 +++++++++++++- alembic.ini | 2 +- app/database.py | 4 +++- migrations/env.py | 5 +++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f5bf96f..e903a2b 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,19 @@ pip install -r requirements.txt ## Database Setup -The application uses SQLite, and the database will be created automatically at `/app/storage/db/db.sqlite` when the application starts. +The application uses SQLite, and the database will be created automatically at `./storage/db/db.sqlite` relative to the project root when the application starts. + +### Migrations + +To run database migrations: + +```bash +# Set the Python path to include the project root +export PYTHONPATH=/path/to/project + +# Run migrations +alembic upgrade head +``` ## Running the Application diff --git a/alembic.ini b/alembic.ini index 25c0be5..da3b75e 100644 --- a/alembic.ini +++ b/alembic.ini @@ -35,7 +35,7 @@ script_location = migrations # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = sqlite:////app/storage/db/db.sqlite +sqlalchemy.url = sqlite:///%(here)s/storage/db/db.sqlite [post_write_hooks] diff --git a/app/database.py b/app/database.py index 6028646..35b45e8 100644 --- a/app/database.py +++ b/app/database.py @@ -2,8 +2,10 @@ from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from pathlib import Path +import os -DB_DIR = Path("/app") / "storage" / "db" +# Use a local path that's accessible in the current environment +DB_DIR = Path(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) / "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 619c30b..b3e868f 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,10 +1,15 @@ from logging.config import fileConfig +import os +import sys from sqlalchemy import engine_from_config from sqlalchemy import pool from alembic import context +# Add the parent directory to the Python path so that 'app' can be imported +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config