Skip to content

fix: attribute source/owner on relationships in update_node#143

Open
PhillSimonds wants to merge 1 commit into
mainfrom
fix/update-node-relationship-attribution
Open

fix: attribute source/owner on relationships in update_node#143
PhillSimonds wants to merge 1 commit into
mainfrom
fix/update-node-relationship-attribution

Conversation

@PhillSimonds

@PhillSimonds PhillSimonds commented Jul 9, 2026

Copy link
Copy Markdown

Problem

update_node (infrahub_sync/adapters/infrahub.py) sets source/owner attribution on updated attributes but not on updated relationships. A relationship changed by a sync therefore carries no lineage back to the sync's source/owner — most visible for update-only flows, where an object is matched and only its relationships/attributes are updated (never created). This is asymmetric with the create path, where generate_payload_create(..., source=..., owner=..., is_protected=True) attributes attributes and relationships alike.

Fixes #142.

Fix

Assign relationships via a data dict ({"id", "source", "owner"}) instead of the bare peer, in both the cardinality-one and cardinality-many branches. The SDK's RelatedNode reads source/owner from that dict and serialises them as _relation__source / _relation__owner in the mutation input — the same attribution the attribute path already applies.

Before → after:

# cardinality one
- setattr(node, attr_name, peer_node)
+ setattr(node, attr_name, _relationship_input_data(peer_node.id, source, owner))

# cardinality many
- attr_manager.add(new_id)
+ attr_manager.add(_relationship_input_data(new_id, source, owner))

source/owner are omitted from the dict when unset, so behaviour is unchanged when no source/owner is configured.

Tests

New tests/adapters/test_infrahub_update_node_attribution.py (12 tests):

  • attributes still get source/owner (regression) — and none when unset
  • cardinality-one relationship carries source/owner (and only id when unset)
  • cardinality-many adds carry source/owner; stale peers still removed
  • a real RelatedNodeSync built from the helper serialises to _relation__source / _relation__owner

The relationship tests were confirmed to fail against the pre-fix code. Full unit suite: 122 passed / 2 skipped. ruff check, ruff format --check, and ty check are all clean (no new diagnostics vs main).

Note

Scoped to source/owner to mirror the attribute-update path exactly. The create path additionally sets is_protected=True on relationships; this PR intentionally does not change protection on update (that would be a behaviour change beyond the attribution gap). Happy to add it if maintainers prefer full create/update parity.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Relationship updates now carry the same attribution details as other updates, including source and owner when provided.
    • One-to-one and one-to-many relationship changes now preserve attribution correctly while still handling added and removed peers as expected.
    • Relationship updates without attribution continue to work normally, using only the peer identifier.

update_node stamped source/owner metadata on updated attributes but not on
updated relationships, so a relationship changed by a sync carried no lineage
back to the sync source/owner. This was asymmetric with the create path, which
attributes relationships via generate_payload_create.

Assign relationships via a data dict ({"id", "source", "owner"}) instead of the
bare peer, in both the cardinality-one and cardinality-many branches, so the
same attribution the attribute path applies is stamped on relationships too.

Adds unit tests covering attribute attribution (regression), cardinality-one
and cardinality-many relationships (with and without source/owner), and a
real-SDK check that the dict serialises to _relation__source/_relation__owner.

Fixes #142

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@PhillSimonds PhillSimonds requested a review from a team as a code owner July 9, 2026 01:44
@PhillSimonds PhillSimonds added the type/bug Something isn't working as expected label Jul 9, 2026
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Added a _relationship_input_data() helper in the Infrahub sync adapter that builds relationship payloads containing peer id plus optional source and owner fields. Updated update_node() to use this helper for both one-to-one relationship assignment and one-to-many relationship peer additions, replacing prior direct id/node assignments. Updated the update_node() docstring accordingly. Added a new test module with fake node/schema/client/manager stand-ins and regression tests verifying attribution behavior for attribute updates and both relationship cardinalities.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding source/owner attribution to relationship updates in update_node.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying infrahub-sync with  Cloudflare Pages  Cloudflare Pages

Latest commit: ca9f9f5
Status: ✅  Deploy successful!
Preview URL: https://bd1f4732.infrahub-sync.pages.dev
Branch Preview URL: https://fix-update-node-relationship.infrahub-sync.pages.dev

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adapter: update_node omits source/owner on relationship updates

1 participant