init_db.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. """Initialise (ou migre) la base de données SQLite. Appelé par les scripts install et upgrade."""
  3. import os
  4. import sqlite3
  5. import sys
  6. config_path = sys.argv[1] if len(sys.argv) > 1 else None
  7. if config_path:
  8. os.environ["BACKUPMANAGER_CONFIG"] = config_path
  9. # Lire DB_PATH directement depuis le fichier de config (sans importer app/SQLAlchemy)
  10. # pour pouvoir migrer le schéma AVANT que l'import de app ne tente de requêter la DB.
  11. _cfg = {}
  12. if config_path and os.path.exists(config_path):
  13. with open(config_path) as _f:
  14. exec(compile(_f.read(), config_path, "exec"), _cfg)
  15. db_path = _cfg.get("DB_PATH") or os.path.join(
  16. os.path.dirname(os.path.abspath(__file__)), "backupmanager.db"
  17. )
  18. # Migrations SQLite directes — avant tout import de SQLAlchemy/app
  19. if os.path.exists(db_path):
  20. _conn = sqlite3.connect(db_path)
  21. _cur = _conn.execute("PRAGMA table_info(jobs)")
  22. existing_cols = {row[1] for row in _cur.fetchall()}
  23. migrations = [
  24. ("destination_id", "ALTER TABLE jobs ADD COLUMN destination_id INTEGER REFERENCES destinations(id)"),
  25. ("remote_instance_id","ALTER TABLE jobs ADD COLUMN remote_instance_id INTEGER REFERENCES remote_instances(id)"),
  26. ]
  27. for col, sql in migrations:
  28. if col not in existing_cols:
  29. _conn.execute(sql)
  30. _conn.commit()
  31. print(f"Migration : colonne {col} ajoutée à jobs.")
  32. _conn.close()
  33. # Import de app après les migrations — SQLAlchemy peut désormais requêter la DB
  34. from app import app, db
  35. with app.app_context():
  36. db.create_all()
  37. print("Base de données initialisée.")