CLAUDE: Fix deletion running twice - only delete on first post_positions call

The post_positions function was being called twice (batters then pitchers).
Each call deleted ALL cardpositions, so the second call would delete the
batter positions that were just created.

Solution: Added delete_existing parameter (default False). Only the first
call (batters) sets delete_existing=True to clean up old data. The second
call (pitchers) just appends positions without deletion.

Result: Both batter and pitcher positions now persist correctly.

🤖 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 12:00:05 -06:00
parent 6746b51ca6
commit e4347a0162

View File

@ -1401,18 +1401,20 @@ async def post_pitching_ratings(ratings_df: pd.DataFrame):
log_exception(ValueError, 'Unable to post pitching ratings') log_exception(ValueError, 'Unable to post pitching ratings')
async def post_positions(pos_df: pd.DataFrame): async def post_positions(pos_df: pd.DataFrame, delete_existing: bool = False):
# Delete all existing cardpositions for this cardset to avoid stale data # Delete all existing cardpositions for this cardset to avoid stale data
# (e.g., DH positions from buggy runs where outfielders had no defensive positions) # (e.g., DH positions from buggy runs where outfielders had no defensive positions)
logger.info(f'Deleting existing cardpositions for cardset {CARDSET_ID}') # Only delete on the first call (batters), not the second call (pitchers)
existing_positions = await db_get('cardpositions', params=[('cardset_id', CARDSET_ID)]) if delete_existing:
if existing_positions and existing_positions.get('count', 0) > 0: logger.info(f'Deleting existing cardpositions for cardset {CARDSET_ID}')
for pos in existing_positions['positions']: existing_positions = await db_get('cardpositions', params=[('cardset_id', CARDSET_ID)])
try: if existing_positions and existing_positions.get('count', 0) > 0:
await db_delete('cardpositions', object_id=pos['id'], timeout=1) for pos in existing_positions['positions']:
except Exception as e: try:
logger.warning(f'Failed to delete cardposition {pos["id"]}: {e}') await db_delete('cardpositions', object_id=pos['id'], timeout=1)
logger.info(f'Deleted {existing_positions["count"]} old cardpositions') 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 = [] all_pos = []
@ -1462,8 +1464,8 @@ async def post_batter_data(bs: pd.DataFrame, bc: pd.DataFrame, br: pd.DataFrame,
left_on='key_bbref', left_on='key_bbref',
right_on='bbref_id' right_on='bbref_id'
) )
await post_positions(dr) await post_positions(dr, delete_existing=True) # Delete on first call (batters)
return len(all_players) return len(all_players)
@ -1499,8 +1501,8 @@ async def post_pitcher_data(ps: pd.DataFrame, pc: pd.DataFrame, pr: pd.DataFrame
left_on='bbref_id', left_on='bbref_id',
right_on='key_bbref' right_on='key_bbref'
) )
await post_positions(dr) await post_positions(dr, delete_existing=False) # Don't delete on second call (pitchers)
return len(all_players) return len(all_players)