Skip to content

Commit

Permalink
make branch_support static in DiffAllPathsQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtmccarty committed Nov 20, 2024
1 parent d3a49bb commit 0f28f9e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
19 changes: 10 additions & 9 deletions backend/infrahub/core/query/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,14 +528,12 @@ def __init__(
self,
base_branch: Branch,
diff_branch_from_time: Timestamp,
branch_support: list[BranchSupportType] | None = None,
current_node_field_specifiers: list[tuple[str, str]] | None = None,
new_node_field_specifiers: list[tuple[str, str]] | None = None,
**kwargs: Any,
):
self.base_branch = base_branch
self.diff_branch_from_time = diff_branch_from_time
self.branch_support = branch_support or [BranchSupportType.AWARE]
self.current_node_field_specifiers = current_node_field_specifiers
self.new_node_field_specifiers = new_node_field_specifiers

Expand All @@ -551,7 +549,9 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
"branch_from_time": self.diff_branch_from_time.to_string(),
"from_time": from_str,
"to_time": self.diff_to.to_string(),
"branch_support": [item.value for item in self.branch_support],
"branch_local": BranchSupportType.LOCAL.value,
"branch_aware": BranchSupportType.AWARE.value,
"branch_agnostic": BranchSupportType.AGNOSTIC.value,
"new_node_field_specifiers": self.new_node_field_specifiers,
"current_node_field_specifiers": self.current_node_field_specifiers,
}
Expand All @@ -577,7 +577,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
WHERE (node_ids_list IS NULL OR p.uuid IN node_ids_list)
AND (from_time <= diff_rel.from < $to_time)
AND (diff_rel.to IS NULL OR (from_time <= diff_rel.to < $to_time))
AND (p.branch_support IN $branch_support OR q.branch_support IN $branch_support)
AND p.branch_support = $branch_aware
WITH p, q, diff_rel, from_time
// -------------------------------------
// Exclude nodes added then removed on branch within timeframe
Expand All @@ -603,6 +603,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
WHERE %(id_func)s(diff_rel) = %(id_func)s(top_diff_rel)
AND type(r_node) IN ["HAS_ATTRIBUTE", "IS_RELATED"]
AND any(l in labels(node) WHERE l in ["Attribute", "Relationship"])
AND node.branch_support IN [$branch_aware, $branch_agnostic]
AND type(r_prop) IN ["IS_VISIBLE", "IS_PROTECTED", "HAS_SOURCE", "HAS_OWNER", "HAS_VALUE", "IS_RELATED"]
AND any(l in labels(prop) WHERE l in ["Boolean", "Node", "AttributeValue"])
AND ALL(
Expand Down Expand Up @@ -655,9 +656,9 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
// exclude attributes and relationships under added/removed nodes b/c they are covered above
WHERE (node_field_specifiers_list IS NULL OR [p.uuid, q.name] IN node_field_specifiers_list)
AND r_root.branch IN [$branch_name, $base_branch_name, $global_branch_name]
AND (p.branch_support IN $branch_support OR q.branch_support IN $branch_support)
AND q.branch_support = $branch_aware
// if p has a different type of branch support and was addded within our timeframe
AND (r_root.from < from_time OR NOT (p.branch_support IN $branch_support))
AND (r_root.from < from_time OR p.branch_support = $branch_agnostic)
AND r_root.status = "active"
// get attributes and relationships added on the branch during the timeframe
AND (from_time <= diff_rel.from < $to_time)
Expand All @@ -671,9 +672,9 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
// exclude attributes and relationships under added/removed nodes b/c they are covered above
WHERE (node_field_specifiers_list IS NULL OR [p.uuid, q.name] IN node_field_specifiers_list)
AND r_root.branch IN [$branch_name, $base_branch_name, $global_branch_name]
AND (p.branch_support IN $branch_support OR q.branch_support IN $branch_support)
AND q.branch_support = $branch_aware
// if p has a different type of branch support and was addded within our timeframe
AND (r_root.from < from_time OR NOT (p.branch_support IN $branch_support))
AND (r_root.from < from_time OR p.branch_support = $branch_agnostic)
// get attributes and relationships added on the branch during the timeframe
AND (from_time <= diff_rel.from < $to_time)
AND (diff_rel.to IS NULL OR (from_time <= diff_rel.to < $to_time))
Expand Down Expand Up @@ -773,7 +774,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
r in [r_root, r_node]
WHERE r.from <= from_time AND r.branch IN [$branch_name, $base_branch_name]
)
AND (p.branch_support IN $branch_support OR q.branch_support IN $branch_support)
AND p.branch_support = $branch_aware
AND any(l in labels(p) WHERE l in ["Attribute", "Relationship"])
AND type(diff_rel) IN ["IS_VISIBLE", "IS_PROTECTED", "HAS_SOURCE", "HAS_OWNER", "HAS_VALUE"]
AND any(l in labels(q) WHERE l in ["Boolean", "Node", "AttributeValue"])
Expand Down
39 changes: 30 additions & 9 deletions backend/tests/unit/core/diff/test_diff_and_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,31 +287,52 @@ async def test_local_nodes_added_on_branch(
enriched_diff = await diff_coordinator.update_branch_diff(base_branch=default_branch, diff_branch=branch2)
diff_person = enriched_diff.get_node(node_uuid=person.id)
assert diff_person.action is DiffAction.ADDED
# diff_car = enriched_diff.get_node(node_uuid=car.id)
# assert diff_car.action is DiffAction.ADDED
# validate car is not in the diff
with pytest.raises(ValueError, match=rf"No node {car.id}"):
enriched_diff.get_node(node_uuid=car.id)

diff_merger = await self._get_diff_merger(db=db, branch=branch2)
await diff_merger.merge_graph(at=Timestamp())

# validate person update on main
updated_person = await NodeManager.get_one(db=db, id=person.id)
assert updated_person.height.value == 180
assert updated_person.name.value == "Guy"
# updated_car = await NodeManager.get_one(db=db, id=car.id)
# assert updated_car.name.value == "camry"
# owner_rel = await updated_car.owner.get(db=db)
# assert owner_rel.peer_id == person.id
# validate car (branch=local) not merged to main
updated_car = await NodeManager.get_one(db=db, id=car.id)
assert updated_car is None
person_schema = registry.schema.get(name="TestPerson", duplicate=False)
cars_rel_schema = person_schema.get_relationship(name="cars")
cars_rels = await NodeManager.query_peers(
db=db, ids=[person.id], source_kind="TestPerson", schema=cars_rel_schema, filters={}, fetch_peers=True
)
assert len(cars_rels) == 1
assert cars_rels[0].peer_id == car.id

assert len(cars_rels) == 0
car_schema = registry.schema.get(name="TestCar", duplicate=False)
owner_rel_schema = car_schema.get_relationship(name="owner")
owner_rels = await NodeManager.query_peers(
db=db, ids=[car.id], source_kind="TestCar", schema=owner_rel_schema, filters={}, fetch_peers=True
)
assert len(owner_rels) == 0
# validate relationship still exists on branch
cars_rels = await NodeManager.query_peers(
db=db,
branch=branch2,
ids=[person.id],
source_kind="TestPerson",
schema=cars_rel_schema,
filters={},
fetch_peers=True,
)
assert len(cars_rels) == 1
assert cars_rels[0].peer_id == car.id
owner_rels = await NodeManager.query_peers(
db=db,
branch=branch2,
ids=[car.id],
source_kind="TestCar",
schema=owner_rel_schema,
filters={},
fetch_peers=True,
)
assert len(owner_rels) == 1
assert owner_rels[0].peer_id == person.id

0 comments on commit 0f28f9e

Please sign in to comment.