Spaces:
Sleeping
Sleeping
File size: 726 Bytes
67c8aca | 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 | from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
import os
# Persistent path for Docker/Hugging Face Spaces (Matches Bucket Mount)
DATA_DIR = "/data"
DB_PATH = os.path.join(DATA_DIR, "loan_db.db")
# Fallback to local root if not in Docker environment
if not os.path.exists(DATA_DIR):
DB_PATH = "./loan_db_v3.db"
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
|