From 0c39b50fb2f5a3b34bb745e9fd939702c5000193 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 5 Apr 2018 21:06:29 +0200 Subject: [PATCH] SONAR-10511 Project key renaming should rename deleted components too --- .../db/component/ComponentKeyUpdaterDao.java | 12 ++++----- .../component/ComponentKeyUpdaterMapper.xml | 2 -- .../component/ComponentKeyUpdaterDaoTest.java | 25 +++++++++++-------- .../component/ComponentServiceTest.java | 8 ++---- .../ComponentServiceUpdateKeyTest.java | 11 +++----- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java index 505618562c1..e394e7c7ccc 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java @@ -83,7 +83,7 @@ public class ComponentKeyUpdaterDao implements Dao { * @return a map with currentKey/newKey is a bulk update was executed */ public Map simulateBulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) { - return collectAllModules(projectUuid, stringToReplace, mapper(dbSession)) + return collectAllModules(projectUuid, stringToReplace, mapper(dbSession), false) .stream() .collect(Collectors.toMap( ResourceDto::getKey, @@ -109,7 +109,7 @@ public class ComponentKeyUpdaterDao implements Dao { public void bulkUpdateKey(DbSession session, String projectUuid, String stringToReplace, String replacementString) { ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class); // must SELECT first everything - Set modules = collectAllModules(projectUuid, stringToReplace, mapper); + Set modules = collectAllModules(projectUuid, stringToReplace, mapper, true); // add branches Map branchBaseKeys = new HashMap<>(); @@ -117,7 +117,7 @@ public class ComponentKeyUpdaterDao implements Dao { .stream() .filter(branch -> !projectUuid.equals(branch.getUuid())) .forEach(branch -> { - Set branchModules = collectAllModules(branch.getUuid(), stringToReplace, mapper); + Set branchModules = collectAllModules(branch.getUuid(), stringToReplace, mapper, true); modules.addAll(branchModules); branchModules.forEach(module -> branchBaseKeys.put(module.getKey(), branchBaseKey(module.getKey()))); }); @@ -161,14 +161,14 @@ public class ComponentKeyUpdaterDao implements Dao { } } - private static Set collectAllModules(String projectUuid, String stringToReplace, ComponentKeyUpdaterMapper mapper) { + private static Set collectAllModules(String projectUuid, String stringToReplace, ComponentKeyUpdaterMapper mapper, boolean includeDisabled) { ResourceDto project = mapper.selectProject(projectUuid); Set modules = new HashSet<>(); - if (project.getKey().contains(stringToReplace)) { + if (project.getKey().contains(stringToReplace) && (project.isEnabled() || includeDisabled)) { modules.add(project); } for (ResourceDto submodule : mapper.selectDescendantProjects(projectUuid)) { - modules.addAll(collectAllModules(submodule.getUuid(), stringToReplace, mapper)); + modules.addAll(collectAllModules(submodule.getUuid(), stringToReplace, mapper, includeDisabled)); } return modules; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml index d7189fdb1ec..51f2114ab66 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml @@ -29,7 +29,6 @@ where root_uuid = #{rootUuid,jdbcType=VARCHAR} and scope != 'PRJ' - and enabled = ${_true} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java index 665179c62b6..68293a24707 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java @@ -62,7 +62,7 @@ public class ComponentKeyUpdaterDaoTest { } @Test - public void updateKey_does_not_update_inactive_components() { + public void updateKey_updates_disabled_components() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organizationDto, "A").setDbKey("my_project")); ComponentDto directory = db.components().insertComponent(newDirectory(project, "/directory").setDbKey("my_project:directory")); @@ -74,8 +74,10 @@ public class ComponentKeyUpdaterDaoTest { dbSession.commit(); List result = dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, "your_project"); - assertThat(result).hasSize(5).extracting(ComponentDto::getDbKey) - .containsOnlyOnce("your_project", "your_project:directory", "your_project:directory/file", "my_project:inactive_directory", "my_project:inactive_directory/file"); + assertThat(result) + .hasSize(5) + .extracting(ComponentDto::getDbKey) + .containsOnlyOnce("your_project", "your_project:directory", "your_project:directory/file", "your_project:inactive_directory", "your_project:inactive_directory/file"); } @Test @@ -148,7 +150,7 @@ public class ComponentKeyUpdaterDaoTest { } @Test - public void bulk_update_key_does_not_update_inactive_components() { + public void bulk_update_key_updates_disabled_components() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "A").setDbKey("my_project")); db.components().insertComponent(newModuleDto(project).setDbKey("my_project:module")); db.components().insertComponent(newModuleDto(project).setDbKey("my_project:inactive_module").setEnabled(false)); @@ -156,8 +158,10 @@ public class ComponentKeyUpdaterDaoTest { underTest.bulkUpdateKey(dbSession, "A", "my_", "your_"); List result = dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, "your_project"); - assertThat(result).hasSize(3).extracting(ComponentDto::getDbKey) - .containsOnlyOnce("your_project", "your_project:module", "my_project:inactive_module"); + assertThat(result) + .hasSize(3) + .extracting(ComponentDto::getDbKey) + .containsOnlyOnce("your_project", "your_project:module", "your_project:inactive_module"); } @Test @@ -260,16 +264,17 @@ public class ComponentKeyUpdaterDaoTest { } @Test - public void simulate_bulk_update_key_do_not_return_disable_components() { + public void simulate_bulk_update_key_does_not_return_disable_components() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "A").setDbKey("project")); db.components().insertComponent(newModuleDto(project).setDbKey("project:enabled-module")); db.components().insertComponent(newModuleDto(project).setDbKey("project:disabled-module").setEnabled(false)); + db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "D").setDbKey("other-project")); Map result = underTest.simulateBulkUpdateKey(dbSession, "A", "project", "new-project"); - assertThat(result) - .hasSize(2) - .containsOnly(entry("project", "new-project"), entry("project:enabled-module", "new-project:enabled-module")); + assertThat(result).containsOnly( + entry("project", "new-project"), + entry("project:enabled-module", "new-project:enabled-module")); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java index 7221f1e079c..78bd7d0f3b1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java @@ -65,8 +65,8 @@ public class ComponentServiceTest { assertComponentKeyUpdated(project.getDbKey(), "your_project"); assertComponentKeyUpdated(module.getDbKey(), "your_project:root:module"); assertComponentKeyUpdated(file.getDbKey(), "your_project:root:module:src/File.xoo"); - assertComponentKeyNotUpdated(inactiveModule.getDbKey()); - assertComponentKeyNotUpdated(inactiveFile.getDbKey()); + assertComponentKeyUpdated(inactiveModule.getDbKey(), "your_project:root:inactive_module"); + assertComponentKeyUpdated(inactiveFile.getDbKey(), "your_project:root:module:src/InactiveFile.xoo"); } private void assertComponentKeyUpdated(String oldKey, String newKey) { @@ -74,8 +74,4 @@ public class ComponentServiceTest { assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent(); } - private void assertComponentKeyNotUpdated(String key) { - assertThat(dbClient.componentDao().selectByKey(dbSession, key)).isPresent(); - } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java index fa3327ea4c2..b3b351c3019 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java @@ -76,8 +76,9 @@ public class ComponentServiceUpdateKeyTest { // Check file key has been updated assertThat(db.getDbClient().componentDao().selectByKey(dbSession, file.getDbKey())).isAbsent(); assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root:src/File.xoo")).isNotNull(); + assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root:src/InactiveFile.xoo")).isNotNull(); - assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getDbKey())).isPresent(); + assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getDbKey())).isAbsent(); org.assertj.core.api.Assertions.assertThat(projectIndexers.hasBeenCalled(project.uuid(), ProjectIndexer.Cause.PROJECT_KEY_UPDATE)).isTrue(); } @@ -185,8 +186,8 @@ public class ComponentServiceUpdateKeyTest { assertComponentKeyUpdated(project.getDbKey(), "your_project"); assertComponentKeyUpdated(module.getDbKey(), "your_project:root:module"); assertComponentKeyUpdated(file.getDbKey(), "your_project:root:module:src/File.xoo"); - assertComponentKeyNotUpdated(inactiveModule.getDbKey()); - assertComponentKeyNotUpdated(inactiveFile.getDbKey()); + assertComponentKeyUpdated(inactiveModule.getDbKey(), "your_project:root:inactive_module"); + assertComponentKeyUpdated(inactiveFile.getDbKey(), "your_project:root:module:src/InactiveFile.xoo"); } private void assertComponentKeyUpdated(String oldKey, String newKey) { @@ -194,10 +195,6 @@ public class ComponentServiceUpdateKeyTest { assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent(); } - private void assertComponentKeyNotUpdated(String key) { - assertThat(dbClient.componentDao().selectByKey(dbSession, key)).isPresent(); - } - private ComponentDto insertSampleRootProject() { return insertProject("sample:root"); } -- 2.39.5