Fix match result showing UNDER/OVER instead of actual fight winner

- Updated Migration_067 to use correct SQL for double-encoded JSON
- Fixed _set_match_status_and_result to properly handle UNDER/OVER results
- When result is UNDER/OVER, store it in under_over_result field
- Extract actual fight winner from winning_outcomes JSON

The winning_outcomes column is stored as a double-encoded JSON string:
- Outer layer: JSON string (e.g., '"[\"KO2\", \"WIN1\"]"')
- Inner layer: JSON array of outcome names

The fix uses JSON_UNQUOTE(JSON_EXTRACT(JSON_UNQUOTE(winning_outcomes), '$[0]'))
to extract the first outcome from the inner array.
parent 43e30e9d
......@@ -7416,59 +7416,67 @@ class Migration_067_FixMatchResults(DatabaseMigration):
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
"""Fix matches where result contains UNDER or OVER instead of actual fight winner
The winning_outcomes column is stored as a double-encoded JSON string:
- Outer layer: JSON string (e.g., '"[\"KO2\", \"WIN1\"]"')
- Inner layer: JSON array of outcome names
We need to:
1. Extract the first element from the inner JSON array
2. Set it as the result (the actual fight winner)
3. The under_over_result should already be set to UNDER/OVER
"""
try:
dialect = db_manager.engine.dialect.name
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]
if dialect in ('mysql', 'mariadb'):
# MySQL/MariaDB: Use JSON functions to extract the first element
# winning_outcomes is stored as: "[\"KO2\", \"WIN1\"]" (JSON string of JSON array)
# JSON_UNQUOTE removes the outer quotes, then JSON_EXTRACT gets the first element
result = conn.execute(text("""
UPDATE matches
SET result = JSON_UNQUOTE(JSON_EXTRACT(JSON_UNQUOTE(winning_outcomes), '$[0]'))
WHERE under_over_result IN ('UNDER', 'OVER')
AND winning_outcomes IS NOT NULL
AND JSON_VALID(winning_outcomes)
"""))
fixed_count = result.rowcount
else:
# SQLite: Use Python to parse the JSON
import json
result = conn.execute(text("""
SELECT id, result, winning_outcomes, under_over_result
FROM matches
WHERE under_over_result IN ('UNDER', 'OVER')
AND winning_outcomes IS NOT NULL
"""))
new_result = None
new_under_over = current_under_over
matches_to_fix = result.fetchall()
# Parse winning_outcomes JSON to find actual fight winner
if winning_outcomes_json:
for match in matches_to_fix:
match_id = match[0]
winning_outcomes_json = match[2]
try:
# Parse the double-encoded JSON
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
if isinstance(winning_outcomes, list) and len(winning_outcomes) > 0:
new_result = winning_outcomes[0]
if new_result not in ('UNDER', 'OVER'):
conn.execute(text("""
UPDATE matches
SET result = :new_result
WHERE id = :match_id
"""), {
'new_result': new_result,
'match_id': match_id
})
fixed_count += 1
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()
......
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