feat(migrations): run a schema migration on a given set of nodes
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
atob, then adda": applying the rename again also renames the newa. - "remove
a, then adda": applying the removal again also deletes the newa.
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 theschema_path_migratetask throughnode_ids(list[str] | None).Nonemeans 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, theMATCHfilters onuuid IN $node_idsand carriesUSING INDEX SEEKonNode(uuid), so the query starts from a seek on the node IDs instead of a scan of the kinds. Without node IDs, it renders aMATCHon 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.updateattribute.read_only.updateattribute.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
colorunique, which removes it from the profile nodes, then makes it non-unique, which adds it back, and a user setscoloron 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_idsraisesTypeError. A string would otherwise be read as a list of characters and match no node, without an error.
Other changes:
AttributeAddQuerytakesnode_idsin place of itsuuidskeyword. The missing attribute row repairers ingraph/missing_attribute_rows/passnode_ids.AttributeSchemaMigration, the base class of every attribute migration, loses itsuuidsfield, which no code read. Migration 043 stopped passing it.dev/guides/backend/creating-migrations.mdsays to open a selection withrender_node_match(), and that the set applies to profile and template nodes too.
How to review
backend/infrahub/core/migrations/query/selection.pyandbackend/infrahub/core/migrations/shared.py: how the set reaches each query, andrender_node_match().- The query modules under
backend/infrahub/core/migrations/query/andbackend/infrahub/core/migrations/schema/: each selection now opens withrender_node_match(). - The tests:
test_migration_node_scope.pyruns 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 inMIGRATION_MAP.- No test reads the query plans. The
USING INDEX SEEKhint thatrender_node_match()adds makes a query with node IDs start from theNode(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_ateven 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.
AttributeAddQueryno longer takesuuids. - 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