Skip to content

Commit

Permalink
ref(backup): Standalone dependencies calculation
Browse files Browse the repository at this point in the history
Prior to this change, the `sort_dependencies` function was the only
public dependency calculation that the import/export system used, merely
returning a list of models in dependency order. This change splits part
of that functionality out, exposing an intermediate state where we can
see all of the model relations for a given model, including which fields
they occupy on the main model. This will be useful when we add
"foreign-key remapping" capabilities on import.

To facilitate this, we have added a couple of fixtures that capture the
state of the model dependency graph in source control, in both detailed
and flattened form. These serve a few purposes:

1. It maintains a record in source control of model dependency graph
   changes.
2. It notifies team-ospo of any such changes, allowing us to ensure that
   they don't break import/export functionality.
3. It serves as a simple golden test for the dependency resolution
   mechanism.

Issue: getsentry/team-ospo#171
  • Loading branch information
azaslavsky committed Aug 10, 2023
1 parent e074e3d commit 326776d
Show file tree
Hide file tree
Showing 9 changed files with 3,648 additions and 38 deletions.
53 changes: 53 additions & 0 deletions bin/generate-model-dependency-fixtures
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
from __future__ import annotations

from sentry.runner import configure

configure()

import click

from sentry.backup.dependencies import DependenciesJSONEncoder, dependencies, sorted_dependencies
from sentry.testutils.factories import get_fixture_path # noqa

encoder = DependenciesJSONEncoder(
sort_keys=True,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
indent=2,
encoding="utf-8",
)


@click.command()
def main():
"""Used for generating fixtures for the model dependency map."""

click.echo("\nThis script can take up to 30 seconds, please be patient...\n")

# Do all of the calculation before any file writes, so that we don't end up with partial
# overwrites.
detailed = dependencies()
flat = {k: v.flatten() for k, v in detailed.items()}
sorted = sorted_dependencies()

det_path = get_fixture_path("backup", "model_dependencies", "detailed.json")
with open(det_path, "w+") as fixture:
fixture.write(encoder.encode(detailed))

flat_path = get_fixture_path("backup", "model_dependencies", "flat.json")
with open(flat_path, "w+") as fixture:
fixture.write(encoder.encode(flat))

det_path = get_fixture_path("backup", "model_dependencies", "sorted.json")
with open(det_path, "w+") as fixture:
fixture.write(encoder.encode(sorted))

click.echo(
f"\nSuccess! The dependency mapping fixtures at {[det_path, flat_path]} were updated.\n"
)


if __name__ == "__main__":
main()
Loading

0 comments on commit 326776d

Please sign in to comment.