Skip to content

Commit

Permalink
Fixes #303 - constraint issue (create before delete) with mergeNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBerton committed Apr 19, 2017
1 parent 9d4abf1 commit 0603e5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/apoc/refactor/GraphRefactoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,16 @@ private Future<Void> categorizeNodes(List<Node> batch, String sourceKey, String
}

private Node mergeNodes(Node source, Node target, boolean delete) {
copyRelationships(source, copyProperties(source, copyLabels(source, target)), delete);
Map<String, Object> properties = source.getAllProperties();
copyRelationships(source, copyLabels(source, target), delete);
if (delete) source.delete();
copyProperties(properties, target);
return target;
}

private Node copyProperties(Map<String, Object> properties, Node target) {
for (Map.Entry<String, Object> prop : properties.entrySet())
target.setProperty(prop.getKey(), prop.getValue());
return target;
}

Expand All @@ -326,5 +334,4 @@ private <T extends PropertyContainer> T copyProperties(PropertyContainer source,
private Relationship copyRelationship(Relationship rel, Node source, Node newSource) {
return copyProperties(rel,newSource.createRelationshipTo(rel.getOtherNode(source), rel.getType()));
}

}
17 changes: 17 additions & 0 deletions src/test/java/apoc/refactor/GraphRefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,21 @@ public void testChangeType() throws Exception {
public void testRedirectRelationship() throws Exception {

}

@Test
public void testMergeNodesWithConstraints() throws Exception {
db.execute("CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE").close();
long id = db.execute("CREATE (p1:Person {name:'Foo'}), (p2:Person {surname:'Bar'}) RETURN id(p1) as id ").<Long>columnAs("id").next();
ExecutionPlanDescription plan = db.execute("EXPLAIN MATCH (o:Person {ID:{oldID}}), (n:Person {ID:{newID}}) CALL apoc.refactor.mergeNodes([o,n]) yield node return node").getExecutionPlanDescription();
System.out.println(plan);
System.out.flush();
testCall(db, "MATCH (o:Person {name:'Foo'}), (n:Person {surname:'Bar'}) CALL apoc.refactor.mergeNodes([o,n]) yield node return node",
(r) -> {
Node node = (Node) r.get("node");
assertEquals(id, node.getId());
assertEquals(true, node.hasLabel(Label.label("Person")));
assertEquals("Foo", node.getProperty("name"));
assertEquals("Bar", node.getProperty("surname"));
});
}
}

0 comments on commit 0603e5a

Please sign in to comment.