ipwatch
This commit is contained in:
52
backend/app/migrations/add_network_device_field.py
Normal file
52
backend/app/migrations/add_network_device_field.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/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()
|
||||
Reference in New Issue
Block a user