Spaces:
Running
Running
File size: 1,003 Bytes
bd84214 |
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 37 38 39 40 41 |
"""
Database connection and session management.
Uses SQLAlchemy 2.0 async-compatible patterns.
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.config import get_settings
settings = get_settings()
# Create engine - using psycopg3 driver
# Convert postgresql:// to postgresql+psycopg:// for psycopg3
db_url = settings.database_url
if db_url.startswith("postgresql://"):
db_url = db_url.replace("postgresql://", "postgresql+psycopg://", 1)
engine = create_engine(
db_url,
pool_pre_ping=True, # Verify connections before using
pool_size=5,
max_overflow=10
)
# Session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Base class for all models
Base = declarative_base()
def get_db():
"""
Dependency that provides a database session.
Ensures proper cleanup after request completes.
"""
db = SessionLocal()
try:
yield db
finally:
db.close()
|