reconcile_workflow_versions.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import annotations
  2. from sqlalchemy import text
  3. from app import create_app, db
  4. from app.core.data_factory.n8n_client import N8nClient, N8nClientError
  5. def main() -> None:
  6. app = create_app()
  7. with app.app_context():
  8. rows = db.session.execute(
  9. text(
  10. "SELECT dataflow_uid::text, environment, version_no, n8n_workflow_id "
  11. "FROM public.dataflow_workflow_versions WHERE status = 'active' "
  12. "ORDER BY dataflow_uid, environment"
  13. )
  14. )
  15. client = N8nClient()
  16. mismatch_count = 0
  17. for uid, environment, version_no, workflow_id in rows:
  18. try:
  19. workflow = client.get_workflow(workflow_id)
  20. remote_active = bool(workflow.get("active"))
  21. state = "match" if remote_active else "mismatch_remote_inactive"
  22. except N8nClientError as exc:
  23. state = f"unavailable:{exc.message}"
  24. if state != "match":
  25. mismatch_count += 1
  26. print(f"{uid}\t{environment}\tv{version_no}\t{workflow_id}\t{state}")
  27. print(f"mismatches={mismatch_count}; repair_performed=false")
  28. if __name__ == "__main__":
  29. main()