Skip to content

Commit

Permalink
only add deleted edges where necessary when deleting a relationship (#…
Browse files Browse the repository at this point in the history
…4961)

* only add deleted edges where necessary when deleting a relationship

* account for branches

* add changelog
  • Loading branch information
ajtmccarty authored Nov 18, 2024
1 parent fb052ac commit 91828a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
31 changes: 30 additions & 1 deletion backend/infrahub/core/query/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,9 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None:
self.params["destination_id"] = self.destination_id
self.params["rel_id"] = self.rel.id
self.params["branch"] = self.branch.name
self.params["branch_level"] = self.branch.hierarchy_level
self.params["branch_names"] = self.branch.get_branches_in_scope()
self.params["rel_prop"] = self.get_relationship_properties_dict(status=RelationshipStatus.DELETED)
self.params["at"] = self.at.to_string()

arrows = self.schema.get_query_arrows()
r1 = f"{arrows.left.start}[r1:{self.rel_type} $rel_prop ]{arrows.left.end}"
Expand All @@ -454,22 +455,50 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None:
CALL {
WITH rl
MATCH (rl)-[edge:IS_VISIBLE]->(visible)
WHERE edge.branch IN $branch_names AND edge.to IS NULL AND edge.status = "active"
WITH rl, edge, visible
ORDER BY edge.branch_level DESC
LIMIT 1
CREATE (rl)-[deleted_edge:IS_VISIBLE $rel_prop]->(visible)
WITH edge
WHERE edge.branch = $branch
SET edge.to = $at
}
CALL {
WITH rl
MATCH (rl)-[edge:IS_PROTECTED]->(protected)
WHERE edge.branch IN $branch_names AND edge.to IS NULL AND edge.status = "active"
WITH rl, edge, protected
ORDER BY edge.branch_level DESC
LIMIT 1
CREATE (rl)-[deleted_edge:IS_PROTECTED $rel_prop]->(protected)
WITH edge
WHERE edge.branch = $branch
SET edge.to = $at
}
CALL {
WITH rl
MATCH (rl)-[edge:HAS_OWNER]->(owner_node)
WHERE edge.branch IN $branch_names AND edge.to IS NULL AND edge.status = "active"
WITH rl, edge, owner_node
ORDER BY edge.branch_level DESC
LIMIT 1
CREATE (rl)-[deleted_edge:HAS_OWNER $rel_prop]->(owner_node)
WITH edge
WHERE edge.branch = $branch
SET edge.to = $at
}
CALL {
WITH rl
MATCH (rl)-[edge:HAS_SOURCE]->(source_node)
WHERE edge.branch IN $branch_names AND edge.to IS NULL AND edge.status = "active"
WITH rl, edge, source_node
ORDER BY edge.branch_level DESC
LIMIT 1
CREATE (rl)-[deleted_edge:HAS_SOURCE $rel_prop]->(source_node)
WITH edge
WHERE edge.branch = $branch
SET edge.to = $at
}
""" % (
r1,
Expand Down
1 change: 1 addition & 0 deletions changelog/+extra-deleted-edges.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in the query to delete a relationship that could create unnecessary "deleted" edges on the database

0 comments on commit 91828a2

Please sign in to comment.