]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 Delete branch content when deleting project (#2368)
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Wed, 16 Aug 2017 14:24:27 +0000 (16:24 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 08:59:56 +0000 (10:59 +0200)
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/PurgeDaoTest.java

index ef9cdaa0013512bf79e0e8147c731fc9eba6ed49..9cf7789d96b98eaff9204e197c932dd281752271 100644 (file)
@@ -255,4 +255,11 @@ class PurgeCommands {
     session.commit();
     profiler.stop();
   }
+
+  void deleteBranches(String rootUuid) {
+    profiler.start("deleteBranches (project_branches)");
+    purgeMapper.deleteBranchesByProjectUuid(rootUuid);
+    session.commit();
+    profiler.stop();
+  }
 }
index 369cd7db93b0ca74fe6b15e3e553f6ddfcac8e09..67fb3b701c683f67ce8660107ddfbabed037a6c3 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.api.utils.log.Loggers;
 import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchMapper;
 import org.sonar.db.component.ComponentDao;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTreeQuery;
@@ -150,8 +151,15 @@ public class PurgeDao implements Dao {
 
   public void deleteRootComponent(DbSession session, String uuid) {
     PurgeProfiler profiler = new PurgeProfiler();
+    PurgeMapper purgeMapper = mapper(session);
     PurgeCommands purgeCommands = new PurgeCommands(session, profiler);
-    deleteRootComponent(uuid, mapper(session), purgeCommands);
+
+    session.getMapper(BranchMapper.class).selectByProjectUuid(uuid)
+      .stream()
+      .filter(branch -> !uuid.equals(branch.getUuid()))
+      .forEach(branch -> deleteRootComponent(branch.getUuid(), purgeMapper, purgeCommands));
+
+    deleteRootComponent(uuid, purgeMapper, purgeCommands);
   }
 
   private static void deleteRootComponent(String rootUuid, PurgeMapper mapper, PurgeCommands commands) {
@@ -171,6 +179,7 @@ public class PurgeDao implements Dao {
     commands.deleteCeActivity(rootUuid);
     commands.deleteCeQueue(rootUuid);
     commands.deleteWebhookDeliveries(rootUuid);
+    commands.deleteBranches(rootUuid);
   }
 
   /**
index 0aefe6335022f8baff3aa12e245fefb4e88fa3d2..23d68ee1bc1e899bf54eab5b47f08ab08bad79f6 100644 (file)
@@ -91,4 +91,6 @@ public interface PurgeMapper {
   void deleteCeQueueByProjectUuid(@Param("projectUuid") String projectUuid);
 
   void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid);
+
+  void deleteBranchesByProjectUuid(@Param("projectUuid") String projectUuid);
 }
index 0a7661ccb3210b035e825a6f135bd7533292cedc..d22fc0b973e24dad9d8715318aad001f21cbee0b 100644 (file)
     delete from webhook_deliveries where component_uuid=#{projectUuid,jdbcType=VARCHAR}
   </delete>
 
+  <delete id="deleteBranchesByProjectUuid">
+    delete from project_branches where project_uuid=#{projectUuid,jdbcType=VARCHAR}
+  </delete>
+
 </mapper>
 
index 1b817dae8239779fe0f848a6a6b277e04b9ba77b..903b59d161f8551cc385ca1cb656416e272f04af 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.measure.MeasureDto;
 import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.property.PropertyDto;
+import org.sonar.db.rule.RuleDefinitionDto;
 
 import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
@@ -59,6 +60,7 @@ import static org.mockito.Mockito.when;
 import static org.sonar.db.ce.CeTaskTypes.REPORT;
 import static org.sonar.db.component.ComponentTesting.newDirectory;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newModuleDto;
 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
 import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto;
 import static org.sonar.db.webhook.WebhookDbTesting.selectAllDeliveryUuids;
@@ -192,6 +194,39 @@ public class PurgeDaoTest {
     assertThat(dbTester.countSql("select count(*) from ce_queue where component_uuid='" + projectToBeDeleted.uuid() + "'")).isEqualTo(0);
   }
 
+  private ComponentDto insertProjectWithBranchAndRelatedData() {
+    RuleDefinitionDto rule = dbTester.rules().insert();
+    ComponentDto project = dbTester.components().insertMainBranch();
+    ComponentDto branch = dbTester.components().insertProjectBranch(project);
+    ComponentDto module = dbTester.components().insertComponent(newModuleDto(branch));
+    ComponentDto subModule = dbTester.components().insertComponent(newModuleDto(module));
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(subModule));
+    dbTester.issues().insert(rule, branch, file);
+    dbTester.issues().insert(rule, branch, subModule);
+    dbTester.issues().insert(rule, branch, module);
+    return project;
+  }
+
+  @Test
+  public void delete_branch_content_when_deleting_project() {
+    ComponentDto anotherLivingProject = insertProjectWithBranchAndRelatedData();
+    int projectEntryCount = dbTester.countRowsOfTable("projects");
+    int issueCount = dbTester.countRowsOfTable("issues");
+    int branchCount = dbTester.countRowsOfTable("project_branches");
+
+    ComponentDto projectToDelete = insertProjectWithBranchAndRelatedData();
+    assertThat(dbTester.countRowsOfTable("projects")).isGreaterThan(projectEntryCount);
+    assertThat(dbTester.countRowsOfTable("issues")).isGreaterThan(issueCount);
+    assertThat(dbTester.countRowsOfTable("project_branches")).isGreaterThan(branchCount);
+
+    underTest.deleteRootComponent(dbSession, projectToDelete.uuid());
+    dbSession.commit();
+
+    assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(projectEntryCount);
+    assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(issueCount);
+    assertThat(dbTester.countRowsOfTable("project_branches")).isEqualTo(branchCount);
+  }
+
   @Test
   public void delete_view_and_child() {
     dbTester.prepareDbUnit(getClass(), "view_sub_view_and_tech_project.xml");