CLAUDE: Fix DH showing on cards for players with defensive positions

Root cause: post_positions() was upserting cardpositions, leaving stale DH
entries from the previous buggy run where outfielders had no defensive
positions.

Solution: Modified post_positions() to DELETE all existing cardpositions for
the cardset before posting new ones. This ensures:
- Stale DH positions are removed when players gain defensive positions
- Cards show only current, accurate positions
- No phantom positions persist across script runs

Example: Ichiro previously had both "RF" and "DH" cardpositions. With this
fix, only "RF" remains after re-running the script.

Updated CLAUDE.md with explanation of the cleanup logic.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2025-11-09 11:48:36 -06:00
parent 5d7a0dd74b
commit c23f6d1ada
2 changed files with 15 additions and 1 deletions

View File

@ -256,3 +256,5 @@ of_run_rating = 'bis_runs_outfield' if 'bis_runs_outfield' in pos_df.columns els
```
**Verification**: Run `./scripts/check_positions.sh <cardset_id>` after card generation to catch this issue
**Additional Fix**: Modified `post_positions()` to DELETE all existing cardpositions for the cardset before posting new ones. This prevents stale DH positions from remaining in the database when players gain defensive positions after bug fixes.

View File

@ -1402,6 +1402,18 @@ async def post_pitching_ratings(ratings_df: pd.DataFrame):
async def post_positions(pos_df: pd.DataFrame):
# Delete all existing cardpositions for this cardset to avoid stale data
# (e.g., DH positions from buggy runs where outfielders had no defensive positions)
logger.info(f'Deleting existing cardpositions for cardset {CARDSET_ID}')
existing_positions = await db_get('cardpositions', params=[('cardset_id', CARDSET_ID)])
if existing_positions and existing_positions.get('count', 0) > 0:
for pos in existing_positions['positions']:
try:
await db_delete('cardpositions', object_id=pos['id'], timeout=1)
except Exception as e:
logger.warning(f'Failed to delete cardposition {pos["id"]}: {e}')
logger.info(f'Deleted {existing_positions["count"]} old cardpositions')
all_pos = []
def append_positions(row):