99 lines
3.8 KiB
Python
Executable File
99 lines
3.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""
|
||
Script pour supprimer les anciennes tables après migration réussie
|
||
⚠️ ATTENTION: Cette opération est irréversible !
|
||
"""
|
||
|
||
import sys
|
||
import os
|
||
from sqlalchemy import create_engine, text
|
||
|
||
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./zebra.db")
|
||
|
||
def cleanup_old_tables():
|
||
print("🗑️ Nettoyage des anciennes tables")
|
||
print("=" * 50)
|
||
print("⚠️ ATTENTION: Cette opération est IRRÉVERSIBLE !")
|
||
print("⚠️ Assurez-vous que:")
|
||
print(" 1. La migration vers ProxmoxHost a réussi")
|
||
print(" 2. L'application fonctionne correctement avec /api/hosts")
|
||
print(" 3. Vous avez fait une sauvegarde de zebra.db")
|
||
|
||
response = input("\n❓ Êtes-vous sûr de vouloir supprimer les anciennes tables ? (oui/NON): ").strip()
|
||
if response.lower() != 'oui':
|
||
print("❌ Opération annulée.")
|
||
return False
|
||
|
||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||
|
||
try:
|
||
with engine.connect() as conn:
|
||
# Vérifier que la nouvelle table existe et contient des données
|
||
result = conn.execute(text("SELECT COUNT(*) FROM proxmox_hosts")).scalar()
|
||
if result == 0:
|
||
print("❌ La table 'proxmox_hosts' est vide!")
|
||
print(" Lancez d'abord la migration avec: python migrate_to_unified_hosts.py")
|
||
return False
|
||
|
||
print(f"✅ Table 'proxmox_hosts' contient {result} hosts")
|
||
|
||
# Vérifier si les anciennes tables existent encore
|
||
tables_to_drop = []
|
||
|
||
try:
|
||
conn.execute(text("SELECT COUNT(*) FROM servers")).scalar()
|
||
tables_to_drop.append("servers")
|
||
print("📋 Table 'servers' trouvée")
|
||
except:
|
||
pass
|
||
|
||
try:
|
||
conn.execute(text("SELECT COUNT(*) FROM proxmox_clusters")).scalar()
|
||
tables_to_drop.append("proxmox_clusters")
|
||
print("📋 Table 'proxmox_clusters' trouvée")
|
||
except:
|
||
pass
|
||
|
||
if not tables_to_drop:
|
||
print("ℹ️ Aucune ancienne table à supprimer.")
|
||
return True
|
||
|
||
print(f"\n🗑️ Tables à supprimer: {', '.join(tables_to_drop)}")
|
||
final_confirm = input("❓ Confirmer la suppression ? (oui/NON): ").strip()
|
||
if final_confirm.lower() != 'oui':
|
||
print("❌ Opération annulée.")
|
||
return False
|
||
|
||
# Supprimer les tables
|
||
for table in tables_to_drop:
|
||
conn.execute(text(f"DROP TABLE IF EXISTS {table}"))
|
||
print(f" ✅ Table '{table}' supprimée")
|
||
|
||
# Supprimer aussi les index associés
|
||
try:
|
||
conn.execute(text("DROP INDEX IF EXISTS ix_servers_id"))
|
||
conn.execute(text("DROP INDEX IF EXISTS ix_servers_name"))
|
||
conn.execute(text("DROP INDEX IF EXISTS ix_proxmox_clusters_id"))
|
||
conn.execute(text("DROP INDEX IF EXISTS ix_proxmox_clusters_name"))
|
||
print(" ✅ Index associés supprimés")
|
||
except:
|
||
pass
|
||
|
||
conn.commit()
|
||
|
||
print(f"\n🎉 Nettoyage terminé avec succès!")
|
||
print(f" - {len(tables_to_drop)} tables supprimées")
|
||
print(f" - La nouvelle architecture unifiée est maintenant en place")
|
||
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"❌ Erreur lors du nettoyage: {e}")
|
||
return False
|
||
|
||
def main():
|
||
success = cleanup_old_tables()
|
||
return 0 if success else 1
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main()) |