Files
serv_benchmark/backend/apply_migration_002.py
2025-12-08 05:42:52 +01:00

67 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
Apply SQL migration 002 to existing database
Migration 002: Add network_results_json column to benchmarks table
Usage: python apply_migration_002.py
"""
import sqlite3
import os
# Database path
DB_PATH = os.path.join(os.path.dirname(__file__), "data", "data.db")
MIGRATION_PATH = os.path.join(os.path.dirname(__file__), "migrations", "002_add_network_results.sql")
def apply_migration():
"""Apply the SQL migration 002"""
if not os.path.exists(DB_PATH):
print(f"❌ Database not found at {DB_PATH}")
print(" The database will be created automatically on first run.")
return
if not os.path.exists(MIGRATION_PATH):
print(f"❌ Migration file not found at {MIGRATION_PATH}")
return
print(f"📂 Database: {DB_PATH}")
print(f"📄 Migration: {MIGRATION_PATH}")
print()
# Read migration SQL
with open(MIGRATION_PATH, 'r') as f:
migration_sql = f.read()
# Connect to database
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
try:
# Check if column already exists
cursor.execute("PRAGMA table_info(benchmarks)")
columns = [row[1] for row in cursor.fetchall()]
if 'network_results_json' in columns:
print("⚠️ Migration 002 already applied (network_results_json column exists)")
print("✅ Database is up to date")
return
# Apply migration
print("🔄 Applying migration 002...")
cursor.executescript(migration_sql)
conn.commit()
print("✅ Migration 002 applied successfully!")
print()
print("New column added to benchmarks:")
print(" - network_results_json")
except sqlite3.Error as e:
print(f"❌ Error applying migration: {e}")
conn.rollback()
finally:
conn.close()
if __name__ == "__main__":
apply_migration()