Update database path from /app/storage/db to /app/db

This commit is contained in:
Automated Action 2025-05-16 06:54:20 +00:00
parent ca45718f42
commit d3a49864ba
3 changed files with 35 additions and 17 deletions

View File

@ -35,7 +35,7 @@ script_location = alembic
# are written from script.py.mako # are written from script.py.mako
# output_encoding = utf-8 # 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]
# post_write_hooks defines scripts or Python functions that are run # post_write_hooks defines scripts or Python functions that are run

View File

@ -7,26 +7,43 @@ from pydantic_settings import BaseSettings
import os import os
# Use a simple, universally accessible path for database # Use correct path for database in production environment
# First try environment variable, then local directory # First try environment variable, then predefined paths
DB_PATH = os.environ.get("DB_PATH") DB_PATH = os.environ.get("DB_PATH")
if DB_PATH: if DB_PATH:
DB_DIR = Path(DB_PATH) DB_DIR = Path(DB_PATH)
print(f"Using database path from environment: {DB_DIR}") print(f"Using database path from environment: {DB_DIR}")
else: else:
# Use 'db' directory in the current working directory # Try production path first, then local directory
DB_DIR = Path.cwd() / "db" paths_to_try = [
print(f"Using local database path: {DB_DIR}") Path("/app/db"), # Production path
Path.cwd() / "db", # Local development path
# Create database directory Path("/tmp/taskmanager") # Fallback path
try: ]
DB_DIR.mkdir(parents=True, exist_ok=True)
print(f"Created or verified database directory: {DB_DIR}") # Find the first writable path
except Exception as e: DB_DIR = None
print(f"Error creating database directory: {e}") for path in paths_to_try:
# Fall back to /tmp if we can't create our preferred directory try:
DB_DIR = Path("/tmp") path.mkdir(parents=True, exist_ok=True)
print(f"Falling back to temporary directory: {DB_DIR}") # 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): class Settings(BaseSettings):
PROJECT_NAME: str = "Task Manager API" PROJECT_NAME: str = "Task Manager API"

View File

@ -10,7 +10,7 @@ from sqlalchemy.exc import OperationalError
from app.db.base import Base # Import all models from app.db.base import Base # Import all models
from app.db.session import engine, db_file 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: def init_db() -> None:
@ -20,6 +20,7 @@ def init_db() -> None:
""" """
print(f"Initializing database at {db_file}") print(f"Initializing database at {db_file}")
print(f"Using SQLAlchemy URL: {settings.SQLALCHEMY_DATABASE_URL}") 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 # First try direct SQLite approach to ensure we have a basic database file
try: try: