check_schema_delta.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  2. # Check that no schema deltas have been added to the wrong version.
  3. import re
  4. from typing import Any, Dict, List
  5. import click
  6. import git
  7. SCHEMA_FILE_REGEX = re.compile(r"^synapse/storage/schema/(.*)/delta/(.*)/(.*)$")
  8. @click.command()
  9. @click.option(
  10. "--force-colors",
  11. is_flag=True,
  12. flag_value=True,
  13. default=None,
  14. help="Always output ANSI colours",
  15. )
  16. def main(force_colors: bool) -> None:
  17. click.secho(
  18. "+++ Checking schema deltas are in the right folder",
  19. fg="green",
  20. bold=True,
  21. color=force_colors,
  22. )
  23. click.secho("Updating repo...")
  24. repo = git.Repo()
  25. repo.remote().fetch()
  26. click.secho("Getting current schema version...")
  27. r = repo.git.show("origin/develop:synapse/storage/schema/__init__.py")
  28. locals: Dict[str, Any] = {}
  29. exec(r, locals)
  30. current_schema_version = locals["SCHEMA_VERSION"]
  31. click.secho(f"Current schema version: {current_schema_version}")
  32. diffs: List[git.Diff] = repo.remote().refs.develop.commit.diff(None)
  33. seen_deltas = False
  34. bad_files = []
  35. for diff in diffs:
  36. if not diff.new_file or diff.b_path is None:
  37. continue
  38. match = SCHEMA_FILE_REGEX.match(diff.b_path)
  39. if not match:
  40. continue
  41. seen_deltas = True
  42. _, delta_version, _ = match.groups()
  43. if delta_version != str(current_schema_version):
  44. bad_files.append(diff.b_path)
  45. if not seen_deltas:
  46. click.secho(
  47. "No deltas found.",
  48. fg="green",
  49. bold=True,
  50. color=force_colors,
  51. )
  52. return
  53. if not bad_files:
  54. click.secho(
  55. f"All deltas are in the correct folder: {current_schema_version}!",
  56. fg="green",
  57. bold=True,
  58. color=force_colors,
  59. )
  60. return
  61. bad_files.sort()
  62. click.secho(
  63. "Found deltas in the wrong folder!",
  64. fg="red",
  65. bold=True,
  66. color=force_colors,
  67. )
  68. for f in bad_files:
  69. click.secho(
  70. f"\t{f}",
  71. fg="red",
  72. bold=True,
  73. color=force_colors,
  74. )
  75. click.secho()
  76. click.secho(
  77. f"Please move these files to delta/{current_schema_version}/",
  78. fg="red",
  79. bold=True,
  80. color=force_colors,
  81. )
  82. click.get_current_context().exit(1)
  83. if __name__ == "__main__":
  84. main()