Allow match_number to be null in all report tables

parent 8af72eb7
...@@ -1238,7 +1238,7 @@ def api_reports_sync(): ...@@ -1238,7 +1238,7 @@ def api_reports_sync():
bet_detail = BetDetail( bet_detail = BetDetail(
bet_id=bet.id, bet_id=bet.id,
match_id=detail_data['match_id'], match_id=detail_data['match_id'],
match_number=detail_data['match_number'], match_number=detail_data.get('match_number'),
outcome=detail_data['outcome'], outcome=detail_data['outcome'],
amount=detail_data['amount'], amount=detail_data['amount'],
win_amount=detail_data.get('win_amount', 0.00), win_amount=detail_data.get('win_amount', 0.00),
......
...@@ -1231,6 +1231,124 @@ class Migration_015_AllowNullResults(Migration): ...@@ -1231,6 +1231,124 @@ class Migration_015_AllowNullResults(Migration):
return True return True
class Migration_016_AllowNullMatchNumber(Migration):
"""Allow NULL values for match_number column in bets, bet_details, extraction_stats, and match_reports tables"""
def __init__(self):
super().__init__("016", "Allow NULL values for match_number column in bets, bet_details, extraction_stats, and match_reports tables")
def up(self):
"""Allow NULL values for match_number column"""
try:
inspector = inspect(db.engine)
# Check and update bets table
if 'bets' in inspector.get_table_names():
bets_columns = {col['name']: col['nullable'] for col in inspector.get_columns('bets')}
if 'match_number' in bets_columns:
if bets_columns['match_number'] is False:
with db.engine.connect() as conn:
conn.execute(text("""
ALTER TABLE bets
MODIFY COLUMN match_number INT NULL
"""))
conn.commit()
logger.info("Updated match_number column to allow NULL in bets table")
else:
logger.info("match_number column already allows NULL in bets table")
# Check and update bet_details table
if 'bet_details' in inspector.get_table_names():
bet_details_columns = {col['name']: col['nullable'] for col in inspector.get_columns('bet_details')}
if 'match_number' in bet_details_columns:
if bet_details_columns['match_number'] is False:
with db.engine.connect() as conn:
conn.execute(text("""
ALTER TABLE bet_details
MODIFY COLUMN match_number INT NULL
"""))
conn.commit()
logger.info("Updated match_number column to allow NULL in bet_details table")
else:
logger.info("match_number column already allows NULL in bet_details table")
# Check and update extraction_stats table
if 'extraction_stats' in inspector.get_table_names():
extraction_stats_columns = {col['name']: col['nullable'] for col in inspector.get_columns('extraction_stats')}
if 'match_number' in extraction_stats_columns:
if extraction_stats_columns['match_number'] is False:
with db.engine.connect() as conn:
conn.execute(text("""
ALTER TABLE extraction_stats
MODIFY COLUMN match_number INT NULL
"""))
conn.commit()
logger.info("Updated match_number column to allow NULL in extraction_stats table")
else:
logger.info("match_number column already allows NULL in extraction_stats table")
# Check and update match_reports table
if 'match_reports' in inspector.get_table_names():
match_reports_columns = {col['name']: col['nullable'] for col in inspector.get_columns('match_reports')}
if 'match_number' in match_reports_columns:
if match_reports_columns['match_number'] is False:
with db.engine.connect() as conn:
conn.execute(text("""
ALTER TABLE match_reports
MODIFY COLUMN match_number INT NULL
"""))
conn.commit()
logger.info("Updated match_number column to allow NULL in match_reports table")
else:
logger.info("match_number column already allows NULL in match_reports table")
logger.info("Migration 016 completed successfully")
return True
except Exception as e:
logger.error(f"Migration 016 failed: {str(e)}")
raise
def down(self):
"""Revert match_number columns to NOT NULL"""
try:
with db.engine.connect() as conn:
# Revert bets column
conn.execute(text("""
ALTER TABLE bets
MODIFY COLUMN match_number INT NOT NULL
"""))
# Revert bet_details column
conn.execute(text("""
ALTER TABLE bet_details
MODIFY COLUMN match_number INT NOT NULL
"""))
# Revert extraction_stats column
conn.execute(text("""
ALTER TABLE extraction_stats
MODIFY COLUMN match_number INT NOT NULL
"""))
# Revert match_reports column
conn.execute(text("""
ALTER TABLE match_reports
MODIFY COLUMN match_number INT NOT NULL
"""))
conn.commit()
logger.info("Migration 016 rolled back successfully")
return True
except Exception as e:
logger.error(f"Rollback of migration 016 failed: {str(e)}")
raise
def can_rollback(self) -> bool:
return True
class MigrationManager: class MigrationManager:
"""Manages database migrations and versioning""" """Manages database migrations and versioning"""
...@@ -1251,6 +1369,7 @@ class MigrationManager: ...@@ -1251,6 +1369,7 @@ class MigrationManager:
Migration_013_CreateMatchReportsTable(), Migration_013_CreateMatchReportsTable(),
Migration_014_AddAccumulatedShortfallAndCapPercentage(), Migration_014_AddAccumulatedShortfallAndCapPercentage(),
Migration_015_AllowNullResults(), Migration_015_AllowNullResults(),
Migration_016_AllowNullMatchNumber(),
] ]
def ensure_version_table(self): def ensure_version_table(self):
......
...@@ -899,7 +899,7 @@ class Bet(db.Model): ...@@ -899,7 +899,7 @@ class Bet(db.Model):
client_id = db.Column(db.String(255), nullable=False, index=True) client_id = db.Column(db.String(255), nullable=False, index=True)
fixture_id = db.Column(db.String(255), nullable=False, index=True) fixture_id = db.Column(db.String(255), nullable=False, index=True)
match_id = db.Column(db.Integer, nullable=False, index=True) match_id = db.Column(db.Integer, nullable=False, index=True)
match_number = db.Column(db.Integer, nullable=False) match_number = db.Column(db.Integer, nullable=True)
bet_datetime = db.Column(db.DateTime, nullable=False, index=True) bet_datetime = db.Column(db.DateTime, nullable=False, index=True)
paid = db.Column(db.Boolean, default=False) paid = db.Column(db.Boolean, default=False)
paid_out = db.Column(db.Boolean, default=False) paid_out = db.Column(db.Boolean, default=False)
...@@ -940,7 +940,7 @@ class BetDetail(db.Model): ...@@ -940,7 +940,7 @@ class BetDetail(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
bet_id = db.Column(db.Integer, db.ForeignKey('bets.id'), nullable=False, index=True) bet_id = db.Column(db.Integer, db.ForeignKey('bets.id'), nullable=False, index=True)
match_id = db.Column(db.Integer, nullable=False, index=True) match_id = db.Column(db.Integer, nullable=False, index=True)
match_number = db.Column(db.Integer, nullable=False) match_number = db.Column(db.Integer, nullable=True)
outcome = db.Column(db.String(50), nullable=False) outcome = db.Column(db.String(50), nullable=False)
amount = db.Column(db.Numeric(15, 2), nullable=False) amount = db.Column(db.Numeric(15, 2), nullable=False)
win_amount = db.Column(db.Numeric(15, 2), default=0.00) win_amount = db.Column(db.Numeric(15, 2), default=0.00)
...@@ -974,7 +974,7 @@ class ExtractionStats(db.Model): ...@@ -974,7 +974,7 @@ class ExtractionStats(db.Model):
sync_id = db.Column(db.Integer, db.ForeignKey('report_syncs.id'), nullable=False, index=True) sync_id = db.Column(db.Integer, db.ForeignKey('report_syncs.id'), nullable=False, index=True)
client_id = db.Column(db.String(255), nullable=False, index=True) client_id = db.Column(db.String(255), nullable=False, index=True)
match_id = db.Column(db.Integer, nullable=False, index=True) match_id = db.Column(db.Integer, nullable=False, index=True)
match_number = db.Column(db.Integer, nullable=False) match_number = db.Column(db.Integer, nullable=True)
fixture_id = db.Column(db.String(255), nullable=False, index=True) fixture_id = db.Column(db.String(255), nullable=False, index=True)
match_datetime = db.Column(db.DateTime, nullable=False) match_datetime = db.Column(db.DateTime, nullable=False)
total_bets = db.Column(db.Integer, nullable=False) total_bets = db.Column(db.Integer, nullable=False)
...@@ -1110,7 +1110,7 @@ class MatchReport(db.Model): ...@@ -1110,7 +1110,7 @@ class MatchReport(db.Model):
# Match identification # Match identification
match_id = db.Column(db.Integer, nullable=False, index=True) match_id = db.Column(db.Integer, nullable=False, index=True)
match_number = db.Column(db.Integer, nullable=False) match_number = db.Column(db.Integer, nullable=True)
fixture_id = db.Column(db.String(255), nullable=False, index=True) fixture_id = db.Column(db.String(255), nullable=False, index=True)
match_datetime = db.Column(db.DateTime, nullable=False, index=True) match_datetime = db.Column(db.DateTime, nullable=False, index=True)
......
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