refactor(migrations): count the nodes a schema migration will change

Merged
AJ
7d ago
+1222-184

Why

Before a schema migration runs, Infrahub cannot say how many nodes it will change. Counting, batching and running a migration on a given set of nodes all need a node selection that is exactly the one the write uses, so that the count and the write cannot drift apart.

Non-goals:

  • Calling the count from the migration flow.
  • Adding up the counts for a whole migration.
  • Filtering by node ID.
  • Changing database migrations.

Jira: IFC-3615

What changed

  • A new Query subclass, MigrationSelectionQuery, has render_selection() and count_changes(). count_changes() runs the selection as a read query with the query's own parameters and returns the number of distinct nodes per kind.
  • Every query that a schema migration in MIGRATION_MAP runs now inherits from it. Each query's selection Cypher moved, unchanged, into render_selection(): the match on the kinds plus the active-state checks.
    • AttributeRemoveQuery: node.attribute.remove, and the profile and template removals.
    • AttributeAddQuery: node.attribute.add, and the profile and template additions.
    • AttributeRenameQuery: attribute.name.update.
    • NodeDuplicateQuery: node.name.update, node.namespace.update and node.inherit_from.update.
    • AttributeKindUpdateMigrationQuery: attribute.kind.update.
    • NodeRemoveMigrationQueryIn and NodeRemoveMigrationQueryOut: node.remove. The two queries now share one selection.
    • NodeRelationshipRemoveMigrationQuery: node.relationship.remove.
  • The count covers only nodes of the migrated kinds, including their profile and template kinds. Nodes of other kinds that a write changes as a side effect, such as the peer on the other side of a removed relationship, are not counted.
  • dev/guides/backend/creating-migrations.md describes the selection step for new schema migration queries.

What stayed the same: the Cypher text and the parameters of every write. A local check, not committed, rendered each query on main and on a branch with a fixed timestamp: 42 cases, including the m012 database migration subclasses. It compared the result with the base commit after every commit. RelationshipDuplicateQuery is used only by a database migration, so it is not changed.

How to review

  • Start with backend/infrahub/core/migrations/query/selection.py.
  • Then each query: the moved block should be identical to what was removed from the write.
    • attribute_add.py passes av and is_protected_value through its selection's WITH, so the write text is unchanged.
    • attribute_kind_update.py inserts the selection twice, as before.
  • Tests: backend/tests/component/core/migrations/schema/test_migration_selection_count.py and selection_dataset.py.

Two existing defects are pinned with xfail(strict=True) and are not fixed here. Both involve branch-agnostic data, whose edges are on -global- and visible from every branch:

  • Renaming an attribute of a branch-agnostic node on the default branch leaves the old attribute active (test_attribute_rename_agnostic_node_leaves_nothing_on_main).
  • On the default branch, AttributeKindUpdateMigrationQuery also selects the branch-agnostic attributes of nodes that exist only on another branch. Its selection does not check the node's IS_PART_OF edge (test_attribute_kind_update_agnostic_attribute[main]).

How to test

uv run pytest backend/tests/component/core/migrations -v
uv run pytest backend/tests/unit/core/migrations -v

For each query, on main and on a branch, the new tests check that the count:

  • equals a value written by hand from the test data;
  • equals the nodes of the migration's kinds that the write changed;
  • returns nothing when counted again after the write.

The data covers deleted nodes, nodes that exist only on the branch, profile and template nodes, branch-agnostic nodes, attributes and relationships, and a generic with an overriding kind. Replacing count(DISTINCT …) with count(*) makes the relationship cases with several peers fail.

Results:

  • backend/tests/component/core/migrations, unit migration tests and test_check_inheritance_cmd.py: 365 passed, 2 skipped, 1 xfailed, before the last test commit, which added one xfail.
  • test_migration_selection_count.py: 41 passed, 2 xfailed.

Impact & rollout

  • Backward compatibility: no change. The write Cypher is identical, and no migration calls the count yet.
  • Performance: no change to the writes. Measured locally on 10,000 nodes of one kind, the count took about 7% of the write's time for an attribute removal, and about 1% for a node removal.
  • Deployment notes: safe to deploy.

Checklist

  • Tests added/updated
  • Changelog entry added: not needed, no user-visible change
  • External docs updated: not needed
  • Internal .md docs updated (dev/guides/backend/creating-migrations.md)
  • I have reviewed AI generated content

šŸ¤– Generated with Claude Code