From d3a49864ba7c8bbc31bbfa819ee346f61c852469 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Fri, 16 May 2025 06:54:20 +0000 Subject: [PATCH] Update database path from /app/storage/db to /app/db --- alembic.ini | 2 +- app/core/config.py | 47 +++++++++++++++++++++++++++++++--------------- app/db/init_db.py | 3 ++- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/alembic.ini b/alembic.ini index 612fb12..a4d5407 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 +sqlalchemy.url = sqlite:////app/db/db.sqlite [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/app/core/config.py b/app/core/config.py index 575986c..1152aa7 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -7,26 +7,43 @@ from pydantic_settings import BaseSettings import os -# Use a simple, universally accessible path for database -# First try environment variable, then local directory +# Use correct path for database in production environment +# First try environment variable, then predefined paths DB_PATH = os.environ.get("DB_PATH") if DB_PATH: DB_DIR = Path(DB_PATH) print(f"Using database path from environment: {DB_DIR}") else: - # Use 'db' directory in the current working directory - DB_DIR = Path.cwd() / "db" - print(f"Using local database path: {DB_DIR}") - -# Create database directory -try: - DB_DIR.mkdir(parents=True, exist_ok=True) - print(f"Created or verified database directory: {DB_DIR}") -except Exception as e: - print(f"Error creating database directory: {e}") - # Fall back to /tmp if we can't create our preferred directory - DB_DIR = Path("/tmp") - print(f"Falling back to temporary directory: {DB_DIR}") + # Try production path first, then local directory + paths_to_try = [ + Path("/app/db"), # Production path + Path.cwd() / "db", # Local development path + Path("/tmp/taskmanager") # Fallback path + ] + + # Find the first writable path + DB_DIR = None + for path in paths_to_try: + try: + path.mkdir(parents=True, exist_ok=True) + # Test if it's writable + test_file = path / ".write_test" + test_file.touch() + test_file.unlink() # Remove the test file + DB_DIR = path + print(f"Using database path: {DB_DIR}") + break + except Exception as e: + print(f"Cannot use path {path}: {e}") + + # Last resort fallback + if DB_DIR is None: + DB_DIR = Path("/tmp") + print(f"Falling back to temporary directory: {DB_DIR}") + try: + Path("/tmp").mkdir(exist_ok=True) + except: + pass class Settings(BaseSettings): PROJECT_NAME: str = "Task Manager API" diff --git a/app/db/init_db.py b/app/db/init_db.py index c3f57aa..4460f84 100644 --- a/app/db/init_db.py +++ b/app/db/init_db.py @@ -10,7 +10,7 @@ from sqlalchemy.exc import OperationalError from app.db.base import Base # Import all models from app.db.session import engine, db_file -from app.core.config import settings +from app.core.config import settings, DB_DIR def init_db() -> None: @@ -20,6 +20,7 @@ def init_db() -> None: """ print(f"Initializing database at {db_file}") print(f"Using SQLAlchemy URL: {settings.SQLALCHEMY_DATABASE_URL}") + print(f"DB_DIR is set to: {DB_DIR} (this should be /app/db in production)") # First try direct SQLite approach to ensure we have a basic database file try: