34 lines
908 B
Python
34 lines
908 B
Python
"""
|
|
Database configuration and session management
|
|
"""
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import declarative_base, sessionmaker, Session
|
|
from typing import Generator
|
|
|
|
from app.config import settings
|
|
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
connect_args={
|
|
"check_same_thread": False,
|
|
# SQLite performance optimizations for bulk imports
|
|
"timeout": 30,
|
|
} if "sqlite" in settings.database_url else {},
|
|
# Performance settings for bulk operations
|
|
pool_pre_ping=True,
|
|
pool_recycle=3600, # Recycle connections after 1 hour
|
|
echo=False # Set to True for SQL debugging
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""Get database session"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |