prop_firm / app /database /base.py
zainkhan557
fresh initial commit
a63a304
Raw
History Blame Contribute Delete
1.47 kB
from sqlalchemy import create_engine, event
from sqlalchemy.orm import declarative_base, sessionmaker
from app.core.config import settings
# SQLite database initialize kar rahe hain
# 'check_same_thread=False' SQLite ke liye zaroori hai taake FastAPI async requests par crash na ho
# ── CONNECTION POOL FIX β€” pehle sirf 5+10=15 connections allow hote the,
# jo zyada users/background loops ke sath "QueuePool limit reached" error deta tha.
# Ab pool bada kar diya taake zyada users ek sath handle ho sakein. ──
is_sqlite = settings.DATABASE_URL.startswith("sqlite")
engine = create_engine(
settings.DATABASE_URL,
connect_args={"check_same_thread": False, "timeout": 30} if is_sqlite else {},
pool_size=50,
max_overflow=100,
pool_timeout=60,
pool_recycle=1800,
)
# ── SQLITE CONCURRENCY FIX β€” WAL mode (sirf SQLite use karne par chalega) ──
if is_sqlite:
@event.listens_for(engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA busy_timeout=30000")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.close()
# Har request ke liye database se raabta banaye rakhne ka session manager
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Is Base class ko extend kar ke hamare saare models (tables) banenge
Base = declarative_base()