#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Migration 004: Add links field to LogAggiornamenti Adds support for multiple web links in events (screening rooms, etc.) Usage: python run_migration_004.py # Usa environment corrente (DEV) python run_migration_004.py --env TEST # Forza environment TEST python run_migration_004.py --env PROD # Forza environment PROD """ import sqlite3 import sys import os from pathlib import Path # Parse command line arguments PRIMA di importare moduli target_env = None if len(sys.argv) > 1: if sys.argv[1] == '--env' and len(sys.argv) > 2: target_env = sys.argv[2].lower() print(f"[MIGRATION] Target environment: {target_env.upper()}") # Override environment PRIMA di importare app.context os.environ['MYICR_ENV'] = target_env # Fix PYTHONPATH per import current_dir = Path(__file__).resolve().parent mediatrack_dir = current_dir.parent.parent myicr_suite_dir = mediatrack_dir.parent.parent.parent if str(myicr_suite_dir) not in sys.path: sys.path.insert(0, str(myicr_suite_dir)) from app.modules.mediatrack.backend.database_logic import _conn, get_database_path def run_migration(): """Execute migration 004 on current environment database.""" db_path = get_database_path() print(f"[MIGRATION 004] Running on: {db_path}") if not Path(db_path).exists(): print(f" ERROR: Database not found: {db_path}") return False try: conn = sqlite3.connect(db_path) cur = conn.cursor() # Check if column already exists cur.execute("PRAGMA table_info(LogAggiornamenti)") columns = [col[1] for col in cur.fetchall()] if 'links' in columns: print(" ✓ Column 'links' already exists, skipping") conn.close() return True # Add links column print(" Adding 'links' column...") cur.execute("ALTER TABLE LogAggiornamenti ADD COLUMN links TEXT") conn.commit() print(" ✅ Migration 004 completed successfully") print(" - Added 'links' TEXT column to LogAggiornamenti") conn.close() return True except Exception as e: print(f" ❌ Error during migration: {e}") import traceback traceback.print_exc() return False if __name__ == '__main__': print("=" * 70) print("MIGRATION 004: Add Event Links") print("=" * 70) print() success = run_migration() if success: print() print("=" * 70) print("MIGRATION 004 COMPLETED SUCCESSFULLY") print("=" * 70) sys.exit(0) else: print() print("=" * 70) print("MIGRATION 004 FAILED") print("=" * 70) sys.exit(1)