Skip to content

Commit 95613a5

Browse files
committed
Update Neo4j CALL() new queries fix where
1 parent ecca4f8 commit 95613a5

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

backend/infrahub/core/diff/query/merge.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
5454
// ------------------------------
5555
// only make IS_PART_OF updates if node is ADDED or REMOVED
5656
// ------------------------------
57+
WITH n, node_diff_map, node_rel_status
5758
WHERE node_rel_status IS NOT NULL
5859
MATCH (root:Root)
5960
// ------------------------------
6061
// set IS_PART_OF.to, optionally, target branch
6162
// ------------------------------
62-
WITH root
63+
WITH root, n, node_rel_status
6364
CALL (root, n, node_rel_status) {
6465
OPTIONAL MATCH (root)<-[target_r_root:IS_PART_OF {branch: $target_branch, status: "active"}]-(n)
6566
WHERE node_rel_status = "deleted"
@@ -69,7 +70,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
6970
// ------------------------------
7071
// create new IS_PART_OF relationship on target_branch
7172
// ------------------------------
72-
WITH root
73+
WITH root, n, node_rel_status
7374
CALL (root, n, node_rel_status) {
7475
OPTIONAL MATCH (root)<-[r_root:IS_PART_OF {branch: $target_branch}]-(n)
7576
WHERE r_root.status = node_rel_status
@@ -85,6 +86,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
8586
// shortcut to delete all attributes and relationships for this node if the node is deleted
8687
// ------------------------------
8788
CALL (n, node_rel_status) {
89+
WITH n, node_rel_status
8890
WHERE node_rel_status = "deleted"
8991
CALL (n) {
9092
OPTIONAL MATCH (n)-[rel1:IS_RELATED]-(:Relationship)-[rel2]-(p)
@@ -113,6 +115,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
113115
// ------------------------------
114116
// and delete HAS_OWNER and HAS_SOURCE edges to this node if the node is deleted
115117
// ------------------------------
118+
WITH n
116119
CALL (n) {
117120
CALL (n) {
118121
MATCH (n)<-[rel:HAS_OWNER]-()
@@ -133,6 +136,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
133136
}
134137
}
135138
}
139+
WITH n, node_diff_map
136140
CALL (n, node_diff_map) {
137141
WITH CASE
138142
WHEN node_diff_map.attributes IS NULL OR node_diff_map.attributes = [] THEN [NULL]
@@ -155,7 +159,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
155159
ORDER BY has_attr.from DESC
156160
LIMIT 1
157161
}
158-
WITH attr_rel_status, a
162+
WITH n, attr_rel_status, a
159163
// ------------------------------
160164
// set HAS_ATTRIBUTE.to on target branch if necessary
161165
// ------------------------------
@@ -167,11 +171,12 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
167171
AND target_r_attr.from <= $at AND target_r_attr.to IS NULL
168172
SET target_r_attr.to = $at
169173
}
170-
WITH attr_rel_status, a
174+
WITH n, attr_rel_status, a
171175
// ------------------------------
172176
// conditionally create new HAS_ATTRIBUTE relationship on target_branch, if necessary
173177
// ------------------------------
174178
CALL (n, attr_rel_status, a) {
179+
WITH n, attr_rel_status, a
175180
WHERE a IS NOT NULL
176181
OPTIONAL MATCH (n)-[r_attr:HAS_ATTRIBUTE {branch: $target_branch}]->(a)
177182
WHERE r_attr.status = attr_rel_status
@@ -185,6 +190,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
185190
}
186191
RETURN 1 AS done
187192
}
193+
WITH n, node_diff_map
188194
CALL (n, node_diff_map) {
189195
UNWIND node_diff_map.relationships AS relationship_diff_map
190196
// ------------------------------
@@ -223,7 +229,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
223229
source_r_rel_1.hierarchy AS r1_hierarchy,
224230
source_r_rel_2.hierarchy AS r2_hierarchy
225231
}
226-
WITH r, r1_dir, r2_dir, r1_hierarchy, r2_hierarchy, rel_name, rel_peer_id, related_rel_status
232+
WITH n, r, r1_dir, r2_dir, r1_hierarchy, r2_hierarchy, rel_name, rel_peer_id, related_rel_status
227233
CALL (n, rel_name, rel_peer_id, related_rel_status) {
228234
OPTIONAL MATCH (n)
229235
-[target_r_rel_1:IS_RELATED {branch: $target_branch, status: "active"}]
@@ -236,7 +242,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
236242
SET target_r_rel_1.to = $at
237243
SET target_r_rel_2.to = $at
238244
}
239-
WITH r, r1_dir, r2_dir, r1_hierarchy, r2_hierarchy, rel_name, rel_peer_id, related_rel_status
245+
WITH n, r, r1_dir, r2_dir, r1_hierarchy, r2_hierarchy, rel_name, rel_peer_id, related_rel_status
240246
// ------------------------------
241247
// conditionally create new IS_RELATED relationships on target_branch, if necessary
242248
// ------------------------------
@@ -258,24 +264,28 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
258264
// create IS_RELATED relationships with directions maintained from source
259265
// ------------------------------
260266
CALL (n, r, r1_dir, r1_hierarchy, related_rel_status) {
267+
WITH n, r, r1_dir, r1_hierarchy, related_rel_status
261268
WHERE r1_dir = "r"
262269
CREATE (n)
263270
-[:IS_RELATED {branch: $target_branch, branch_level: $branch_level, from: $at, status: related_rel_status, hierarchy: r1_hierarchy}]
264271
->(r)
265272
}
266273
CALL (n, r, r1_dir, r1_hierarchy, related_rel_status) {
274+
WITH n, r, r1_dir, r1_hierarchy, related_rel_status
267275
WHERE r1_dir = "l"
268276
CREATE (n)
269277
<-[:IS_RELATED {branch: $target_branch, branch_level: $branch_level, from: $at, status: related_rel_status, hierarchy: r1_hierarchy}]
270278
-(r)
271279
}
272280
CALL (r, p, r2_dir, r2_hierarchy, related_rel_status) {
281+
WITH r, p, r2_dir, r2_hierarchy, related_rel_status
273282
WHERE r2_dir = "r"
274283
CREATE (r)
275284
-[:IS_RELATED {branch: $target_branch, branch_level: $branch_level, from: $at, status: related_rel_status, hierarchy: r2_hierarchy}]
276285
->(p)
277286
}
278287
CALL (r, p, r2_dir, r2_hierarchy, related_rel_status) {
288+
WITH r, p, r2_dir, r2_hierarchy, related_rel_status
279289
WHERE r2_dir = "l"
280290
CREATE (r)
281291
<-[:IS_RELATED {branch: $target_branch, branch_level: $branch_level, from: $at, status: related_rel_status, hierarchy: r2_hierarchy}]
@@ -389,7 +399,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
389399
}
390400
RETURN COALESCE (peer, bool, av) AS prop_node
391401
}
392-
WITH property_diff.property_type AS prop_type, prop_node, CASE
402+
WITH attr_rel,property_diff.property_type AS prop_type, prop_node, CASE
393403
WHEN property_diff.action = "ADDED" THEN "active"
394404
WHEN property_diff.action = "REMOVED" THEN "deleted"
395405
ELSE NULL
@@ -416,29 +426,34 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
416426
AND (r_prop.to > $at OR r_prop.to IS NULL)
417427
RETURN r_prop
418428
}
419-
WITH prop_rel_status, prop_type, prop_node, r_prop
429+
WITH attr_rel,prop_rel_status, prop_type, prop_node, r_prop
420430
WHERE r_prop IS NULL
421431
// ------------------------------
422432
// create new edge to prop_node on target_branch, if necessary
423433
// one subquery per possible edge type b/c edge type cannot be a variable
424434
// ------------------------------
425435
CALL (attr_rel, prop_rel_status, prop_type, prop_node) {
436+
WITH attr_rel, prop_rel_status, prop_type, prop_node
426437
WHERE prop_type = "HAS_VALUE"
427438
CREATE (attr_rel)-[:HAS_VALUE { branch: $target_branch, branch_level: $branch_level, from: $at, status: prop_rel_status }]->(prop_node)
428439
}
429440
CALL (attr_rel, prop_rel_status, prop_type, prop_node) {
441+
WITH attr_rel, prop_rel_status, prop_type, prop_node
430442
WHERE prop_type = "HAS_SOURCE"
431443
CREATE (attr_rel)-[:HAS_SOURCE { branch: $target_branch, branch_level: $branch_level, from: $at, status: prop_rel_status }]->(prop_node)
432444
}
433445
CALL (attr_rel, prop_rel_status, prop_type, prop_node) {
446+
WITH attr_rel, prop_rel_status, prop_type, prop_node
434447
WHERE prop_type = "HAS_OWNER"
435448
CREATE (attr_rel)-[:HAS_OWNER { branch: $target_branch, branch_level: $branch_level, from: $at, status: prop_rel_status }]->(prop_node)
436449
}
437450
CALL (attr_rel, prop_rel_status, prop_type, prop_node) {
451+
WITH attr_rel, prop_rel_status, prop_type, prop_node
438452
WHERE prop_type = "IS_VISIBLE"
439453
CREATE (attr_rel)-[:IS_VISIBLE { branch: $target_branch, branch_level: $branch_level, from: $at, status: prop_rel_status }]->(prop_node)
440454
}
441455
CALL (attr_rel, prop_rel_status, prop_type, prop_node) {
456+
WITH attr_rel, prop_rel_status, prop_type, prop_node
442457
WHERE prop_type = "IS_PROTECTED"
443458
CREATE (attr_rel)-[:IS_PROTECTED { branch: $target_branch, branch_level: $branch_level, from: $at, status: prop_rel_status }]->(prop_node)
444459
}

backend/infrahub/core/diff/query/save.py

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
9898
// -------------------------
9999
// delete parent-child relationships for included nodes, they will be added in EnrichedNodesLinkQuery
100100
// -------------------------
101+
WITH diff_node
101102
MATCH (diff_node)-[:DIFF_HAS_RELATIONSHIP]->(:DiffRelationship)-[parent_rel:DIFF_HAS_NODE]->(:DiffNode)
102103
DELETE parent_rel
103104
}
@@ -106,13 +107,15 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
106107
// -------------------------
107108
// create a node-level conflict, if necessary
108109
// -------------------------
110+
WITH diff_node, current_node_conflict, has_node_conflict
109111
WHERE current_node_conflict IS NULL AND has_node_conflict = TRUE
110112
CREATE (diff_node)-[:DIFF_HAS_CONFLICT]->(:DiffConflict)
111113
}
112114
CALL (current_node_conflict, has_node_conflict) {
113115
// -------------------------
114116
// delete a node-level conflict, if necessary
115117
// -------------------------
118+
WITH current_node_conflict, has_node_conflict
116119
WHERE current_node_conflict IS NOT NULL AND has_node_conflict = FALSE
117120
DETACH DELETE current_node_conflict
118121
}
@@ -121,6 +124,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
121124
// -------------------------
122125
// set the properties of the node-level conflict, if necessary
123126
// -------------------------
127+
WITH diff_node, has_node_conflict, node_conflict_params
124128
WHERE has_node_conflict = TRUE
125129
OPTIONAL MATCH (diff_node)-[:DIFF_HAS_CONFLICT]->(node_conflict:DiffConflict)
126130
SET node_conflict = node_conflict_params

backend/infrahub/core/diff/query/summary_counts_enricher.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,17 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None: # noqa
230230
CALL (root, total_conflicts_delta) {
231231
SET root.num_conflicts = coalesce(root.num_conflicts, 0) + total_conflicts_delta
232232
SET root.contains_conflict = root.num_conflicts > 0
233+
WITH root
233234
OPTIONAL MATCH (root)-[:DIFF_HAS_NODE]->(dn:DiffNode {action: "added"})
234-
WITH count(dn.action) AS num_added
235+
WITH root, count(dn.action) AS num_added
235236
SET root.num_added = num_added
237+
WITH root
236238
OPTIONAL MATCH (root)-[:DIFF_HAS_NODE]->(dn:DiffNode {action: "updated"})
237-
WITH count(dn.action) AS num_updated
239+
WITH root, count(dn.action) AS num_updated
238240
SET root.num_updated = num_updated
241+
WITH root
239242
OPTIONAL MATCH (root)-[:DIFF_HAS_NODE]->(dn:DiffNode {action: "removed"})
240-
WITH count(dn.action) AS num_removed
243+
WITH root, count(dn.action) AS num_removed
241244
SET root.num_removed = num_removed
242245
}
243246
"""

backend/infrahub/core/query/relationship.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
10271027
WHERE %(active_rel_filter)s AND active_edge.status ="active"
10281028
CREATE (rl)-[deleted_edge:IS_RELATED $rel_prop]->(n)
10291029
SET deleted_edge.hierarchy = active_edge.hierarchy
1030-
WITH active_edge, n
1030+
WITH rl, active_edge, n
10311031
WHERE active_edge.branch = $branch AND active_edge.to IS NULL
10321032
SET active_edge.to = $at
10331033
RETURN
@@ -1037,12 +1037,12 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
10371037
"outbound" as rel_direction
10381038
10391039
UNION
1040-
1040+
WITH rl
10411041
MATCH (rl)<-[active_edge:IS_RELATED]-(n)
10421042
WHERE %(active_rel_filter)s AND active_edge.status ="active"
10431043
CREATE (rl)<-[deleted_edge:IS_RELATED $rel_prop]-(n)
10441044
SET deleted_edge.hierarchy = active_edge.hierarchy
1045-
WITH active_edge, n
1045+
WITH rl, active_edge, n
10461046
WHERE active_edge.branch = $branch AND active_edge.to IS NULL
10471047
SET active_edge.to = $at
10481048
RETURN

backend/tests/unit/core/migrations/graph/test_020.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ async def test_duplicate_edges_migration(self, db: InfrahubDatabase, car_person_
4646
LIMIT 1
4747
CREATE (a)-[new_ve:IS_VISIBLE]->(v)
4848
SET new_ve = properties(ve)
49+
WITH a
4950
MATCH (a)-[pe:IS_PROTECTED]->(p)
50-
WITH pe, p
51+
WITH a, pe, p
5152
LIMIT 1
5253
CREATE (a)-[new_pe:IS_PROTECTED]->(p)
5354
SET new_pe = properties(pe)

0 commit comments

Comments
 (0)