]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9326 drop issues by project_uuid when deleting a project
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 26 May 2017 09:01:42 +0000 (11:01 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 1 Jun 2017 13:18:14 +0000 (15:18 +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/PurgeCommandsTest.java
server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml

index e09b7e7dd1155ce25b27638bd0a2ea754dae85b3..d57990db4bf8fc4754077b66feab704d2bb8b920 100644 (file)
@@ -63,7 +63,7 @@ class PurgeCommands {
     profiler.stop();
 
     List<List<String>> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(purgeMapper.selectAnalysisIdsAndUuids(new PurgeSnapshotQuery().setComponentUuid(rootUuid))),
-      MAX_SNAPSHOTS_PER_QUERY);
+        MAX_SNAPSHOTS_PER_QUERY);
 
     deleteAnalysisDuplications(analysisUuidsPartitions);
 
@@ -80,8 +80,8 @@ class PurgeCommands {
 
   void deleteAnalyses(PurgeSnapshotQuery... queries) {
     List<IdUuidPair> snapshotIds = from(asList(queries))
-      .transformAndConcat(purgeMapper::selectAnalysisIdsAndUuids)
-      .toList();
+        .transformAndConcat(purgeMapper::selectAnalysisIdsAndUuids)
+        .toList();
     deleteAnalyses(snapshotIds);
   }
 
@@ -115,7 +115,7 @@ class PurgeCommands {
     profiler.start("deleteSnapshotWastedMeasures (project_measures)");
     List<Long> metricIdsWithoutHistoricalData = purgeMapper.selectMetricIdsWithoutHistoricalData();
     analysisUuidsPartitions
-      .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData));
+        .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData));
     session.commit();
     profiler.stop();
 
@@ -144,6 +144,18 @@ class PurgeCommands {
     profiler.stop();
   }
 
+  void deleteIssues(String rootUuid) {
+    profiler.start("deleteIssues (issue_changes)");
+    purgeMapper.deleteComponentIssueChanges(rootUuid);
+    session.commit();
+    profiler.stop();
+
+    profiler.start("deleteIssues (issues)");
+    purgeMapper.deleteComponentIssues(rootUuid);
+    session.commit();
+    profiler.stop();
+  }
+
   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);
