#!/usr/bin/env python3
"""
Apply migration 012: Add pci_device_id field
"""
import sqlite3
import os

DB_PATH = "/home/gilles/projects/serv_benchmark/backend/data/peripherals.db"

def apply_migration():
    if not os.path.exists(DB_PATH):
        print(f"❌ Database not found: {DB_PATH}")
        return False

    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()

    try:
        # Check if column already exists
        cursor.execute("PRAGMA table_info(peripherals)")
        columns = [col[1] for col in cursor.fetchall()]

        if "pci_device_id" in columns:
            print("✅ Column pci_device_id already exists, skipping migration")
            return True

        # Add the column
        print("📝 Adding pci_device_id column...")
        cursor.execute("ALTER TABLE peripherals ADD COLUMN pci_device_id VARCHAR(20)")
        conn.commit()

        print("✅ Migration 012 applied successfully")
        return True

    except Exception as e:
        print(f"❌ Error applying migration: {e}")
        conn.rollback()
        return False

    finally:
        conn.close()

if __name__ == "__main__":
    apply_migration()
