]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8402 Purge should delete queued task when deleting a project
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 21 Feb 2017 20:23:04 +0000 (21:23 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 22 Feb 2017 16:17:39 +0000 (17:17 +0100)
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 86b00635d84a544c20a1fc8663d8e03a86c9e9ce..9ce1e097eadd0c7c30152bcaadd45221f0f9f033 100644 (file)
@@ -206,6 +206,13 @@ class PurgeCommands {
     profiler.stop();
   }
 
+  public void deleteCeQueue(String rootUuid) {
+    profiler.start("deleteCeQueue (ce_queue)");
+    purgeMapper.deleteCeQueueByProjectUuid(rootUuid);
+    session.commit();
+    profiler.stop();
+  }
+
   public void deleteWebhookDeliveries(String rootUuid) {
     profiler.start("deleteWebhookDeliveries (webhook_deliveries)");
     purgeMapper.deleteWebhookDeliveriesByProjectUuid(rootUuid);
index 196efb18e56a82e72a646c1ea2bc260d0ea6836c..a2a2419102fe5ef80160e3c1908672d8faeae095 100644 (file)
@@ -158,6 +158,7 @@ public class PurgeDao implements Dao {
     commands.deleteComponents(childrenIds);
     commands.deleteFileSources(rootUuid);
     commands.deleteCeActivity(rootUuid);
+    commands.deleteCeQueue(rootUuid);
     commands.deleteWebhookDeliveries(rootUuid);
   }
 
index 71776d55c8dab526a21258b33f0dfd1d7490433a..d9fa8209e7cf3cf4f5a853565d7dbaf61b4b889c 100644 (file)
@@ -84,7 +84,9 @@ public interface PurgeMapper {
 
   void deleteFileSourcesByUuid(@Param("fileUuids") List<String> fileUuids);
 
-  void deleteCeActivityByProjectUuid(String projectUuid);
+  void deleteCeActivityByProjectUuid(@Param("projectUuid") String projectUuid);
+
+  void deleteCeQueueByProjectUuid(@Param("projectUuid") String projectUuid);
 
   void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid);
 }
index 8f70bf90514b24968002824839642d14df59d5cd..b36aeb526385c0bff7bed682ba2e7f26397c99b4 100644 (file)
   </delete>
 
   <delete id="deleteCeActivityByProjectUuid">
-      delete from ce_activity where component_uuid=#{rootProjectUuid,jdbcType=VARCHAR}
+      delete from ce_activity where component_uuid=#{projectUuid,jdbcType=VARCHAR}
+  </delete>
+
+  <delete id="deleteCeQueueByProjectUuid">
+    delete from ce_queue where component_uuid=#{projectUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteWebhookDeliveriesByProjectUuid">
index 8296b7020a25f77e8a915736207206d953533ba2..6e8b84d3957be2cd05d1f1dae64871e421005cb2 100644 (file)
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import org.apache.commons.lang.math.RandomUtils;
 import org.junit.Rule;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -35,6 +36,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeQueueDto;
+import org.sonar.db.ce.CeQueueDto.Status;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 
@@ -82,7 +84,7 @@ public class PurgeDaoTest {
   public void shouldDeleteHistoricalDataOfDirectoriesAndFiles() {
     dbTester.prepareDbUnit(getClass(), "shouldDeleteHistoricalDataOfDirectoriesAndFiles.xml");
     PurgeConfiguration conf = new PurgeConfiguration(
-      new IdUuidPair(THE_PROJECT_ID, "ABCD"), new String[]{Scopes.DIRECTORY, Scopes.FILE}, 30, System2.INSTANCE, Collections.emptyList());
+      new IdUuidPair(THE_PROJECT_ID, "ABCD"), new String[] {Scopes.DIRECTORY, Scopes.FILE}, 30, System2.INSTANCE, Collections.emptyList());
 
     underTest.purge(dbSession, conf, PurgeListener.EMPTY, new PurgeProfiler());
     dbSession.commit();
@@ -144,8 +146,8 @@ public class PurgeDaoTest {
     dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject);
 
     // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and on on another project
-    insertCeActivity(projectToBeDeleted.uuid());
-    insertCeActivity(anotherLivingProject.uuid());
+    insertCeActivity(projectToBeDeleted);
+    insertCeActivity(anotherLivingProject);
     dbSession.commit();
 
     underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
@@ -154,6 +156,25 @@ public class PurgeDaoTest {
     assertThat(dbTester.countRowsOfTable("ce_activity")).isEqualTo(1);
   }
 
+  @Test
+  public void delete_tasks_in_ce_queue_when_deleting_project() {
+    ComponentDto projectToBeDeleted = dbTester.components().insertProject();
+    ComponentDto anotherLivingProject = dbTester.components().insertProject();
+
+    // Insert 3 rows in CE_QUEUE: two for the project that will be deleted (in order to check that status
+    // is not involved in deletion), and one on another project
+    dbClient.ceQueueDao().insert(dbSession, createCeQueue(projectToBeDeleted, Status.PENDING));
+    dbClient.ceQueueDao().insert(dbSession, createCeQueue(projectToBeDeleted, Status.IN_PROGRESS));
+    dbClient.ceQueueDao().insert(dbSession, createCeQueue(anotherLivingProject, Status.PENDING));
+    dbSession.commit();
+
+    underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
+    dbSession.commit();
+
+    assertThat(dbTester.countRowsOfTable("ce_queue")).isEqualTo(1);
+    assertThat(dbTester.countSql("select count(*) from ce_queue where component_uuid='" + projectToBeDeleted.uuid() + "'")).isEqualTo(0);
+  }
+
   @Test
   public void delete_view_and_child() {
     dbTester.prepareDbUnit(getClass(), "view_sub_view_and_tech_project.xml");
@@ -222,13 +243,20 @@ public class PurgeDaoTest {
     assertThat(selectAllDeliveryUuids(dbTester, dbSession)).containsOnly("D2");
   }
 
-  private CeActivityDto insertCeActivity(String componentUuid) {
+  private CeQueueDto createCeQueue(ComponentDto component, Status status) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setUuid(Uuids.create());
     queueDto.setTaskType(REPORT);
-    queueDto.setComponentUuid(componentUuid);
+    queueDto.setComponentUuid(component.uuid());
     queueDto.setSubmitterLogin("henri");
     queueDto.setCreatedAt(1_300_000_000_000L);
+    queueDto.setStatus(status);
+    return queueDto;
+  }
+
+  private CeActivityDto insertCeActivity(ComponentDto component) {
+    Status unusedStatus = Status.values()[RandomUtils.nextInt(Status.values().length)];
+    CeQueueDto queueDto = createCeQueue(component, unusedStatus);
 
     CeActivityDto dto = new CeActivityDto(queueDto);
     dto.setStatus(CeActivityDto.Status.SUCCESS);