#!/usr/bin/env python3 """ Run method rotation system database migration. This script applies the rotation system database schema to the existing database. """ import sys import os import sqlite3 import logging from pathlib import Path # Add project root to path project_root = Path(__file__).parent sys.path.insert(0, str(project_root)) # Setup logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) def run_migration(): """Run the method rotation system migration""" try: # Database path db_path = project_root / "database" / "accounts.db" migration_path = project_root / "database" / "migrations" / "add_method_rotation_system.sql" if not db_path.exists(): logger.error(f"Database not found at {db_path}") return False if not migration_path.exists(): logger.error(f"Migration file not found at {migration_path}") return False # Read migration SQL with open(migration_path, 'r', encoding='utf-8') as f: migration_sql = f.read() # Connect to database logger.info(f"Connecting to database at {db_path}") conn = sqlite3.connect(str(db_path)) try: # Check if migration has already been run cursor = conn.cursor() cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='method_strategies'") if cursor.fetchone(): logger.info("Method rotation tables already exist, skipping migration") return True # Run migration logger.info("Running method rotation system migration...") conn.executescript(migration_sql) conn.commit() # Verify migration cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '%method%' OR name LIKE '%rotation%'") tables = cursor.fetchall() expected_tables = [ 'method_strategies', 'rotation_sessions', 'rotation_events', 'method_performance_analytics', 'method_cooldowns', 'platform_method_states' ] created_tables = [table[0] for table in tables] missing_tables = [t for t in expected_tables if t not in created_tables] if missing_tables: logger.error(f"Migration incomplete, missing tables: {missing_tables}") return False logger.info(f"Migration successful! Created tables: {created_tables}") # Verify default data was inserted cursor.execute("SELECT COUNT(*) FROM method_strategies") strategy_count = cursor.fetchone()[0] cursor.execute("SELECT COUNT(*) FROM platform_method_states") state_count = cursor.fetchone()[0] logger.info(f"Default data inserted: {strategy_count} strategies, {state_count} platform states") return True finally: conn.close() except Exception as e: logger.error(f"Migration failed: {e}") return False def check_migration_status(): """Check if migration has been applied""" try: db_path = project_root / "database" / "accounts.db" if not db_path.exists(): logger.warning(f"Database not found at {db_path}") return False conn = sqlite3.connect(str(db_path)) cursor = conn.cursor() # Check for rotation tables cursor.execute(""" SELECT name FROM sqlite_master WHERE type='table' AND ( name LIKE '%method%' OR name LIKE '%rotation%' ) ORDER BY name """) tables = [row[0] for row in cursor.fetchall()] conn.close() if tables: logger.info(f"Method rotation tables found: {tables}") return True else: logger.info("No method rotation tables found") return False except Exception as e: logger.error(f"Failed to check migration status: {e}") return False def main(): """Main function""" logger.info("Method Rotation System Database Migration") logger.info("=" * 50) # Check current status logger.info("Checking current migration status...") if check_migration_status(): logger.info("Migration already applied") return # Run migration logger.info("Applying migration...") if run_migration(): logger.info("Migration completed successfully!") logger.info("Method rotation system is now ready to use") else: logger.error("Migration failed!") sys.exit(1) if __name__ == "__main__": main()