]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10301 delete from CE_SCANNER_CONTEXT when deleting a project
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 9 Feb 2018 10:08:44 +0000 (11:08 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 13 Feb 2018 13:50:35 +0000 (14:50 +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/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 e4a38a66e9e7d0529f0cbe5b969bf87d4aa7f481..51115390b5d4fe8ae726e17f8c43afa6620f3fee 100644 (file)
@@ -247,6 +247,10 @@ class PurgeCommands {
   }
 
   void deleteCeActivity(String rootUuid) {
+    profiler.start("deleteCeActivity (ce_scanner_context)");
+    purgeMapper.deleteCeScannerContextOfCeActivityByProjectUuid(rootUuid);
+    session.commit();
+    profiler.stop();
     profiler.start("deleteCeActivity (ce_activity)");
     purgeMapper.deleteCeActivityByProjectUuid(rootUuid);
     session.commit();
@@ -254,6 +258,10 @@ class PurgeCommands {
   }
 
   void deleteCeQueue(String rootUuid) {
+    profiler.start("deleteCeQueue (ce_scanner_context)");
+    purgeMapper.deleteCeScannerContextOfCeQueueByProjectUuid(rootUuid);
+    session.commit();
+    profiler.stop();
     profiler.start("deleteCeQueue (ce_queue)");
     purgeMapper.deleteCeQueueByProjectUuid(rootUuid);
     session.commit();
index 11396bed00cc11e7001b5483865411882acca7af..c7626d029711360731d03cdea5d10a5bea990c22 100644 (file)
@@ -90,8 +90,12 @@ public interface PurgeMapper {
 
   void deleteFileSourcesByFileUuid(@Param("fileUuids") List<String> fileUuids);
 
+  void deleteCeScannerContextOfCeActivityByProjectUuid(@Param("projectUuid") String projectUuid);
+
   void deleteCeActivityByProjectUuid(@Param("projectUuid") String projectUuid);
 
+  void deleteCeScannerContextOfCeQueueByProjectUuid(@Param("projectUuid") String projectUuid);
+
   void deleteCeQueueByProjectUuid(@Param("projectUuid") String projectUuid);
 
   void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid);
index 471bd59dbb11ad44b70a0d28273758a8b269b6fa..f12fd5007d9258e3befe501df85fd37d64749200 100644 (file)
     </foreach>
   </delete>
 
+  <delete id="deleteCeScannerContextOfCeActivityByProjectUuid">
+    delete from ce_scanner_context
+    where
+      task_uuid in (select uuid from ce_activity where component_uuid=#{projectUuid,jdbcType=VARCHAR})
+  </delete>
+
   <delete id="deleteCeActivityByProjectUuid">
       delete from ce_activity where component_uuid=#{projectUuid,jdbcType=VARCHAR}
   </delete>
 
+  <delete id="deleteCeScannerContextOfCeQueueByProjectUuid">
+    delete from ce_scanner_context
+    where
+      task_uuid in (select uuid from ce_queue where component_uuid=#{projectUuid,jdbcType=VARCHAR})
+  </delete>
+
   <delete id="deleteCeQueueByProjectUuid">
     delete from ce_queue where component_uuid=#{projectUuid,jdbcType=VARCHAR}
   </delete>
index 7a21a6b0850ddfce07863212cb88c05472c6e5e7..d05790e8b3d6e631c06c2ff1e083fb1e990f0072 100644 (file)
@@ -37,6 +37,8 @@ import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.System2;
+import org.sonar.core.util.CloseableIterator;
+import org.sonar.core.util.UuidFactoryFast;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -205,12 +207,12 @@ public class PurgeDaoTest {
   }
 
   @Test
-  public void delete_project_in_ce_activity_when_deleting_project() {
+  public void delete_row_in_ce_activity_when_deleting_project() {
     ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
     ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
     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
+    // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project
     insertCeActivity(projectToBeDeleted);
     insertCeActivity(anotherLivingProject);
     dbSession.commit();
@@ -221,6 +223,31 @@ public class PurgeDaoTest {
     assertThat(dbTester.countRowsOfTable("ce_activity")).isEqualTo(1);
   }
 
+  @Test
+  public void delete_row_in_ce_scanner_context_referring_to_a_row_in_ce_activity_when_deleting_project() {
+    ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+    ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+    dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject);
+
+    // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project
+    CeActivityDto toBeDeleted = insertCeActivity(projectToBeDeleted);
+    insertCeScannerContext(toBeDeleted.getUuid());
+    CeActivityDto toNotDelete = insertCeActivity(anotherLivingProject);
+    insertCeScannerContext(toNotDelete.getUuid());
+    insertCeScannerContext("non existing task");
+    dbSession.commit();
+
+    underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
+    dbSession.commit();
+
+    assertThat(dbTester.select("select uuid as \"UUID\" from ce_activity"))
+      .extracting(row -> (String) row.get("UUID"))
+      .containsOnly(toNotDelete.getUuid());
+    assertThat(dbTester.select("select task_uuid as \"UUID\" from ce_scanner_context"))
+      .extracting(row -> (String) row.get("UUID"))
+      .containsOnly(toNotDelete.getUuid(), "non existing task");
+  }
+
   @Test
   public void delete_tasks_in_ce_queue_when_deleting_project() {
     ComponentDto projectToBeDeleted = dbTester.components().insertPrivateProject();
@@ -240,6 +267,31 @@ public class PurgeDaoTest {
     assertThat(dbTester.countSql("select count(*) from ce_queue where component_uuid='" + projectToBeDeleted.uuid() + "'")).isEqualTo(0);
   }
 
+  @Test
+  public void delete_row_in_ce_scanner_context_referring_to_a_row_in_ce_queue_when_deleting_project() {
+    ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+    ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+    dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject);
+
+    // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project
+    CeQueueDto toBeDeleted = insertCeQueue(projectToBeDeleted);
+    insertCeScannerContext(toBeDeleted.getUuid());
+    CeQueueDto toNotDelete = insertCeQueue(anotherLivingProject);
+    insertCeScannerContext(toNotDelete.getUuid());
+    insertCeScannerContext("non existing task");
+    dbSession.commit();
+
+    underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
+    dbSession.commit();
+
+    assertThat(dbTester.select("select uuid as \"UUID\" from ce_queue"))
+      .extracting(row -> (String) row.get("UUID"))
+      .containsOnly(toNotDelete.getUuid());
+    assertThat(dbTester.select("select task_uuid as \"UUID\" from ce_scanner_context"))
+      .extracting(row -> (String) row.get("UUID"))
+      .containsOnly(toNotDelete.getUuid(), "non existing task");
+  }
+
   private ComponentDto insertProjectWithBranchAndRelatedData() {
     RuleDefinitionDto rule = dbTester.rules().insert();
     ComponentDto project = dbTester.components().insertMainBranch();
@@ -641,6 +693,25 @@ public class PurgeDaoTest {
     return dto;
   }
 
+  private CeQueueDto insertCeQueue(ComponentDto project) {
+    CeQueueDto res = new CeQueueDto()
+      .setUuid(UuidFactoryFast.getInstance().create())
+      .setTaskType("foo")
+      .setComponentUuid(project.uuid())
+      .setStatus(Status.PENDING)
+      .setExecutionCount(0)
+      .setCreatedAt(1_2323_222L)
+      .setUpdatedAt(1_2323_222L);
+    dbClient.ceQueueDao().insert(dbSession, res);
+    dbSession.commit();
+    return res;
+  }
+
+  private void insertCeScannerContext(String uuid) {
+    dbClient.ceScannerContextDao().insert(dbSession, uuid, CloseableIterator.from(Arrays.asList("a", "b", "c").iterator()));
+    dbSession.commit();
+  }
+
   private static PurgeableAnalysisDto getById(List<PurgeableAnalysisDto> snapshots, String uuid) {
     return snapshots.stream()
       .filter(snapshot -> uuid.equals(snapshot.getAnalysisUuid()))