Spaces:
Sleeping
Sleeping
File size: 1,054 Bytes
d91bbbb 87047a5 d91bbbb 87047a5 | 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 | from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
import os
# HuggingFace pe /data exist nahi karta — fallback current dir pe
_default_db = "sqlite:///./proxy.db"
DATABASE_URL = os.getenv("DATABASE_URL", _default_db)
# Agar SQLite hai aur path /data se start hota hai to directory banao
if "sqlite" in DATABASE_URL:
db_path = DATABASE_URL.replace("sqlite:///", "").replace("sqlite:////", "/")
db_dir = os.path.dirname(db_path)
if db_dir and db_dir != "." and not os.path.exists(db_dir):
try:
os.makedirs(db_dir, exist_ok=True)
except Exception:
# fallback: current directory mein banao
DATABASE_URL = _default_db
engine = create_engine(
DATABASE_URL,
connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {},
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close() |