Files
ipwatch/backend/app/migrations/add_network_device_field.py
2026-02-07 16:57:37 +01:00

53 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Migration: Ajouter le champ network_device à la table ip
"""
import os
import sys
from sqlalchemy import text, create_engine
def main():
"""Ajoute le champ network_device à la table ip"""
# Récupérer le chemin de la base de données
db_path = os.getenv('DB_PATH', './data/db.sqlite')
db_url = f"sqlite:///{db_path}"
# Créer l'engine directement
engine = create_engine(db_url, echo=False)
print(f"📦 Migration: Ajout du champ network_device")
print(f"🗄️ Base de données: {db_path}")
try:
with engine.connect() as conn:
# Vérifier si la colonne existe déjà
result = conn.execute(text("PRAGMA table_info(ip)"))
columns = [row[1] for row in result]
if 'network_device' in columns:
print("⚠️ La colonne 'network_device' existe déjà. Migration ignorée.")
return
# Ajouter la colonne network_device
print(" Ajout de la colonne 'network_device'...")
conn.execute(text("""
ALTER TABLE ip
ADD COLUMN network_device BOOLEAN DEFAULT 0
"""))
# Créer un index sur la colonne
print("🔍 Création de l'index sur 'network_device'...")
conn.execute(text("""
CREATE INDEX IF NOT EXISTS idx_ip_network_device ON ip(network_device)
"""))
conn.commit()
print("✅ Migration réussie!")
except Exception as e:
print(f"❌ Erreur lors de la migration: {e}")
sys.exit(1)
if __name__ == "__main__":
main()