aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-02-09 11:08:44 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-02-13 14:50:35 +0100
commit776bea268bc8be5c45306819be0548c6300ec4dd (patch)
tree6190aa82f1717d3cbdd28ab2190b395c34059806 /server
parentab801b91e6f61f587a3e8f059b7c0953814617fa (diff)
downloadsonarqube-776bea268bc8be5c45306819be0548c6300ec4dd.tar.gz
sonarqube-776bea268bc8be5c45306819be0548c6300ec4dd.zip
SONAR-10301 delete from CE_SCANNER_CONTEXT when deleting a project
Diffstat (limited to 'server')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java8
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java4
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml12
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java75
4 files changed, 97 insertions, 2 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
index e4a38a66e9e..51115390b5d 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
@@ -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();
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
index 11396bed00c..c7626d02971 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
@@ -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);
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
index 471bd59dbb1..f12fd5007d9 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
@@ -312,10 +312,22 @@
</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>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
index 7a21a6b0850..d05790e8b3d 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
@@ -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();
@@ -222,6 +224,31 @@ public class PurgeDaoTest {
}
@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();
ComponentDto anotherLivingProject = 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()))