Add automatic migration for accumulated_shortfall and cap_percentage

- Add Migration_014 to migrations.py for automatic application at launch
- Migration adds accumulated_shortfall and cap_percentage columns to match_reports and extraction_stats tables
- Migration will be automatically applied when the application starts
- Includes both Python migration class and SQL script for manual execution if needed
parent 99ecea2f
......@@ -1017,6 +1017,110 @@ class Migration_013_CreateMatchReportsTable(Migration):
def can_rollback(self) -> bool:
return True
class Migration_014_AddAccumulatedShortfallAndCapPercentage(Migration):
"""Add accumulated_shortfall and cap_percentage columns to match_reports and extraction_stats tables"""
def __init__(self):
super().__init__("014", "Add accumulated_shortfall and cap_percentage columns to match_reports and extraction_stats tables")
def up(self):
"""Add accumulated_shortfall and cap_percentage columns"""
try:
inspector = inspect(db.engine)
# Check if match_reports table exists
if 'match_reports' in inspector.get_table_names():
match_reports_columns = [col['name'] for col in inspector.get_columns('match_reports')]
if 'accumulated_shortfall' not in match_reports_columns:
logger.info("Adding accumulated_shortfall column to match_reports table...")
alter_table_sql = '''
ALTER TABLE match_reports
ADD COLUMN accumulated_shortfall DECIMAL(15,2) NOT NULL DEFAULT 0.00
'''
with db.engine.connect() as conn:
conn.execute(text(alter_table_sql))
conn.commit()
logger.info("accumulated_shortfall column added to match_reports table")
else:
logger.info("accumulated_shortfall column already exists in match_reports table")
if 'cap_percentage' not in match_reports_columns:
logger.info("Adding cap_percentage column to match_reports table...")
alter_table_sql = '''
ALTER TABLE match_reports
ADD COLUMN cap_percentage DECIMAL(5,2)
'''
with db.engine.connect() as conn:
conn.execute(text(alter_table_sql))
conn.commit()
logger.info("cap_percentage column added to match_reports table")
else:
logger.info("cap_percentage column already exists in match_reports table")
else:
logger.info("match_reports table does not exist yet, skipping migration")
# Check if extraction_stats table exists
if 'extraction_stats' in inspector.get_table_names():
extraction_stats_columns = [col['name'] for col in inspector.get_columns('extraction_stats')]
if 'accumulated_shortfall' not in extraction_stats_columns:
logger.info("Adding accumulated_shortfall column to extraction_stats table...")
alter_table_sql = '''
ALTER TABLE extraction_stats
ADD COLUMN accumulated_shortfall DECIMAL(15,2) NOT NULL DEFAULT 0.00
'''
with db.engine.connect() as conn:
conn.execute(text(alter_table_sql))
conn.commit()
logger.info("accumulated_shortfall column added to extraction_stats table")
else:
logger.info("accumulated_shortfall column already exists in extraction_stats table")
if 'cap_percentage' not in extraction_stats_columns:
logger.info("Adding cap_percentage column to extraction_stats table...")
alter_table_sql = '''
ALTER TABLE extraction_stats
ADD COLUMN cap_percentage DECIMAL(5,2)
'''
with db.engine.connect() as conn:
conn.execute(text(alter_table_sql))
conn.commit()
logger.info("cap_percentage column added to extraction_stats table")
else:
logger.info("cap_percentage column already exists in extraction_stats table")
else:
logger.info("extraction_stats table does not exist yet, skipping migration")
logger.info("Migration 014 completed successfully")
return True
except Exception as e:
logger.error(f"Migration 014 failed: {str(e)}")
raise
def down(self):
"""Drop accumulated_shortfall and cap_percentage columns from match_reports and extraction_stats tables"""
try:
with db.engine.connect() as conn:
# Drop columns from match_reports table
conn.execute(text("ALTER TABLE match_reports DROP COLUMN IF EXISTS accumulated_shortfall"))
conn.execute(text("ALTER TABLE match_reports DROP COLUMN IF EXISTS cap_percentage"))
# Drop columns from extraction_stats table
conn.execute(text("ALTER TABLE extraction_stats DROP COLUMN IF EXISTS accumulated_shortfall"))
conn.execute(text("ALTER TABLE extraction_stats DROP COLUMN IF EXISTS cap_percentage"))
conn.commit()
logger.info("Dropped accumulated_shortfall and cap_percentage columns from match_reports and extraction_stats tables")
return True
except Exception as e:
logger.error(f"Rollback of migration 014 failed: {str(e)}")
raise
def can_rollback(self) -> bool:
return True
class MigrationManager:
"""Manages database migrations and versioning"""
......@@ -1035,6 +1139,7 @@ class MigrationManager:
Migration_011_AddCapCompensationBalance(),
Migration_012_AddMatchNumberToBetsAndStats(),
Migration_013_CreateMatchReportsTable(),
Migration_014_AddAccumulatedShortfallAndCapPercentage(),
]
def ensure_version_table(self):
......
"""
Migration to add accumulated_shortfall and cap_percentage fields to match_reports and extraction_stats tables
"""
from app import db
def upgrade():
"""Add accumulated_shortfall and cap_percentage columns to match_reports and extraction_stats tables"""
try:
# Check if columns already exist in match_reports
inspector = db.inspect(db.engine)
match_reports_columns = [col['name'] for col in inspector.get_columns('match_reports')]
if 'accumulated_shortfall' not in match_reports_columns:
# Add accumulated_shortfall column to match_reports
with db.engine.connect() as conn:
conn.execute(db.text("""
ALTER TABLE match_reports
ADD COLUMN accumulated_shortfall NUMERIC(15, 2) DEFAULT 0.00
"""))
db.session.commit()
print("✓ Added accumulated_shortfall column to match_reports table")
else:
print("✓ accumulated_shortfall column already exists in match_reports table")
if 'cap_percentage' not in match_reports_columns:
# Add cap_percentage column to match_reports
with db.engine.connect() as conn:
conn.execute(db.text("""
ALTER TABLE match_reports
ADD COLUMN cap_percentage NUMERIC(5, 2)
"""))
db.session.commit()
print("✓ Added cap_percentage column to match_reports table")
else:
print("✓ cap_percentage column already exists in match_reports table")
# Check if columns already exist in extraction_stats
extraction_stats_columns = [col['name'] for col in inspector.get_columns('extraction_stats')]
if 'accumulated_shortfall' not in extraction_stats_columns:
# Add accumulated_shortfall column to extraction_stats
with db.engine.connect() as conn:
conn.execute(db.text("""
ALTER TABLE extraction_stats
ADD COLUMN accumulated_shortfall NUMERIC(15, 2) DEFAULT 0.00
"""))
db.session.commit()
print("✓ Added accumulated_shortfall column to extraction_stats table")
else:
print("✓ accumulated_shortfall column already exists in extraction_stats table")
if 'cap_percentage' not in extraction_stats_columns:
# Add cap_percentage column to extraction_stats
with db.engine.connect() as conn:
conn.execute(db.text("""
ALTER TABLE extraction_stats
ADD COLUMN cap_percentage NUMERIC(5, 2)
"""))
db.session.commit()
print("✓ Added cap_percentage column to extraction_stats table")
else:
print("✓ cap_percentage column already exists in extraction_stats table")
except Exception as e:
db.session.rollback()
print(f"✗ Error adding columns: {str(e)}")
raise
def downgrade():
"""Remove accumulated_shortfall and cap_percentage columns from match_reports and extraction_stats tables"""
try:
# Remove columns from match_reports table
with db.engine.connect() as conn:
conn.execute(db.text("""
ALTER TABLE match_reports
DROP COLUMN IF EXISTS accumulated_shortfall
"""))
conn.execute(db.text("""
ALTER TABLE match_reports
DROP COLUMN IF EXISTS cap_percentage
"""))
db.session.commit()
print("✓ Removed accumulated_shortfall and cap_percentage columns from match_reports table")
# Remove columns from extraction_stats table
with db.engine.connect() as conn:
conn.execute(db.text("""
ALTER TABLE extraction_stats
DROP COLUMN IF EXISTS accumulated_shortfall
"""))
conn.execute(db.text("""
ALTER TABLE extraction_stats
DROP COLUMN IF EXISTS cap_percentage
"""))
db.session.commit()
print("✓ Removed accumulated_shortfall and cap_percentage columns from extraction_stats table")
except Exception as e:
db.session.rollback()
print(f"✗ Error removing columns: {str(e)}")
raise
if __name__ == '__main__':
print("Running migration: Add accumulated_shortfall and cap_percentage columns")
print("=" * 60)
upgrade()
print("=" * 60)
print("Migration completed successfully!")
\ No newline at end of file
-- Migration to add accumulated_shortfall and cap_percentage fields to match_reports and extraction_stats tables
-- Run this SQL directly on your MySQL database
-- Add accumulated_shortfall column to match_reports table
ALTER TABLE match_reports
ADD COLUMN IF NOT EXISTS accumulated_shortfall NUMERIC(15, 2) DEFAULT 0.00;
-- Add cap_percentage column to match_reports table
ALTER TABLE match_reports
ADD COLUMN IF NOT EXISTS cap_percentage NUMERIC(5, 2);
-- Add accumulated_shortfall column to extraction_stats table
ALTER TABLE extraction_stats
ADD COLUMN IF NOT EXISTS accumulated_shortfall NUMERIC(15, 2) DEFAULT 0.00;
-- Add cap_percentage column to extraction_stats table
ALTER TABLE extraction_stats
ADD COLUMN IF NOT EXISTS cap_percentage NUMERIC(5, 2);
\ No newline at end of file
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