feat(schema): cascade repository deletion to its managed objects

Merged
ID
4mo ago
+1440-84

Problem

Deleting a repository fails because the objects it imports/generates (transforms, checks, GraphQL queries, generators → artifact definitions → artifacts/validators, generator definitions → instances/validators, check definitions → user validators) are attached through mandatory relationships. Infrahub correctly refuses to orphan a mandatory child, so users are forced to delete the whole dependency tree by hand, leaf-first. Tracked in #3076.

Infrahub already has an on_delete cascade capability, but it was never wired to the repository-managed relationships.

Solution

  1. Wire the repository ownership tree with on_delete=CASCADE. Mark the repository's owner-side relationships (transformations, queries, checks, generators) as CASCADE, and add the missing parent-side relationships so the cascade reaches every leaf:

    • Transformation → ArtifactDefinition
    • ArtifactDefinition → Artifact, → ArtifactValidator
    • GeneratorDefinition → GeneratorInstance, → GeneratorValidator
    • CheckDefinition → UserValidator
    • GraphQLQuery → GraphQLQueryGroup

    Each new relationship reuses (or symmetrically auto-derives) its child-side identifier, so it maps to the existing edge — no data migration. This mirrors the existing ArtifactTarget.artifacts / Validator.checks cascade pattern.

  2. Make the mandatory-orphan check deep. NodeDeleteIndex now seeds its dependent-check from every cascade-reachable kind, not just the explicitly-deleted kind. Previously the check was shallow, so an incomplete cascade would silently orphan a mandatory child. Now, if a cascaded node is still referenced by a mandatory relationship from an object outside the delete set, deletion fails with a clear error instead of corrupting data. (This also fixed a latent re-queue bug in the cascade traversal for leaf kinds.)

  3. Cascade GraphQLQuery → GraphQLQueryGroup. CoreGraphQLQueryGroup.query is a mandatory back-reference and these groups are created routinely by the artifact/generator/computed-attribute pipelines. Without this, deleting a repository whose queries had ever run would be blocked — moving the wall of manual deletions one node over.

Accepted trade-off

Deleting a repository (or an artifact/generator/check definition directly) now also deletes validators attached to proposed changes and their generated artifacts/instances. This is the intended ownership semantics and the alternative is the manual wall-of-deletions this fixes.

Testing

  • Unit (test_delete_validator.py): asserts the cascade closure from both ReadOnlyRepository and Repository reaches every managed leaf (Artifact, ArtifactValidator, GeneratorInstance, GeneratorValidator, UserValidator, GraphQLQueryGroup).
  • Component (test_node_manager_delete.py, against live Neo4j): deleting a repository cascades through transform → artifact definition → artifact, plus check/generator definitions and generator instances; deleting a repository with a GraphQLQueryGroup succeeds and removes the query + group; an out-of-tree mandatory referrer to a cascaded node raises ValidationError instead of silently orphaning.
  • Regenerated protocols.py, schema.graphql, and frontend GraphQL types; schema validation and ruff/ty/mypy all pass.

Migration

No graph migration required. on_delete values and relationship definitions are reconciled into the graph by update_core_schema during infrahub upgrade (it diffs the code-defined candidate schema against the DB and upserts changed attribute values + new relationships), and the delete path reads on_delete from the in-memory registry that is populated from that reconciled graph. Graph migrations (mNNN_*) are only needed for structural changes to the meta-schema itself (e.g. m006 added the on_delete field to SchemaRelationship) — not for changing values on existing relationships or adding new ones. GRAPH_VERSION stays at 73.

Related issues

  • Closes #3076 — the bug this fixes (repository-managed objects not cleaned up on delete).
  • #2767 (closed) — the on_delete cascade schema capability this builds on; #2680 (closed) — its parent "schema integrity on node deletion" task. Both carry state/ref, so there is likely a corresponding INFP Jira ticket tracking the on_delete/cascade work worth linking.
  • #6849 (open) — related race-condition in cascade delete (not addressed here). #9790 (open) — sibling stale-artifact cleanup gap (different trigger).

Summary by cubic

Repository deletions now cascade to all repo-managed objects so a repo can be removed in one action. The delete validator checks mandatory dependents across the full cascade and blocks unsafe deletes with clear errors. Closes #3076.

  • Bug Fixes

    • Enabled on_delete=CASCADE from repositories to transformations, queries, checks, generators, and repository groups; added parent links so cascades reach: Transformation → ArtifactDefinition; ArtifactDefinition → Artifacts/Validators; GeneratorDefinition → Instances/Validators; GraphQLQuery → QueryGroups; CheckDefinition → UserValidators.
    • Delete validator now checks the full cascade closure and respects mandatory refs via Node/Profile/Template inherit_from; tests assert the exact deleted ID set and that validators attached only to a ProposedChange (not repo-owned) survive; regenerated backend protocols, schema.graphql, and frontend GraphQL types.
  • Migration

    • No graph migration needed; update_core_schema applies relationship changes during infrahub upgrade (GRAPH_VERSION unchanged).