I have a Cypher query that is supposed to create IMPACT
relationships between an Issue
node and Version
nodes in a version chain.
MATCH (problem:Issue {id: $issueId})
MATCH path = (problem)-[:FIRST_AFFECTED]->(firstVersion:Version)
(()<-[:PREDECESSOR_VERSION]-(nextVersion:Version) WHERE NOT EXISTS { (nextVersion)<-[:FIXED]-(problem) })*
(affectedVersion:Version)
MERGE (problem)-[:IMPACT]->(affectedVersion)
UNION
MATCH (problem:Issue {id: $issueId})
MATCH path = (problem)-[:FIXED]->(fixedVersion:Version)
(()-[:PREDECESSOR_VERSION]->(upVersion:Version) WHERE NOT EXISTS { (upVersion)<-[:FIRST_AFFECTED | IMPACT | FIXED]-(problem) })*
(affectedVersion:Version)
WHERE affectedVersion <> fixedVersion
MERGE (problem)-[:IMPACT]->(affectedVersion)
WITH problem, affectedVersion
MATCH path = (affectedVersion)<-[:PREDECESSOR_VERSION*]-(childVersion:Version)
WHERE NOT EXISTS { (childVersion)<-[:FIXED | FIRST_AFFECTED | IMPACT]-(problem) }
MERGE (problem)-[:IMPACT]->(childVersion)
The query consists of two parts joined by a UNION
. The first part handles versions affected by the issue, and the second part handles versions fixed by the issue. The issue arises in the second part of the query, specifically in the following section:
MATCH path = (affectedVersion)<-[:PREDECESSOR_VERSION*]-(childVersion:Version)
WHERE NOT EXISTS { (childVersion)<-[:FIXED | FIRST_AFFECTED | IMPACT]-(problem) }
MERGE (problem)-[:IMPACT]->(childVersion)
The problem is that this part of the query does not stop traversing the version chain when it encounters a Version
node that has a FIXED
, FIRST_AFFECTED
, or IMPACT
relationship with problem
. Instead, it continues to create IMPACT
relationships for all parent versions, except the one with the FIXED
relationship.
Example Graph:
Consider the following version chain:
If an Issue
has a FIXED
relationship with v2
, the query should only create an IMPACT
relationship with v1
. However, it creates IMPACT
relationships with all versions from v1
to v10
, except v2
.
How can I modify the query to ensure that the traversal stops when it encounters a Version
node with a FIXED
, FIRST_AFFECTED
, or IMPACT
relationship, and only creates IMPACT
relationships up to that point?