]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6834 Purge CE_ACTIVITY when deleting a project
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 27 Oct 2015 10:16:50 +0000 (11:16 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 27 Oct 2015 11:07:04 +0000 (12:07 +0100)
sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java
sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java
sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java
sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
sonar-db/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java
sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/delete_project_in_ce_activity_when_deleting_project.xml [new file with mode: 0644]

index 8e93918e78000b8ed5b7370deac7d934b96f7c19..20a4cf742e4bf5b2c3c8ec39447161b0a7e41044 100644 (file)
@@ -48,7 +48,7 @@ class PurgeCommands {
     return purgeMapper.selectSnapshotIds(query);
   }
 
-  void deleteResources(List<IdUuidPair> componentIdUuids) {
+  void deleteComponents(List<IdUuidPair> componentIdUuids) {
     List<List<Long>> componentIdPartitions = Lists.partition(IdUuidPairs.ids(componentIdUuids), MAX_RESOURCES_PER_QUERY);
     List<List<String>> componentUuidsPartitions = Lists.partition(IdUuidPairs.uuids(componentIdUuids), MAX_RESOURCES_PER_QUERY);
     // Note : do not merge the delete statements into a single loop of resource ids. It's
@@ -221,4 +221,11 @@ class PurgeCommands {
     session.commit();
     profiler.stop();
   }
+
+  public void deleteCeActivity(String rootUuid) {
+    profiler.start("deleteCeActivity (ce_activity)");
+    purgeMapper.deleteCeActivityByProjectUuid(rootUuid);
+    session.commit();
+    profiler.stop();
+  }
 }
index 75213d7d2031f8137380599b9056872e6acb7e74..dc645cfd0e654a42c6bc8d4deb56a2e6edbfd20a 100644 (file)
@@ -173,17 +173,14 @@ public class PurgeDao implements Dao {
     PurgeProfiler profiler = new PurgeProfiler();
     PurgeCommands purgeCommands = new PurgeCommands(session, profiler);
     deleteProject(uuid, mapper(session), purgeCommands);
-    deleteFileSources(uuid, purgeCommands);
     return this;
   }
 
-  private static void deleteFileSources(String rootUuid, PurgeCommands commands) {
-    commands.deleteFileSources(rootUuid);
-  }
-
   private static void deleteProject(String rootUuid, PurgeMapper mapper, PurgeCommands commands) {
     List<IdUuidPair> childrenIds = mapper.selectComponentsByProjectUuid(rootUuid);
-    commands.deleteResources(childrenIds);
+    commands.deleteComponents(childrenIds);
+    commands.deleteFileSources(rootUuid);
+    commands.deleteCeActivity(rootUuid);
   }
 
   private void disableResource(IdUuidPair componentIdUuid, PurgeMapper mapper) {
index e6c8c845fcd956affa133345c7df92368d8f1168..00c9a4d23eb139a4fee9eca5c1c42cb2f8a8d362 100644 (file)
@@ -92,4 +92,6 @@ public interface PurgeMapper {
 
   void deleteFileSourcesByUuid(String fileUuid);
 
+  void deleteCeActivityByProjectUuid(String projectUuid);
+
 }
index 8dcfbf876091ff2eed1144178ddeb2b63a755a11..1185c61863e04e38b5b2cc97be9c70f585ccee14 100644 (file)
     </choose>
   </delete>
 
+    <delete id="deleteCeActivityByProjectUuid">
+        delete from ce_activity where component_uuid=#{rootProjectUuid}
+    </delete>
+
 </mapper>
 
index 49922e94eda5f3e81c255b5b5425959c030ce6f8..3cd0783b93c3bc48fd7f71e3eed6a1dc77c3b280 100644 (file)
@@ -97,7 +97,7 @@ public class PurgeCommandsTest {
   public void shouldDeleteResource() {
     dbTester.prepareDbUnit(getClass(), "shouldDeleteResource.xml");
 
-    new PurgeCommands(dbTester.getSession(), profiler).deleteResources(newArrayList(new IdUuidPair(1L, "1")));
+    new PurgeCommands(dbTester.getSession(), profiler).deleteComponents(newArrayList(new IdUuidPair(1L, "1")));
 
     assertThat(dbTester.countRowsOfTable("projects")).isZero();
     assertThat(dbTester.countRowsOfTable("snapshots")).isZero();
@@ -113,7 +113,7 @@ public class PurgeCommandsTest {
   @Test
   public void should_not_fail_when_deleting_huge_number_of_resources() {
     dbTester.truncateTables();
-    new PurgeCommands(dbTester.getSession(), profiler).deleteResources(getHugeNumberOfIdUuids());
+    new PurgeCommands(dbTester.getSession(), profiler).deleteComponents(getHugeNumberOfIdUuids());
     // The goal of this test is only to check that the query do no fail, not to check result
   }
 
index ac64984651ab56b2b9a3ff9096f88ea8698cb69f..805591f7dc561b5350c2f1e22c63d7818bd40d5c 100644 (file)
@@ -25,13 +25,20 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.System2;
+import org.sonar.core.util.Uuids;
+import org.sonar.db.DbClient;
 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.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
 import org.sonar.test.DbTests;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.db.ce.CeTaskTypes.REPORT;
 
 @Category(DbTests.class)
 public class PurgeDaoTest {
@@ -41,6 +48,8 @@ public class PurgeDaoTest {
   @Rule
   public DbTester dbTester = DbTester.create(system2);
 
+  DbClient dbClient = dbTester.getDbClient();
+
   DbSession dbSession = dbTester.getSession();
 
   PurgeDao underTest = dbTester.getDbClient().purgeDao();
@@ -116,6 +125,23 @@ public class PurgeDaoTest {
     assertThat(dbTester.countRowsOfTable("file_sources")).isZero();
   }
 
+  @Test
+  public void delete_project_in_ce_activity_when_deleting_project() {
+    ComponentDto projectToBeDeleted = ComponentTesting.newProjectDto();
+    ComponentDto anotherLivingProject = ComponentTesting.newProjectDto();
+    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());
+    dbSession.commit();
+
+    underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
+    dbSession.commit();
+
+    assertThat(dbTester.countRowsOfTable("ce_activity")).isEqualTo(1);
+  }
+
   @Test
   public void delete_view_and_child() {
     dbTester.prepareDbUnit(getClass(), "view_sub_view_and_tech_project.xml");
@@ -160,6 +186,23 @@ public class PurgeDaoTest {
     dbTester.assertDbUnit(getClass(), "should_delete_all_closed_issues-result.xml", "issues", "issue_changes");
   }
 
+  private CeActivityDto insertCeActivity(String componentUuid) {
+    CeQueueDto queueDto = new CeQueueDto();
+    queueDto.setUuid(Uuids.create());
+    queueDto.setTaskType(REPORT);
+    queueDto.setComponentUuid(componentUuid);
+    queueDto.setSubmitterLogin("henri");
+    queueDto.setCreatedAt(1_300_000_000_000L);
+
+    CeActivityDto dto = new CeActivityDto(queueDto);
+    dto.setStatus(CeActivityDto.Status.SUCCESS);
+    dto.setStartedAt(1_500_000_000_000L);
+    dto.setExecutedAt(1_500_000_000_500L);
+    dto.setExecutionTimeMs(500L);
+    dbClient.ceActivityDao().insert(dbSession, dto);
+    return dto;
+  }
+
   private static PurgeableSnapshotDto getById(List<PurgeableSnapshotDto> snapshots, long id) {
     for (PurgeableSnapshotDto snapshot : snapshots) {
       if (snapshot.getSnapshotId() == id) {
diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/delete_project_in_ce_activity_when_deleting_project.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/delete_project_in_ce_activity_when_deleting_project.xml
new file mode 100644 (file)
index 0000000..b357e0a
--- /dev/null
@@ -0,0 +1,11 @@
+<dataset>
+
+    <!-- project -->
+    <projects id="1" enabled="[true]" root_id="[null]"
+              uuid="A" project_uuid="A" module_uuid="[null]" module_uuid_path=".A."
+              long_name="[null]" scope="PRJ" qualifier="TRK" kee="project" name="project"
+              description="[null]" language="java" copy_resource_id="[null]" person_id="[null]"
+              authorization_updated_at="[null]"/>
+
+
+</dataset>