store: PR review: paper-dynasty-database#45 — batch BattingCard/BattingCardRatings lookups (APPROVED)

This commit is contained in:
Cal Corum 2026-03-03 17:48:28 -06:00
parent 4ba245ddf4
commit bd50dd98b5

View File

@ -0,0 +1,32 @@
---
id: 71256008-18b8-4206-a036-909fe11ce901
type: workflow
title: "PR review: paper-dynasty-database#45 — batch BattingCard/BattingCardRatings lookups (APPROVED)"
tags: [pr-reviewer, paper-dynasty-database, python, peewee, performance, batch-query, sqlite]
importance: 0.4
confidence: 0.8
created: "2026-03-03T23:48:28.060018+00:00"
updated: "2026-03-03T23:48:28.060018+00:00"
---
## PR #45 — fix: batch BattingCard/BattingCardRatings lookups in lineup builder
**Verdict:** APPROVED (could not post — Gitea blocks self-review)
**Files reviewed:** `app/routers_v2/teams.py`
**What the PR does:**
- Before the position loop in `get_team_lineup`, batch-fetches all `BattingCard` rows for `legal_players backup_players` in one query
- Batch-fetches all `BattingCardRatings` for those cards in a second query
- Builds `_batting_cards_by_player` (player_id → BattingCard) and `_ratings_by_card_hand` (card_id → {hand → BattingCardRatings}) dicts
- Rewrites `get_bratings()` closure to do O(1) dict lookups instead of 3 DB queries per player
**Key correctness checks:**
- Dict keys match: `battingcard_id` (FK integer) in ratings dict == `this_bcard.id` in lookup
- Error behaviour preserved: missing card/ratings still raises AttributeError (caught by callers)
- Empty guard: `if _batting_cards_by_player else []` prevents empty IN clause
- Passing BattingCard model instances to Peewee `<<` is valid; PK extraction is automatic
**Pre-existing issues noted (out of scope):**
- Dead code block lines 544-576 (old batch approach, commented out)
- `get_bratings()` call at line 537 not wrapped in try/except