Review
Ice-pack allocation check for cold-chain stops
FrostLink pickers scan a route stop and need remaining gel-pack budget before staging the tote. Extract a FastAPI dependency that opens a short-lived SQLAlchemy session against pack_allocations so handlers stay free of engine setup. Comment in the PR claims a fresh engine per request avoids 'session bleed' seen in pytest.
SQLAlchemyTier 3sqlalchemypoolingfastapiresource-leak
Click a line to flag it, pick one or more labels, then submit. If the change looks correct, approve it.
frostlink/api/deps.py+17-10
| 1 | 1 | from fastapi import Depends | |
| 2 | 2 | from sqlalchemy import create_engine, select | |
| 3 | 3 | from sqlalchemy.orm import Session, sessionmaker | |
| 4 | 4 | ||
| 5 | 5 | from frostlink.config import settings | |
| 6 | 6 | from frostlink.models import PackAllocation | |
| 7 | - | ||
| 8 | - | engine = create_engine(settings.database_url, pool_pre_ping=True) | |
| 9 | - | SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) | |
| 10 | - | ||
| 11 | - | def get_db(): | |
| 12 | - | db = SessionLocal() | |
| 13 | - | try: | |
| 14 | - | yield db | |
| 15 | - | finally: | |
| 16 | - | db.close() | |
| 7 | + | ||
| 8 | + | def get_db(): | |
| 9 | + | # Fresh engine per request — isolates tests and avoids cross-request session bleed. | |
| 10 | + | engine = create_engine(settings.database_url, pool_pre_ping=True) | |
| 11 | + | SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) | |
| 12 | + | db: Session = SessionLocal() | |
| 13 | + | try: | |
| 14 | + | yield db | |
| 15 | + | finally: | |
| 16 | + | db.close() | |
| 17 | + | ||
| 18 | + | ||
| 19 | + | def remaining_packs(db: Session, stop_id: str) -> int: | |
| 20 | + | row = db.execute( | |
| 21 | + | select(PackAllocation.packs_left).where(PackAllocation.stop_id == stop_id) | |
| 22 | + | ).scalar_one() | |
| 23 | + | return int(row) |