fix(node): preserve unfetched optional one-cardinality relationships on upsert#1079
Open
iddocohen wants to merge 1 commit into
Open
fix(node): preserve unfetched optional one-cardinality relationships on upsert#1079iddocohen wants to merge 1 commit into
iddocohen wants to merge 1 commit into
Conversation
…on upsert A node hydrated by `from_graphql` from a partial response would silently null-clear any optional cardinality-one relationship the response did not fetch, because the serialization gate could not distinguish "never loaded" from "explicitly cleared". Add `_peer_has_been_mutated` on `RelatedNodeBase`, flipped by `Node.__setattr__` on explicit assignment, and require it in the gate. Mirrors the existing `value_has_been_mutated` pattern on attributes.
Deploying infrahub-sdk-python with
|
| Latest commit: |
73a5d35
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://7cc79b1e.infrahub-sdk-python.pages.dev |
| Branch Preview URL: | https://ic-from-graphql-preserves-un.infrahub-sdk-python.pages.dev |
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## stable #1079 +/- ##
=======================================
Coverage 81.79% 81.79%
=======================================
Files 135 135
Lines 11643 11648 +5
Branches 1761 1761
=======================================
+ Hits 9523 9528 +5
Misses 1572 1572
Partials 548 548
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1080
Summary
A node hydrated by
from_graphqlfrom a partial GraphQL response would silently null-clear any optional cardinality-one relationship the response did not fetch, on the nextsave(allow_upsert=True). This is the kind of bug that surfaces as data corruption in downstream queries — orphan nodes that exist but are no longer reachable from their previous side of the relationship.The root cause is a conflation in the serialization gate at
infrahub_sdk/node/node.py:344-349. For an_existingnode, an "uninitialized" optional one-cardinality rel is emitted asnull. ButRelatedNode.initializedis a derived property (bool(id) or bool(hfid)), and that derivation cannot tell apart:node.rel = None.Both produce
id is None AND hfid is None. The gate was written to support case (2), but case (1) silently falls through it and clears data the user never intended to touch.Fix
Mirror the existing
Attribute.value_has_been_mutatedpattern on relationships:_peer_has_been_mutated: bool = FalsetoRelatedNodeBase.__init__.Truein both async and syncNode.__setattr__when the user assigns to a cardinality-one rel.self._existing AND rel._peer_has_been_mutatedbefore emittingnull.Net effect: nodes hydrated from partial payloads no longer leak
rel: nullinto upsert mutations. Explicitnode.rel = Nonestill clears the rel.Scope
RelationshipManager.initialized = data is not None(relationship.py:149) — fetched-empty and never-fetched are already distinguishable there.Test plan
test_update_input_data_existing_node_preserves_untouched_optional_relationshipfails on the old code, passes after the fix (both[standard]and[sync]variants).test_update_input_data_existing_node_with_optional_relationshipupdated to actually callnode.primary_tag = None(matching what its docstring already claimed) — verifies the explicit-clear path still works.uv run invoke format lint-codeclean.