From 836d0b757953c20923741326369204f924e813bf Mon Sep 17 00:00:00 2001 From: Giuseppe Villani Date: Wed, 12 Oct 2022 09:16:54 +0200 Subject: [PATCH] [pDw6xmRG] Fixes #26: apoc.refactor.rename.label delets label if oldLabel is equal to newLabel --- .../main/java/apoc/refactor/rename/Rename.java | 6 +++--- .../java/apoc/refactor/rename/RenameTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/apoc/refactor/rename/Rename.java b/core/src/main/java/apoc/refactor/rename/Rename.java index b267e6a66..d95285fb0 100644 --- a/core/src/main/java/apoc/refactor/rename/Rename.java +++ b/core/src/main/java/apoc/refactor/rename/Rename.java @@ -65,7 +65,7 @@ public Stream label(@Name("oldLabel") String oldLab oldLabel = Util.sanitize(oldLabel); newLabel = Util.sanitize(newLabel); String cypherIterate = nodes != null && !nodes.isEmpty() ? "UNWIND $nodes AS n WITH n WHERE n:`"+oldLabel+"` RETURN n" : "MATCH (n:`"+oldLabel+"`) RETURN n"; - String cypherAction = "SET n:`"+newLabel+"` REMOVE n:`"+oldLabel+"`"; + String cypherAction = "REMOVE n:`"+oldLabel+"` SET n:`" + newLabel + "`"; Map parameters = MapUtil.map("batchSize", 100000, "parallel", true, "iterateList", true, "params", MapUtil.map("nodes", nodes)); return getResultOfBatchAndTotalWithInfo( newPeriodic().iterate(cypherIterate, cypherAction, parameters), db, oldLabel, null, null); } @@ -124,7 +124,7 @@ public Stream nodeProperty(@Name("oldName") String oldName = Util.sanitize(oldName); newName = Util.sanitize(newName); String cypherIterate = nodes != null && ! nodes.isEmpty() ? "UNWIND $nodes AS n WITH n WHERE n.`"+oldName+"` IS NOT NULL return n" : "match (n) where n.`"+oldName+"` IS NOT NULL return n"; - String cypherAction = "set n.`"+newName+"`= n.`"+oldName+"` remove n.`"+oldName+"`"; + String cypherAction = "WITH n, n. `" + oldName + "` AS propVal REMOVE n.`" + oldName + "` SET n.`"+newName+"` = propVal"; final Map params = MapUtil.map("nodes", nodes); Map parameters = getPeriodicConfig(config, params); return getResultOfBatchAndTotalWithInfo(newPeriodic().iterate(cypherIterate, cypherAction, parameters), db, null, null, oldName); @@ -143,7 +143,7 @@ public Stream typeProperty(@Name("oldName") String newName = Util.sanitize(newName); oldName = Util.sanitize(oldName); String cypherIterate = rels != null && ! rels.isEmpty() ? "UNWIND $rels AS r WITH r WHERE r.`"+oldName+"` IS NOT NULL return r" : "match ()-[r]->() where r.`"+oldName+"` IS NOT NULL return r"; - String cypherAction = "set r.`"+newName+"` = r.`"+oldName+"` remove r.`"+oldName+"`"; + String cypherAction = "WITH r, r. `" + oldName + "` AS propVal REMOVE r.`"+oldName + "` SET r.`"+newName+"`= propVal"; final Map params = MapUtil.map("rels", rels); Map parameters = getPeriodicConfig(config, params); return getResultOfBatchAndTotalWithInfo(newPeriodic().iterate(cypherIterate, cypherAction, parameters), db, null, null, oldName); diff --git a/core/src/test/java/apoc/refactor/rename/RenameTest.java b/core/src/test/java/apoc/refactor/rename/RenameTest.java index 4e70a8633..577f36300 100644 --- a/core/src/test/java/apoc/refactor/rename/RenameTest.java +++ b/core/src/test/java/apoc/refactor/rename/RenameTest.java @@ -17,6 +17,7 @@ import static apoc.util.MapUtil.map; import static apoc.util.TestUtil.testCall; +import static apoc.util.TestUtil.testCallCount; import static apoc.util.TestUtil.testResult; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -363,6 +364,22 @@ public void testRenamePropertyForSomeRelationship() throws Exception { assertEquals(2L, resultRelationshipsMatches(null, "surname")); assertEquals(8L, resultRelationshipsMatches(null, "name")); } + + @Test + public void testRenameWithSameValues() { + db.executeTransactionally("CREATE (n:ToRename {a: 1})-[:REL_TO_RENAME {a: 1}]->(:Other)"); + testCall(db, "CALL apoc.refactor.rename.label('ToRename', 'ToRename')", r -> assertEquals(1L, r.get("total"))); + testCallCount(db, "MATCH (n:ToRename {a: 1}) RETURN n", 1); + + testCall(db, "CALL apoc.refactor.rename.type('REL_TO_RENAME', 'REL_TO_RENAME')", r -> assertEquals(1L, r.get("total"))); + testCallCount(db, "MATCH (:ToRename)-[r:REL_TO_RENAME {a: 1}]->(:Other) RETURN r", 1); + + testCall(db, "CALL apoc.refactor.rename.nodeProperty('a', 'a')", r -> assertEquals(1L, r.get("total"))); + testCallCount(db, "MATCH (n:ToRename {a: 1}) RETURN n", 1); + + testCall(db, "CALL apoc.refactor.rename.typeProperty('a', 'a')", r -> assertEquals(1L, r.get("total"))); + testCallCount(db, "MATCH (:ToRename)-[r:REL_TO_RENAME {a: 1}]->(:Other) RETURN r", 1); + } private long resultRelationshipsMatches(String type, String prop){ String query = type != null ? "MATCH ()-[r:`"+type+"`]->() RETURN count(r) as countResult" : "match ()-[r]->() where r.`"+prop+"` IS NOT NULL return count(r) as countResult";