Skip to content

Commit

Permalink
[bZQoGhFu] apoc.refactor.mergeNodes is not working with uniqueness in…
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed May 30, 2023
1 parent 4a92bc1 commit 4075722
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/src/main/java/apoc/refactor/GraphRefactoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,15 @@ private Future<Void> categorizeNodes(List<Node> batch, String sourceKey, String
private void mergeNodes(Node source, Node target, RefactorConfig conf, List<Long> excludeRelIds) {
try {
Map<String, Object> properties = source.getAllProperties();
final Iterable<Label> labels = source.getLabels();

copyRelationships(source, copyLabels(source, target), true, conf.isCreatingNewSelfRel());
copyRelationships(source, target, true, conf.isCreatingNewSelfRel());
if (conf.getMergeRelsAllowed()) {
mergeRelsWithSameTypeAndDirectionInMergeNodes(target, conf, Direction.OUTGOING, excludeRelIds);
mergeRelsWithSameTypeAndDirectionInMergeNodes(target, conf, Direction.INCOMING, excludeRelIds);
}
source.delete();
labels.forEach(target::addLabel);
PropertiesManager.mergeProperties(properties, target, conf);
} catch (NotFoundException e) {
log.warn("skipping a node for merging: " + e.getCause().getMessage());
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/apoc/refactor/GraphRefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ public void testEagernessMergeNodesFails() throws Exception {
assertEquals(2L, node.getProperty("ID"));
});
}

@Test
public void testMergeNodesShouldNotFailWithSamePropKeysConstraints() {
db.executeTransactionally("CREATE CONSTRAINT FOR (a:A) REQUIRE a.prop1 IS UNIQUE");
db.executeTransactionally("CREATE CONSTRAINT FOR (a:B) REQUIRE a.prop1 IS UNIQUE");
String id = db.executeTransactionally("CREATE (a:A {prop1: 1}), (:B {prop1: 1}) RETURN elementId(a) as id", emptyMap(),
r -> Iterators.single(r.columnAs("id")));
testCall(db, "MATCH (a:A {prop1:1}), (b:B {prop1:1})\n" +
"CALL apoc.refactor.mergeNodes([a, b]) YIELD node RETURN node;",
(r) -> {
Node node = (Node) r.get("node");
assertEquals(id, node.getElementId());
assertTrue(node.hasLabel(label("A")));
assertTrue(node.hasLabel(label("B")));
assertEquals(1L, node.getProperty("prop1"));
});
}

@Test
public void testMergeNodesEagerAggregation() throws Exception {
Expand Down

0 comments on commit 4075722

Please sign in to comment.