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:
parent
6746b51ca6
commit
e4347a0162
@ -1401,18 +1401,20 @@ async def post_pitching_ratings(ratings_df: pd.DataFrame):
|
||||
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
|
||||
# (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')
|
||||
# Only delete on the first call (batters), not the second call (pitchers)
|
||||
if delete_existing:
|
||||
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 = []
|
||||
|
||||
@ -1462,8 +1464,8 @@ async def post_batter_data(bs: pd.DataFrame, bc: pd.DataFrame, br: pd.DataFrame,
|
||||
left_on='key_bbref',
|
||||
right_on='bbref_id'
|
||||
)
|
||||
await post_positions(dr)
|
||||
|
||||
await post_positions(dr, delete_existing=True) # Delete on first call (batters)
|
||||
|
||||
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',
|
||||
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)
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user