Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add a CI job to check that schema deltas are in the correct folder. #13063

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ jobs:
- run: scripts-dev/generate_sample_config.sh --check
- run: scripts-dev/config-lint.sh

check-schema-delta:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: "pip install 'click==8.1.1' 'GitPython>=3.1.20'"
Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd be using the poetry locked environment here (and indeed in check-sampleconfig and lint-newsfile)---but I won't block this on that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mainly did this as its so much faster than installing all the deps.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but FWIW there's a bunch of caching in https://github.com/matrix-org/setup-python-poetry which should help mitigate that

- run: scripts-dev/check_schema_delta.py

lint:
uses: "matrix-org/backend-meta/.github/workflows/python-poetry-ci.yml@v1"
with:
Expand Down Expand Up @@ -48,7 +56,7 @@ jobs:
# Dummy step to gate other tests on without repeating the whole list
linting-done:
if: ${{ !cancelled() }} # Run this even if prior jobs were skipped
needs: [lint, lint-crlf, lint-newsfile, check-sampleconfig]
needs: [lint, lint-crlf, lint-newsfile, check-sampleconfig, check-schema-delta]
runs-on: ubuntu-latest
steps:
- run: "true"
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ parameterized = ">=0.7.4"
idna = ">=2.5"

# The following are used by the release script
click = "==8.1.0"
click = "==8.1.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this'll be my fault for making scripts-dev by checked by mypy.

(Not sorry)

# GitPython was == 3.1.14; bumped to 3.1.20, the first release with type hints.
GitPython = ">=3.1.20"
commonmark = "==0.9.1"
Expand Down
84 changes: 84 additions & 0 deletions scripts-dev/check_schema_delta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3

# Check that no schema deltas have been added to the wrong version.

import re
from typing import Any, Dict, List

import click
import git

SCHEMA_FILE_REGEX = re.compile(r"^synapse/storage/schema/(.*)/delta/(.*)/(.*)$")


@click.command()
def main() -> None:
click.secho(
"+++ Checking schema deltas are in the right folder", fg="green", bold=True
)

click.secho("Updating repo...")

repo = git.Repo()
repo.remote().fetch()

click.secho("Getting current schema version...")

r = repo.git.show("origin/develop:synapse/storage/schema/__init__.py")

locals: Dict[str, Any] = {}
exec(r, locals)
current_schema_version = locals["SCHEMA_VERSION"]

click.secho(f"Current schema version: {current_schema_version}")

diffs: List[git.Diff] = repo.remote().refs.develop.commit.diff(None)

seen_deltas = False
bad_files = []
for diff in diffs:
if not diff.new_file or diff.b_path is None:
continue

match = SCHEMA_FILE_REGEX.match(diff.b_path)
if not match:
continue

seen_deltas = True

_, delta_version, _ = match.groups()

if delta_version != str(current_schema_version):
bad_files.append(diff.b_path)
Comment on lines +61 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes PRs that

  • want to bump the schema version from N to N+1, and
  • introduce new migrations in the N+1 directories
    fail CI. Is this intended?

Workaround for now is to split those two bullet points across separate PRs.


if not seen_deltas:
click.secho("No deltas found.", fg="green", bold=True)
return

if not bad_files:
click.secho(
f"All deltas are in the correct folder: {current_schema_version}!",
fg="green",
bold=True,
)
return

bad_files.sort()

click.secho("Found deltas in the wrong folder!", fg="red", bold=True)

for f in bad_files:
click.secho(f"\t{f}", fg="red", bold=True)

click.secho()
click.secho(
f"Please move these files to delta/{current_schema_version}/",
fg="red",
bold=True,
)

click.get_current_context().exit(1)


if __name__ == "__main__":
main()