@@ -168,16 +180,6 @@ class PurgeCommands {
     session.commit();
     profiler.stop();
 
-    profiler.start("deleteComponentIssueChanges (issue_changes)");
-    componentUuidsPartitions.forEach(purgeMapper::deleteComponentIssueChanges);
-    session.commit();
-    profiler.stop();
-
-    profiler.start("deleteComponentIssues (issues)");
-    componentUuidsPartitions.forEach(purgeMapper::deleteComponentIssues);
-    session.commit();
-    profiler.stop();
-
     profiler.start("deleteResource (projects)");
     componentUuidsPartitions.forEach(purgeMapper::deleteComponents);
     session.commit();
index 8c4113682577b1d569aa1b2285cc3c3659242d1d..1e804170efda21eeb2729ba279638af2b3fd9f27 100644 (file)
@@ -162,6 +162,7 @@ public class PurgeDao implements Dao {
     commands.deletePermissions(rootId);
     commands.deleteAnalyses(rootUuid);
     commands.deleteComponents(childrenIds);
+    commands.deleteIssues(rootUuid);
     commands.deleteFileSources(rootUuid);
     commands.deleteCeActivity(rootUuid);
     commands.deleteCeQueue(rootUuid);
index 3d99f8063be3bfac3bad61af66a9a40e3c9b877f..f860348e6e8e52d493d40b3c697aae277a1d5f02 100644 (file)
@@ -68,9 +68,9 @@ public interface PurgeMapper {
 
   List<PurgeableAnalysisDto> selectPurgeableAnalysesWithoutEvents(@Param("componentUuid") String componentUuid);
 
-  void deleteComponentIssueChanges(@Param("componentUuids") List<String> componentUuids);
+  void deleteComponentIssueChanges(@Param("rootUuid") String rootUuid);
 
-  void deleteComponentIssues(@Param("componentUuids") List<String> componentUuids);
+  void deleteComponentIssues(@Param("rootUuid") String rootUuid);
 
   List<String> selectOldClosedIssueKeys(@Param("projectUuid") String projectUuid, @Nullable @Param("toDate") Long toDate);
 
index 6b668699bd9549903b1e6262ba33aab6fdad7d57..7a93f7136be35e0a7c22722aa1b2b7a0efd4ad32 100644 (file)
 
   <delete id="deleteComponentIssueChanges" parameterType="map">
     delete from issue_changes ic
-    where exists (select * from issues i where i.kee=ic.issue_key and i.component_uuid in
-    <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
-      #{componentUuid,jdbcType=VARCHAR}
-    </foreach>
-    )
+    where
+      exists (select 1 from issues i where i.kee=ic.issue_key and i.project_uuid = #{rootUuid,jdbcType=VARCHAR})
   </delete>
 
   <!-- Mssql -->
   <delete id="deleteComponentIssueChanges" databaseId="mssql" parameterType="map">
     delete issue_changes from issue_changes
-    inner join issues on issue_changes.issue_key=issues.kee
-    where issues.component_uuid in
-    <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
-      #{componentUuid,jdbcType=VARCHAR}
-    </foreach>
+    inner join issues on
+      issue_changes.issue_key=issues.kee
+    where
+      issues.project_uuid = #{rootUuid,jdbcType=VARCHAR}
   </delete>
 
   <!-- Mysql -->
   <delete id="deleteComponentIssueChanges" databaseId="mysql" parameterType="map">
-    delete ic from issue_changes as ic, issues as i where ic.issue_key=i.kee and i.component_uuid in
-    <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
-      #{componentUuid,jdbcType=VARCHAR}
-    </foreach>
+    delete ic from issue_changes as ic, issues as i
+    where
+      ic.issue_key=i.kee
+      and i.component_uuid = #{rootUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteComponentIssues" parameterType="map">
-    delete from issues where component_uuid in
-    <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
-      #{componentUuid,jdbcType=VARCHAR}
-    </foreach>
+    delete from issues
+    where project_uuid = #{rootUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteFileSourcesByProjectUuid">
index 688413d0d14c5ae920d45ecf7b6b19a1a235b7d1..25c7ee39c21d6b885d385f78ad844da1d06bf6c7 100644 (file)
@@ -91,8 +91,8 @@ public class PurgeCommandsTest {
     assertThat(dbTester.countRowsOfTable("projects")).isZero();
     assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
     assertThat(dbTester.countRowsOfTable("events")).isEqualTo(3);
-    assertThat(dbTester.countRowsOfTable("issues")).isZero();
-    assertThat(dbTester.countRowsOfTable("issue_changes")).isZero();
+    assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(1);
+    assertThat(dbTester.countRowsOfTable("issue_changes")).isEqualTo(1);
   }
 
 
@@ -105,11 +105,26 @@ public class PurgeCommandsTest {
 
     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(1);
     assertThat(dbTester.countRowsOfTable("snapshots")).isZero();
-    assertThat(dbTester.countRowsOfTable("events")).isEqualTo(0);
+    assertThat(dbTester.countRowsOfTable("events")).isZero();
     assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(1);
     assertThat(dbTester.countRowsOfTable("issue_changes")).isEqualTo(1);
   }
 
+
+  @Test
+  public void shouldDeleteIssuesAndIssueChanges() {
+    dbTester.prepareDbUnit(getClass(), "shouldDeleteResource.xml");
+
+    PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler);
+    purgeCommands.deleteIssues("uuid_1");
+
+    assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(1);
+    assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
+    assertThat(dbTester.countRowsOfTable("events")).isEqualTo(3);
+    assertThat(dbTester.countRowsOfTable("issues")).isZero();
+    assertThat(dbTester.countRowsOfTable("issue_changes")).isZero();
+  }
+
   @Test
   public void deletePermissions_deletes_permissions_of_public_project() {
     OrganizationDto organization = dbTester.organizations().insert();
index 2d521a1fded64c9526c4f47aa59f958ec2980db1..330057a1b73dc9fcdd873c6aa1cc4dd20917f3d2 100644 (file)
@@ -80,7 +80,7 @@
   <issues id="1"
           kee="ABCDE"
           component_uuid="uuid_1"
-          project_uuid="1"
+          project_uuid="uuid_1"
           status="CLOSED"
           resolution="[null]"
           line="200"