Add Migration_067_FixMatchResults to fix incorrect UNDER/OVER values in match result field

- Added migration that fixes matches where result field contains UNDER or OVER
- Parses winning_outcomes JSON to find actual fight winner
- Sets under_over_result to the previous incorrect result value
- Runs automatically at application launch
parent 19d8b894
...@@ -7409,6 +7409,81 @@ class Migration_066_AddWalletTables(DatabaseMigration): ...@@ -7409,6 +7409,81 @@ class Migration_066_AddWalletTables(DatabaseMigration):
return False return False
class Migration_067_FixMatchResults(DatabaseMigration):
"""Fix match results that have UNDER/OVER in result field instead of actual fight winner"""
def __init__(self):
super().__init__("067", "Fix match results that have UNDER/OVER in result field")
def up(self, db_manager) -> bool:
"""Fix matches where result contains UNDER or OVER instead of actual fight winner"""
import json
try:
with db_manager.engine.connect() as conn:
# Find all matches where result is UNDER or OVER (incorrect)
result = conn.execute(text("""
SELECT id, result, winning_outcomes, under_over_result
FROM matches
WHERE result IN ('UNDER', 'OVER')
"""))
matches_to_fix = result.fetchall()
fixed_count = 0
for match in matches_to_fix:
match_id = match[0]
current_result = match[1]
winning_outcomes_json = match[2]
current_under_over = match[3]
new_result = None
new_under_over = current_under_over
# Parse winning_outcomes JSON to find actual fight winner
if winning_outcomes_json:
try:
winning_outcomes = json.loads(winning_outcomes_json)
if isinstance(winning_outcomes, list):
# Find the first outcome that is not UNDER or OVER
for outcome in winning_outcomes:
if outcome not in ('UNDER', 'OVER'):
new_result = outcome
break
except (json.JSONDecodeError, TypeError):
pass
# If we found a new result, update the match
if new_result:
# Set under_over_result to the old result value if not already set
if not current_under_over and current_result in ('UNDER', 'OVER'):
new_under_over = current_result
conn.execute(text("""
UPDATE matches
SET result = :new_result, under_over_result = :new_under_over
WHERE id = :match_id
"""), {
'new_result': new_result,
'new_under_over': new_under_over,
'match_id': match_id
})
fixed_count += 1
logger.info(f"Fixed match {match_id}: result changed from '{current_result}' to '{new_result}', under_over_result set to '{new_under_over}'")
conn.commit()
logger.info(f"Fixed {fixed_count} matches with incorrect result values")
return True
except Exception as e:
logger.error(f"Failed to fix match results: {e}")
return False
def down(self, db_manager) -> bool:
"""Cannot reliably rollback this migration as we don't store the original incorrect values"""
logger.warning("Cannot rollback Migration_067_FixMatchResults - original incorrect values not preserved")
return True
MIGRATIONS: List[DatabaseMigration] = [ MIGRATIONS: List[DatabaseMigration] = [
Migration_001_InitialSchema(), Migration_001_InitialSchema(),
Migration_002_AddIndexes(), Migration_002_AddIndexes(),
...@@ -7476,6 +7551,7 @@ MIGRATIONS: List[DatabaseMigration] = [ ...@@ -7476,6 +7551,7 @@ MIGRATIONS: List[DatabaseMigration] = [
Migration_064_AddIsPrimaryToCurrencies(), Migration_064_AddIsPrimaryToCurrencies(),
Migration_065_AddDefaultStreamingConfig(), Migration_065_AddDefaultStreamingConfig(),
Migration_066_AddWalletTables(), Migration_066_AddWalletTables(),
Migration_067_FixMatchResults(),
] ]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment