File size: 994 Bytes
e418416 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.config import settings
import os
import platform
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Use /tmp on Linux (HF Spaces) to ensure write permissions
if platform.system() == "Linux":
db_path = "/tmp/andesops.db"
else:
db_path = os.path.join(BASE_DIR, "andesops.db")
default_db_path = f"sqlite:///{db_path}"
SQLALCHEMY_DATABASE_URL = settings.database_url or default_db_path
# SQLite specific config for FastAPI multi-threading
connect_args = {"check_same_thread": False} if SQLALCHEMY_DATABASE_URL.startswith("sqlite") else {}
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args=connect_args
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
|