feat(migrations): run a schema migration on a given set of nodes

Merged
AJ
3d ago
+1045-55

Human summary

allow specify a list of UUIDs for all schema migrations so that they can be run in a more targeted manner. this will allow us to run the schema migrations only on the precise objects that need them instead of all objects. will provide the most value once integrated into merge and rebase where we will be able to re-run schema migrations on just the particular objects on the default or user branch that need the migration

Why

Merge and rebase are planned to apply a branch's stored schema changes again, in order, on the target branch (IFC-3623). Each migration applied again must change only the nodes that have not been through it yet. Today a schema migration always runs on every node of the changed kind, so applying it again damages data when a branch reuses a name (IFC-3081):

  • "rename a to b, then add a": applying the rename again also renames the new a.
  • "remove a, then add a": applying the removal again also deletes the new a.

This PR lets a caller run a schema migration on a given set of node IDs.

Not in this PR:

  • Choosing which nodes go in the set. IFC-3623 does that.
  • Any caller that passes node IDs. The schema load still runs every migration on all nodes.

Jira: IFC-3618, in the epic IFC-3590. Design: project J of INFP-719 Make schema migrations visible.

What changed

No behavior changes for users today: the schema load passes no node IDs, and each migration selects the same nodes as before.

What a caller can now do:

  • Pass a set of node IDs to a schema migration through MigrationInput.node_ids (frozenset[str] | None), or to the schema_path_migrate task through node_ids (list[str] | None). None means every node of the kind.
  • Count the nodes a migration will change in that set: the write and count_changes() use the same selection.

How the queries apply the set:

  • Each schema migration query opens its selection with MigrationSelectionQuery.render_node_match(). With node IDs, the MATCH filters on uuid IN $node_ids and carries USING INDEX SEEK on Node(uuid), so the query starts from a seek on the node IDs instead of a scan of the kinds. Without node IDs, it renders a MATCH on the kinds.

  • Every migration applies the set to profile and template nodes too: a profile or template node of the kind changes only when its ID is in the set. This includes the migrations that change only profile and template nodes:

    • attribute.optional.update
    • attribute.read_only.update
    • attribute.unique.update
    • the node uniqueness constraints update

    Running one of these on every profile node whatever the set would lose data when merge or rebase applies an earlier step again after a later one. For example, a branch makes color unique, which removes it from the profile nodes, then makes it non-unique, which adds it back, and a user sets color on a profile node. Applying the first step again would remove that value.

  • An empty set changes no node. It does not mean every node.

  • A single string passed as node_ids raises TypeError. A string would otherwise be read as a list of characters and match no node, without an error.

Other changes:

  • AttributeAddQuery takes node_ids in place of its uuids keyword. The missing attribute row repairers in graph/missing_attribute_rows/ pass node_ids.
  • AttributeSchemaMigration, the base class of every attribute migration, loses its uuids field, which no code read. Migration 043 stopped passing it.
  • dev/guides/backend/creating-migrations.md says to open a selection with render_node_match(), and that the set applies to profile and template nodes too.

How to review

  1. backend/infrahub/core/migrations/query/selection.py and backend/infrahub/core/migrations/shared.py: how the set reaches each query, and render_node_match().
  2. The query modules under backend/infrahub/core/migrations/query/ and backend/infrahub/core/migrations/schema/: each selection now opens with render_node_match().
  3. The tests:
    • test_migration_node_scope.py runs each migration on an empty set, then on a set, and checks that only the nodes in the set change. It then runs the migration on the remaining nodes. It requires a case for every migration in MIGRATION_MAP.
    • No test reads the query plans. The USING INDEX SEEK hint that render_node_match() adds makes a query with node IDs start from the Node(uuid) index.

Points that need attention:

  • A caller must include the IDs of the profile and template nodes it wants migrated. A set that holds only the main nodes leaves the profiles and templates of the kind on the old schema.
  • Removing a relationship closes the whole Relationship vertex, so its peer of the same kind also gets a new updated_at even when the peer is not in the set. The count includes only the node in the set.

How to test

uv run pytest backend/tests/unit/core/migrations/
uv run pytest backend/tests/component/core/migrations/schema/

Local results:

  • backend/tests/unit/core/migrations/: 24 passed.
  • backend/tests/component/core/migrations/schema/, with the uniqueness validation query tests: 267 passed and 10 xfailed.
  • The graph migration tests passed locally before the last changes. CI runs them on this commit.

Impact & rollout

  • Backward compatibility: no change to the API, the GraphQL schema or the database schema. AttributeAddQuery no longer takes uuids.
  • Performance: unchanged for the schema load, which passes no node IDs. With node IDs, each query starts from a seek on the Node(uuid) index.
  • Config/env changes: none.

Checklist

  • Tests added/updated
  • Changelog entry added: not needed, because users see no change until a caller passes node IDs
  • 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