]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13784 Deleting project should make sure all components are removed
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 21 Aug 2020 16:32:35 +0000 (11:32 -0500)
committersonartech <sonartech@sonarsource.com>
Mon, 24 Aug 2020 20:06:06 +0000 (20:06 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java

index 3d28191d9b12ed31bc0196450694fbaeceef0b0c..33abd185e2ef16246617998074b7ff465b4288ca 100644 (file)
@@ -266,6 +266,13 @@ class PurgeCommands {
     profiler.stop();
   }
 
+  void deleteComponentsByMainBranchProjectUuid(String uuid) {
+    profiler.start("deleteComponentsByMainBranchProjectUuid (projects)");
+    purgeMapper.deleteComponentsByMainBranchProjectUuid(uuid);
+    session.commit();
+    profiler.stop();
+  }
+
   void deleteProject(String projectUuid) {
     profiler.start("deleteProject (projects)");
     purgeMapper.deleteProjectsByProjectUuid(projectUuid);
index 16d0df3ffc67845eecbf2086f1a00bf28d4f6439..a86217c5d83019f3c5c879d1e01e6427fd03c1a2 100644 (file)
@@ -207,6 +207,7 @@ public class PurgeDao implements Dao {
     commands.deleteNewCodePeriods(rootUuid);
     commands.deleteBranch(rootUuid);
     commands.deleteComponents(rootUuid);
+    commands.deleteComponentsByMainBranchProjectUuid(rootUuid);
     commands.deleteProject(rootUuid);
   }
 
index 5e068e4cac9328a796c5060aed389549b76c4422..56f09dda6b6610718dbc52e154e5a5e4fb442a45 100644 (file)
@@ -68,6 +68,8 @@ public interface PurgeMapper {
 
   void deleteComponentsByProjectUuid(@Param("rootUuid") String rootUuid);
 
+  void deleteComponentsByMainBranchProjectUuid(@Param("uuid") String uuid);
+
   void deleteProjectsByProjectUuid(@Param("projectUuid") String projectUuid);
 
   void deleteComponentsByUuids(@Param("componentUuids") List<String> componentUuids);
index 7172ba7bba9e579a513b35cc025cf3335707b3b0..c9cb7aad403c9d306c227416c8841e368be32dc8 100644 (file)
       project_uuid = #{rootUuid,jdbcType=VARCHAR}
   </delete>
 
+    <delete id="deleteComponentsByMainBranchProjectUuid" parameterType="map">
+    delete from components
+    where
+      main_branch_project_uuid = #{uuid,jdbcType=VARCHAR}
+  </delete>
+
   <delete id="deleteProjectsByProjectUuid" parameterType="map">
     delete from projects
     where
index 717f259e0b38d49f7bfab285b05aac59b8f8e725..596f943d0a2068c64bff9db344088c7eb53cfcb9 100644 (file)
@@ -148,6 +148,28 @@ public class PurgeCommandsTest {
     assertThat(countComponentOfRoot(otherProject)).isEqualTo(8);
   }
 
+  @Test
+  @UseDataProvider("projects")
+  public void deleteComponentsByMainBranchProjectUuid_deletes_all_branches_of_a_project(OrganizationDto organizationDto, ComponentDto project) {
+    dbTester.organizations().insert(organizationDto);
+    dbTester.components().insertComponent(project);
+    ComponentDto branch = dbTester.components().insertProjectBranch(project);
+    Stream.of(project, branch).forEach(prj -> {
+      ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(prj));
+      ComponentDto directory1 = dbTester.components().insertComponent(ComponentTesting.newDirectory(module, "a"));
+      ComponentDto directory2 = dbTester.components().insertComponent(ComponentTesting.newDirectory(prj, "b"));
+      dbTester.components().insertComponent(newFileDto(prj));
+      dbTester.components().insertComponent(newFileDto(module));
+      dbTester.components().insertComponent(newFileDto(directory1));
+      dbTester.components().insertComponent(newFileDto(directory2));
+    });
+
+    underTest.deleteComponentsByMainBranchProjectUuid(project.uuid());
+
+    assertThat(countComponentOfRoot(project)).isEqualTo(8);
+    assertThat(countComponentOfRoot(branch)).isZero();
+  }
+
   @Test
   @UseDataProvider("views")
   public void deleteComponents_delete_tree_of_components_of_a_view(OrganizationDto organizationDto, ComponentDto view) {