import os from pathlib import Path from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker # Get the project root directory (using an absolute path) PROJECT_ROOT = Path(__file__).parent.parent.parent.absolute() try: # Create the directory for the SQLite database if it doesn't exist DB_DIR = PROJECT_ROOT / "app" / "storage" / "db" DB_DIR.mkdir(parents=True, exist_ok=True) # Ensure the directory is writable test_file = DB_DIR / ".test_write_access" with open(test_file, "w") as f: f.write("test") os.remove(test_file) # Use a simplified database path for better compatibility DB_FILE = DB_DIR / "db.sqlite" SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_FILE}" print(f"Database URL: {SQLALCHEMY_DATABASE_URL}") print(f"Database directory: {DB_DIR}") engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}, # Echo SQL for debugging (remove in production) echo=False, ) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) except Exception as e: print(f"Error setting up database connection: {e}") raise