import sqlite3 import os def main(): print("==========================================") print(" EJECUTANDO MIGRACION DE TABLAS PREMIUM") print("==========================================") dbs = ["musical_master.db", "melodix.db"] for db_file in dbs: if os.path.exists(db_file): print(f"\nMigrando base de datos: {db_file}...") conn = sqlite3.connect(db_file) cursor = conn.cursor() # 1. Agregar columna escudos_racha try: cursor.execute("ALTER TABLE progresos_plan ADD COLUMN escudos_racha INTEGER DEFAULT 0") conn.commit() print("[OK] Columna 'escudos_racha' agregada exitosamente.") except sqlite3.OperationalError as e: if "duplicate column name" in str(e).lower(): print("[INFO] La columna 'escudos_racha' ya existe. Saltando.") else: print(f"[ERROR] Error al agregar columna 'escudos_racha': {e}") # 2. Crear tabla torneos try: cursor.execute(""" CREATE TABLE IF NOT EXISTS torneos ( id INTEGER PRIMARY KEY AUTOINCREMENT, codigo TEXT UNIQUE, nombre TEXT, creador_id INTEGER, estado TEXT DEFAULT 'activo', ganador_id INTEGER, preguntas_json TEXT, desempate_preguntas_json TEXT, fecha_creacion TEXT, FOREIGN KEY(creador_id) REFERENCES users(id), FOREIGN KEY(ganador_id) REFERENCES users(id) ) """) conn.commit() print("[OK] Tabla 'torneos' creada exitosamente.") except Exception as e: print(f"[ERROR] Error al crear tabla 'torneos': {e}") # 3. Crear tabla participantes_torneo try: cursor.execute(""" CREATE TABLE IF NOT EXISTS participantes_torneo ( id INTEGER PRIMARY KEY AUTOINCREMENT, torneo_id INTEGER, user_id INTEGER, score INTEGER DEFAULT 0, tiempo_empleado REAL DEFAULT 0.0, score_desempate INTEGER DEFAULT 0, tiempo_desempate REAL DEFAULT 0.0, eliminado BOOLEAN DEFAULT 0, estado TEXT DEFAULT 'pendiente', FOREIGN KEY(torneo_id) REFERENCES torneos(id), FOREIGN KEY(user_id) REFERENCES users(id) ) """) conn.commit() print("[OK] Tabla 'participantes_torneo' creada exitosamente.") except Exception as e: print(f"[ERROR] Error al crear tabla 'participantes_torneo': {e}") conn.close() else: print(f"Base de datos {db_file} no encontrada. Saltando.") print("\n==========================================") print(" MIGRACION COMPLETADA CON EXITO") print("==========================================") if __name__ == "__main__": main()