generate-model-dependency-fixtures 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. from __future__ import annotations
  3. from sentry.runner import configure
  4. configure()
  5. import click
  6. from sentry.backup.dependencies import (
  7. DependenciesJSONEncoder,
  8. dependencies,
  9. get_model_name,
  10. sorted_dependencies,
  11. )
  12. from sentry.testutils.factories import get_fixture_path # noqa
  13. encoder = DependenciesJSONEncoder(
  14. sort_keys=True,
  15. ensure_ascii=True,
  16. check_circular=True,
  17. allow_nan=True,
  18. indent=2,
  19. encoding="utf-8",
  20. )
  21. @click.command()
  22. def main():
  23. """Used for generating fixtures for the model dependency map."""
  24. click.echo("\nThis script can take up to 30 seconds, please be patient...\n")
  25. # Do all of the calculation before any file writes, so that we don't end up with partial
  26. # overwrites.
  27. detailed = {str(k): v for k, v in dependencies().items()}
  28. flat = {k: v.flatten() for k, v in detailed.items()}
  29. sorted = sorted_dependencies()
  30. truncate = [dependencies()[get_model_name(m)].table_name for m in sorted_dependencies()]
  31. det_path = get_fixture_path("backup", "model_dependencies", "detailed.json")
  32. with open(det_path, "w+") as fixture:
  33. fixture.write(encoder.encode(detailed))
  34. flat_path = get_fixture_path("backup", "model_dependencies", "flat.json")
  35. with open(flat_path, "w+") as fixture:
  36. fixture.write(encoder.encode(flat))
  37. det_path = get_fixture_path("backup", "model_dependencies", "sorted.json")
  38. with open(det_path, "w+") as fixture:
  39. fixture.write(encoder.encode(sorted))
  40. # Print tables in the order one would need to list them for a TRUNCATE statement.
  41. det_path = get_fixture_path("backup", "model_dependencies", "truncate.json")
  42. with open(det_path, "w+") as fixture:
  43. fixture.write(encoder.encode(truncate))
  44. click.echo(
  45. f"\nSuccess! The dependency mapping fixtures at {[det_path, flat_path]} were updated.\n"
  46. )
  47. if __name__ == "__main__":
  48. main()