]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10511 Project key renaming should rename deleted components too
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 5 Apr 2018 19:06:29 +0000 (21:06 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 6 Apr 2018 11:32:52 +0000 (13:32 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java

index 505618562c10a4141d40930a76eae5295efaba19..e394e7c7cccc69ef1aa24a71128c278e5e2a9c43 100644 (file)
@@ -83,7 +83,7 @@ public class ComponentKeyUpdaterDao implements Dao {
    * @return a map with currentKey/newKey is a bulk update was executed
    */
   public Map<String, String> 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<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper);
+    Set<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper, true);
 
     // add branches
     Map<String, String> branchBaseKeys = new HashMap<>();
@@ -117,7 +117,7 @@ public class ComponentKeyUpdaterDao implements Dao {
       .stream()
       .filter(branch -> !projectUuid.equals(branch.getUuid()))
       .forEach(branch -> {
-        Set<ResourceDto> branchModules = collectAllModules(branch.getUuid(), stringToReplace, mapper);
+        Set<ResourceDto> 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<ResourceDto> collectAllModules(String projectUuid, String stringToReplace, ComponentKeyUpdaterMapper mapper) {
+  private static Set<ResourceDto> collectAllModules(String projectUuid, String stringToReplace, ComponentKeyUpdaterMapper mapper, boolean includeDisabled) {
     ResourceDto project = mapper.selectProject(projectUuid);
     Set<ResourceDto> 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;
   }
index d7189fdb1ec65e96fd36eb424e91173b0d5a93d9..51f2114ab66c442a51e4cc68798dab4db28d1717 100644 (file)
@@ -29,7 +29,6 @@
     where
     root_uuid = #{rootUuid,jdbcType=VARCHAR}
     and scope != 'PRJ'
-    and enabled = ${_true}
   </select>
 
   <select id="selectDescendantProjects" parameterType="String" resultMap="resourceResultMap">
@@ -38,7 +37,6 @@
     scope='PRJ'
     and root_uuid = #{rootUuid,jdbcType=VARCHAR}
     and uuid != #{rootUuid,jdbcType=VARCHAR}
-    and enabled = ${_true}
   </select>
 
   <update id="update" parameterType="Resource">
index 665179c62b64221bc644f431eac38b6b547ab7bd..68293a2470716b8229fd1f1e9459f71568f1f46d 100644 (file)
@@ -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<ComponentDto> 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<ComponentDto> 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<String, String> 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
index 7221f1e079c1da7ac62c5e1411491896a8d4aad0..78bd7d0f3b1e637dc14fb8fb39a9583616c7fcbf 100644 (file)
@@ -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();
-  }
-
 }
index fa3327ea4c2c652e22173fdb73775695383753e1..b3b351c30199894c828fe407a83b1f9db6160aae 100644 (file)
@@ -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");
   }