fix(git): delete removed definitions after their dependents [IFC-2967]

Merged
OG
2mo ago
+383-39

Why

Removing a GraphQL query from .infrahub.yml in the same commit as the transform or generator definition that uses it wedges the repository import. The importer deletes stale objects in the same order it creates them, so it deletes the query while the transform still holds it through the mandatory query relationship. The delete fails with a 422, the import aborts with sync_status: error-import, the repository stays on the old commit, and every re-import fails the same way until someone deletes the pinning objects by hand.

Goal: a commit that removes or re-points repository-managed queries together with the transforms and generator definitions using them imports cleanly and advances the repository.

Closes #10109

Note that cubic initially complained about the order of operations and that this fix wouldn't resolve an issue where a target group wasn't deleted in the correct order. However this actually never happens today due to other reasons. I've opened #10826 as a follow up for this.

What changed

Behavioral changes:

  • Removing a query together with the transforms or generator definitions that use it (including the artifact definition, python transform and query trio from the ticket) imports cleanly and advances the commit.
  • Re-pointing a transform from query Q1 to Q2 and removing Q1 in one commit works: the transform is updated first, Q1 is deleted afterwards.
  • Removing the last remaining query from .infrahub.yml now deletes it. Before, an early return skipped the whole query reconcile when no queries were declared.
  • Deleting a removed transform also deletes every artifact definition that references it, plus their artifacts. This comes from the existing on_delete=CASCADE on CoreTransformation.artifact_definitions (1.11.0) and also applies to artifact definitions created outside the repository.

Implementation notes:

  • The five reconcile methods that delete (queries, python transforms, jinja2 transforms, checks, generators) now create and update only, and return the nodes the repository no longer declares, keyed by name.
  • apply_import_plan deletes them once every create and update has run (including the deferred objects), in reverse dependency order: generator and check definitions, then python and jinja2 transforms, then queries. The deletes stay inside the existing try, so a failure still sets error-import.
  • The "not found locally, deleting" log line now sits with the actual delete, in one format: <Kind> '<name>' not found locally in branch <branch>, deleting.
  • The single-category tasks (import_all_graphql_query, import_python_transforms, ...) delete what their reconcile returns straight away, so their behavior is unchanged.
  • The stale nodes stay local to apply_import_plan. ObjectImportPlan is the desired state read from the commit and is left untouched, and nothing crosses a Prefect task boundary.

What stayed the same:

  • No schema, migration or API changes.
  • _apply_artifact_definitions still never deletes. Its lookup is not limited to the repository, so deleting its only_graph list would remove other repositories' and UI-created definitions.
  • A delete blocked by an object the repository does not manage (for example another repository's transform using this repository's query) still aborts the import, as today.

Known limitations

The third blocker in #10109, a group from an objects file pinned by CoreGeneratorDefinition.targets or CoreArtifactDefinition.targets, cannot fire today: a repository import never deletes groups (#6886), so removing a definition together with its target group imports cleanly and leaves the group behind. Once #6886 is fixed, groups would be deleted when the object import finishes, before the definitions that target them, and the wedge would return. #10826 tracks deferring stale-object deletion as part of that fix.

CoreTransformation enforces a unique name. A commit that replaces a python transform foo with a jinja2 transform foo (or the reverse) now hits that constraint on the create, before the deferred delete runs, and the import errors out. Today the python to jinja2 direction works by accident. This is judged rare, and the error is actionable (rename, or delete the old transform). Detecting reused names and deleting those early is a possible future improvement.

Out of scope (follow-ups)

  • Deferring stale-object deletion until after the definition deletes, needed once #6886 makes the import delete groups (#10826).
  • Deleting artifact definitions removed from .infrahub.yml while their transform stays. Needs ownership detection and a product decision.
  • Limiting the artifact definition lookup to the repository. A repository declaring a name already used by another repository's or a UI-created definition silently takes that definition over.
  • Skipping instead of aborting when a delete is blocked by an object the repository does not manage.

How to review

  • backend/infrahub/git/integrator.py: apply_import_plan and _delete_stale_nodes carry the behavior change. The per-category edits are mechanical (the delete loop becomes a returned dict, plus the removed early return).
  • backend/tests/integration/git/test_import_removed_definitions.py: one class on a read-only repository. Each test commits a new .infrahub.yml, runs the read-only last-commit import workflow, and checks that the repository commit advanced and sync_status is in-sync. Every commit keeps one base query, so a removal commit still declares queries, as a real repository does. Without it, the early return would hide the ordering bug.

How to test

uv run pytest backend/tests/integration/git/test_import_removed_definitions.py

Covered cases: artifact definition plus its python transform plus that transform's query; a jinja2 transform plus its query; re-pointing a transform to a new query and removing the old one; a generator definition plus its query; the last remaining query.

Regression check: against the unfixed code, the ticket scenario fails on the removal commit with the ticket's exact error, Cannot delete CoreGraphQLQuery '...'. It is linked to mandatory relationship query on node CoreTransformPython '...' at CoreTransformPython.query. The repository then stays wedged, so the later tests fail on the same delete. The last-query test fails its deletion assertion through the early return.

Also run locally: /pre-ci (all applicable checks, backend unit tests 2685 passed), component backend/tests/component/git, integration backend/tests/integration/git and the proposed_change artifact-regeneration modules.

Impact & rollout

  • Backward compatibility: no breaking changes. Behavior differs only for commits that remove objects, as listed above, plus the name-swap limitation.- Deployment notes: safe to deploy. Targets stable for a 1.11.x patch and reaches develop through the regular merge.

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create ...)
  • External docs updated (if user-facing or ops-facing change)
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)
  • I have reviewed AI generated content

Assisted-by: creating-changelog-entries unversioned
Assisted-by: opsmill-dev-commit 0.1.0
Assisted-by: opsmill-dev-pr 0.2.0