fix(migrations): kind update selects only nodes on its branch

Merged
AJ
7d ago
+80-18

Why

#11638 pins with a strict expected failure that changing an attribute's kind also changes nodes that do not exist on the branch the migration runs on. On the default branch, the kind update selected nodes created on another branch, because their branch-agnostic attribute edges are on -global-, which every branch reads. It wrote edges on the default branch for those nodes, counted them as changes, and those edges took effect on the default branch when the other branch was merged.

Goal: the kind update selects only nodes that exist on its branch.

Non-goals:

  • The other defect #11638 pins: a rename of a branch-agnostic attribute on the default branch leaves the old attribute active, and a second rename renames again. Fixing it without writing branch-level edges on a branch-agnostic attribute needs changes to the retention check, described in IFC-3296.
  • Which branch the rename and the kind update write a branch-agnostic attribute's edges on (IFC-3296).

Follows #11638, which this pull request is based on.

What changed

Behavioral changes:

  • The kind update changes, and counts, only nodes whose latest IS_PART_OF edge on the branch is active. A node created on another branch, or deleted on the branch while an older branch still reads it, is skipped.

Implementation notes:

  • render_selection() in backend/infrahub/core/migrations/schema/attribute_kind_update.py checks the node's latest IS_PART_OF edge under the branch filter. The write and the count of nodes the migration will change share the selection, so both apply it.

Tests that pin known problems, without fixing them:

  • test_an_edit_after_an_agnostic_rename_on_a_user_branch_is_read_on_that_branch: after a branch-agnostic rename on a user branch, an edit of the renamed attribute is not shown on that branch, because the rename's value edge is on the branch and outranks the -global- edge the edit writes (IFC-3296).
  • test_a_kind_update_on_a_user_branch_keeps_the_values_read_on_that_branch: after a kind update between an indexed kind and TextArea on a user branch, the branch can read the attribute's default value, for a branch-aware and a branch-agnostic attribute. It is a non-strict expected failure because the result varies between runs. The kind update writes a deleted and a new value edge with the same branch level and timestamp, and the attribute read in backend/infrahub/core/query/node.py orders value edges without a status tiebreak. The same code is on develop.

What stayed the same:

  • The edges the kind update writes.
  • No database migration and no schema change.

How to review

  • The filter is in render_selection(); the test change is the removal of the strict expected failure in test_migration_selection_count.py.
  • The IS_PART_OF order has no status tiebreak, the same as the other migration selections.

How to test

T=backend/tests/component/core/migrations/schema
uv run pytest $T/test_migration_second_run.py $T/test_migration_selection_count.py \
  $T/test_attribute_kind_update.py $T/test_node_kind_update_generated_kinds.py
uv run invoke lint

Expected: 124 passed and 10 expected failures. The two cases of test_a_kind_update_on_a_user_branch_keeps_the_values_read_on_that_branch sometimes pass unexpectedly instead. lint passes.

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

šŸ¤– Generated with Claude Code