ipwatch
This commit is contained in:
62
backend/app/migrations/add_hardware_bench_field.py
Normal file
62
backend/app/migrations/add_hardware_bench_field.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""
|
||||
Script de migration pour ajouter le champ 'hardware_bench' à la table IP
|
||||
Exécuter avec: python -m backend.app.migrations.add_hardware_bench_field
|
||||
"""
|
||||
from sqlalchemy import text, create_engine
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
def migrate():
|
||||
"""Ajoute la colonne 'hardware_bench' et son index à la table IP"""
|
||||
try:
|
||||
db_path = os.getenv('DB_PATH', './data/db.sqlite')
|
||||
db_url = f"sqlite:///{db_path}"
|
||||
|
||||
engine = create_engine(db_url, echo=False)
|
||||
|
||||
with engine.connect() as conn:
|
||||
result = conn.execute(text("PRAGMA table_info(ip)"))
|
||||
columns = [row[1] for row in result]
|
||||
|
||||
if 'hardware_bench' in columns:
|
||||
print("✓ La colonne 'hardware_bench' existe déjà dans la table IP")
|
||||
return
|
||||
|
||||
print("→ Ajout de la colonne 'hardware_bench' à la table IP...")
|
||||
conn.execute(text("ALTER TABLE ip ADD COLUMN hardware_bench BOOLEAN DEFAULT 0"))
|
||||
|
||||
print("→ Création de l'index sur 'hardware_bench'...")
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_ip_hardware_bench ON ip(hardware_bench)"))
|
||||
|
||||
conn.commit()
|
||||
print("✓ Migration terminée avec succès!")
|
||||
print(" - Colonne 'hardware_bench' ajoutée")
|
||||
print(" - Index 'idx_ip_hardware_bench' créé")
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Erreur lors de la migration: {str(e)}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def rollback():
|
||||
"""Supprime la colonne 'hardware_bench' (rollback de la migration)"""
|
||||
try:
|
||||
db_path = os.getenv('DB_PATH', './data/db.sqlite')
|
||||
db_url = f"sqlite:///{db_path}"
|
||||
engine = create_engine(db_url, echo=False)
|
||||
|
||||
with engine.connect() as conn:
|
||||
print("⚠ Rollback non implémenté pour SQLite")
|
||||
print(" Pour annuler, restaurez une sauvegarde de la base de données")
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Erreur lors du rollback: {str(e)}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "rollback":
|
||||
rollback()
|
||||
else:
|
||||
migrate()
|
||||
Reference in New Issue
Block a user