
- Updated database configuration to use /app/storage/db directory - Added robust directory and file permission handling - Implemented fallback paths for database location - Enhanced Alembic migration script with retry logic and clear error diagnostics - Updated the README with the new database path information
105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
import secrets
|
|
from pathlib import Path
|
|
from typing import List, Union
|
|
|
|
from pydantic import AnyHttpUrl, field_validator
|
|
from pydantic_settings import BaseSettings
|
|
|
|
import os
|
|
|
|
# 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:
|
|
# Try production path first, then local directories
|
|
paths_to_try = [
|
|
Path("/app/storage/db"), # Primary production path with proper permissions
|
|
Path("/app/storage"), # Alternate storage path
|
|
Path.cwd() / "storage/db", # Local development path
|
|
Path("/tmp/taskmanager"), # Fallback path
|
|
]
|
|
|
|
# Find the first writable path
|
|
DB_DIR = None
|
|
for path in paths_to_try:
|
|
try:
|
|
# Create directory with parents if it doesn't exist
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Explicitly set permissions if possible (755 = rwxr-xr-x)
|
|
try:
|
|
import stat
|
|
|
|
path_mode = (
|
|
stat.S_IRWXU
|
|
| stat.S_IRGRP
|
|
| stat.S_IXGRP
|
|
| stat.S_IROTH
|
|
| stat.S_IXOTH
|
|
)
|
|
os.chmod(path, path_mode)
|
|
except Exception as chmod_err:
|
|
print(f"Warning: Could not set permissions on {path}: {chmod_err}")
|
|
|
|
# Test if it's writable
|
|
test_file = path / ".write_test"
|
|
test_file.touch()
|
|
|
|
# Try to write to it
|
|
with open(test_file, "w") as f:
|
|
f.write("test")
|
|
|
|
# Read back to verify
|
|
with open(test_file, "r") as f:
|
|
content = f.read()
|
|
if content != "test":
|
|
raise Exception("Write verification failed")
|
|
|
|
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 Exception:
|
|
pass
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
PROJECT_NAME: str = "Task Manager API"
|
|
# No API version prefix - use direct paths
|
|
API_PREFIX: str = ""
|
|
SECRET_KEY: str = secrets.token_urlsafe(32)
|
|
# 60 minutes * 24 hours * 8 days = 8 days
|
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
|
|
|
# CORS Configuration
|
|
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
|
|
|
@field_validator("BACKEND_CORS_ORIGINS", mode="before")
|
|
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
|
|
if isinstance(v, str) and not v.startswith("["):
|
|
return [i.strip() for i in v.split(",")]
|
|
elif isinstance(v, (list, str)):
|
|
return v
|
|
raise ValueError(v)
|
|
|
|
# Database configuration
|
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
model_config = {"case_sensitive": True}
|
|
|
|
|
|
settings = Settings()
|