Update database path from /app/storage/db to /app/db
This commit is contained in:
parent
ca45718f42
commit
d3a49864ba
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user