Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only add deleted edges where necessary when deleting a relationship #4961

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading