aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-01-25 18:15:52 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-01-25 18:15:52 +0100
commit24654671b37c9a14c9f6fe6316e0983f1b1d6844 (patch)
tree9b7680193adb2aed70e379c5be7b9927142edcf4
parent8c6df4977b90cfce0cc6797e3b1ba23b1253a62c (diff)
downloadsonarqube-24654671b37c9a14c9f6fe6316e0983f1b1d6844.tar.gz
sonarqube-24654671b37c9a14c9f6fe6316e0983f1b1d6844.zip
SONAR-2757 close reviews when disabling resources + add unit tests
-rw-r--r--sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java12
-rw-r--r--sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java2
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml5
-rw-r--r--sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java21
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot-result.xml99
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot.xml92
6 files changed, 227 insertions, 4 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java
index 2c2db13d8e3..fc8ff41e816 100644
--- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java
@@ -19,6 +19,7 @@
*/
package org.sonar.core.purge;
+import com.google.common.annotations.VisibleForTesting;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
@@ -123,12 +124,13 @@ public class PurgeDao {
session.increment(9);
}
+ @VisibleForTesting
int disableResource(long resourceId, PurgeMapper mapper) {
mapper.deleteResourceIndex(resourceId);
mapper.setSnapshotIsLastToFalse(resourceId);
mapper.disableResource(resourceId);
- // TODO close reviews
- return 3; // nb of SQL requests
+ mapper.closeResourceReviews(resourceId);
+ return 4; // nb of SQL requests
}
@@ -150,8 +152,9 @@ public class PurgeDao {
}
}
+ @VisibleForTesting
int purgeSnapshot(long snapshotId, PurgeMapper mapper) {
- // note that events are not deleted.
+ mapper.deleteSnapshotEvents(snapshotId);
mapper.deleteSnapshotDependencies(snapshotId);
mapper.deleteSnapshotDuplications(snapshotId);
mapper.deleteSnapshotSource(snapshotId);
@@ -160,9 +163,10 @@ public class PurgeDao {
mapper.deleteSnapshotCharacteristicMeasures(snapshotId);
// TODO SONAR-2061 delete wasted measures (!metric.keepHistory)
mapper.updatePurgeStatusToOne(snapshotId);
- return 7; // nb of SQL requests
+ return 8; // nb of SQL requests
}
+ @VisibleForTesting
int deleteSnapshot(Long snapshotId, PurgeMapper mapper) {
mapper.deleteSnapshotDependencies(snapshotId);
mapper.deleteSnapshotDuplications(snapshotId);
diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
index 7662417ac72..65536f6acc1 100644
--- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
@@ -70,4 +70,6 @@ public interface PurgeMapper {
void deleteResourceReviews(long resourceId);
void deleteResourceEvents(long resourceId);
+
+ void closeResourceReviews(long resourceId);
}
diff --git a/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml b/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml
index 9e857f38a87..44eaed90b43 100644
--- a/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml
@@ -101,6 +101,11 @@
update projects set enabled=${_false} where id=#{id}
</update>
+ <update id="closeResourceReviews" parameterType="long">
+ update reviews set status='CLOSED', updated_at=CURRENT_TIMESTAMP where resource_id=#{id}
+ </update>
+
+
<delete id="deleteResourceIndex" parameterType="long">
delete from resource_index where resource_id=#{id}
</delete>
diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
index 7db16de255d..88f56b9e083 100644
--- a/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/purge/PurgeDaoTest.java
@@ -55,6 +55,27 @@ public class PurgeDaoTest extends DaoTestCase {
"snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "duplications_index", "events", "dependencies");
}
+ /**
+ * Test that all related data is purged.
+ */
+
+ @Test
+ public void shouldPurgeSnapshot() {
+ setupData("shouldPurgeSnapshot");
+
+ SqlSession session = getMyBatis().openSession();
+ try {
+ // this method does not commit and close the session
+ dao.purgeSnapshot(1L, session.getMapper(PurgeMapper.class));
+ session.commit();
+
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ checkTables("shouldPurgeSnapshot",
+ "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "duplications_index", "events", "dependencies", "reviews");
+ }
+
@Test
public void shouldPurgeProject() {
setupData("shouldPurgeProject");
diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot-result.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot-result.xml
new file mode 100644
index 00000000000..0408788f8dc
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot-result.xml
@@ -0,0 +1,99 @@
+<!--
+
+Changes:
+* snapshot.purge_status=1
+* commented-out rows are deleted
+
+Note that measure and review are not deleted.
+
+-->
+<dataset>
+ <snapshots id="1"
+ project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
+ status="P" islast="false" purge_status="1"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" version="[null]" path="[null]"/>
+
+ <!--<snapshot_sources ID="1" SNAPSHOT_ID="1" DATA="foo"/>-->
+
+ <!--<rule_failures ID="1" SNAPSHOT_ID="1"-->
+ <!--switched_off="[null]" permanent_id="[null]" RULE_ID="1" FAILURE_LEVEL="2"-->
+ <!--MESSAGE="msg1" LINE="[null]" COST="[null]"-->
+ <!--created_at="2008-12-02 13:58:00.00"-->
+ <!--checksum="[null]" committer="[null]"/>-->
+
+ <project_measures ID="1" project_id="1" SNAPSHOT_ID="1" RULE_ID="[null]" characteristic_id="[null]"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]"
+ variation_value_5="[null]" rule_priority="[null]" alert_text="[null]" VALUE="10.0" METRIC_ID="1" rules_category_id="[null]"
+ text_value="[null]" tendency="[null]" measure_date="[null]" alert_status="[null]" description="[null]"/>
+
+ <measure_data id="1" measure_id="1" snapshot_id="1" data="[null]"/>
+
+ <!--<dependencies id="1" from_resource_id="1" from_snapshot_id="1" to_resource_id="2" to_snapshot_id="2"-->
+ <!--parent_dependency_id="[null]" project_snapshot_id="[null]"-->
+ <!--dep_usage="USES" dep_weight="1" from_scope="PRJ" to_scope="LIB"/>-->
+
+ <!--<dependencies id="2" from_resource_id="3" from_snapshot_id="3" to_resource_id="1" to_snapshot_id="1"-->
+ <!--parent_dependency_id="[null]" project_snapshot_id="2"-->
+ <!--dep_usage="USES" dep_weight="1" from_scope="LIB" to_scope="PRJ"/>-->
+
+ <!--<events id="1" resource_id="1" snapshot_id="1"-->
+ <!--category="VERSION" description="[null]" name="Version 1.0" event_date="2008-12-02 13:58:00.00" created_at="[null]"/>-->
+
+ <!--<duplications_index project_snapshot_id="1" snapshot_id="1"-->
+ <!--hash="bb" index_in_file="0" start_line="0" end_line="0"/>-->
+
+ <reviews id="1" project_id="1" resource_id="1" status="OPEN"
+ rule_failure_permanent_id="1" resolution="[null]" created_at="[null]" updated_at="[null]" resource_line="200" severity="BLOCKER"
+ user_id="300" assignee_id="300" rule_id="500" manual_violation="[true]" manual_severity="[false]" title="[null]"/>
+
+
+ <!-- The following is not purged but is kept for DBUnit -->
+ <snapshots id="2"
+ project_id="2" parent_snapshot_id="[null]" root_project_id="2" root_snapshot_id="[null]"
+ status="P" islast="false" purge_status="1"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" version="[null]" path="[null]"/>
+
+ <snapshot_sources ID="2" SNAPSHOT_ID="2" DATA="foo"/>
+
+ <rule_failures ID="2" SNAPSHOT_ID="2"
+ switched_off="[null]" permanent_id="[null]" RULE_ID="1" FAILURE_LEVEL="2"
+ MESSAGE="msg1" LINE="[null]" COST="[null]"
+ created_at="2008-12-02 13:58:00.00"
+ checksum="[null]" committer="[null]"/>
+
+ <project_measures ID="2" project_id="2" SNAPSHOT_ID="2" RULE_ID="[null]" characteristic_id="[null]"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]"
+ variation_value_5="[null]" rule_priority="[null]" alert_text="[null]" VALUE="10.0" METRIC_ID="1" rules_category_id="[null]"
+ text_value="[null]" tendency="[null]" measure_date="[null]" alert_status="[null]" description="[null]"/>
+
+ <measure_data id="2" measure_id="2" snapshot_id="2" data="[null]"/>
+
+ <dependencies id="3" from_resource_id="33" from_snapshot_id="33" to_resource_id="44" to_snapshot_id="44"
+ parent_dependency_id="[null]" project_snapshot_id="[null]"
+ dep_usage="USES" dep_weight="1" from_scope="PRJ" to_scope="LIB"/>
+
+ <dependencies id="4" from_resource_id="55" from_snapshot_id="55" to_resource_id="66" to_snapshot_id="66"
+ parent_dependency_id="[null]" project_snapshot_id="2"
+ dep_usage="USES" dep_weight="1" from_scope="LIB" to_scope="PRJ"/>
+
+ <events id="2" resource_id="2" snapshot_id="2"
+ category="VERSION" description="[null]" name="Version 1.0" event_date="2008-12-02 13:58:00.00" created_at="[null]"/>
+
+ <duplications_index project_snapshot_id="2" snapshot_id="2"
+ hash="bb" index_in_file="0" start_line="0" end_line="0"/>
+
+ <reviews id="2" project_id="2" resource_id="2" status="OPEN"
+ rule_failure_permanent_id="1" resolution="[null]" created_at="[null]" updated_at="[null]" resource_line="200" severity="BLOCKER"
+ user_id="300" assignee_id="300" rule_id="500" manual_violation="[true]" manual_severity="[false]" title="[null]"/>
+
+</dataset> \ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot.xml
new file mode 100644
index 00000000000..861fc035895
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot.xml
@@ -0,0 +1,92 @@
+<dataset>
+
+ <snapshots id="1"
+ project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
+ status="P" islast="false" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" version="[null]" path="[null]"/>
+
+ <snapshot_sources ID="1" SNAPSHOT_ID="1" DATA="foo"/>
+
+ <rule_failures ID="1" SNAPSHOT_ID="1"
+ switched_off="[null]" permanent_id="[null]" RULE_ID="1" FAILURE_LEVEL="2"
+ MESSAGE="msg1" LINE="[null]" COST="[null]"
+ created_at="2008-12-02 13:58:00.00"
+ checksum="[null]" committer="[null]"/>
+
+ <project_measures ID="1" project_id="1" SNAPSHOT_ID="1" RULE_ID="[null]" characteristic_id="[null]"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]"
+ variation_value_5="[null]" rule_priority="[null]" alert_text="[null]" VALUE="10.0" METRIC_ID="1" rules_category_id="[null]"
+ text_value="[null]" tendency="[null]" measure_date="[null]" alert_status="[null]" description="[null]"/>
+
+ <measure_data id="1" measure_id="1" snapshot_id="1" data="[null]"/>
+
+ <dependencies id="1" from_resource_id="1" from_snapshot_id="1" to_resource_id="2" to_snapshot_id="2"
+ parent_dependency_id="[null]" project_snapshot_id="[null]"
+ dep_usage="USES" dep_weight="1" from_scope="PRJ" to_scope="LIB"/>
+
+ <dependencies id="2" from_resource_id="3" from_snapshot_id="3" to_resource_id="1" to_snapshot_id="1"
+ parent_dependency_id="[null]" project_snapshot_id="2"
+ dep_usage="USES" dep_weight="1" from_scope="LIB" to_scope="PRJ"/>
+
+ <events id="1" resource_id="1" snapshot_id="1"
+ category="VERSION" description="[null]" name="Version 1.0" event_date="2008-12-02 13:58:00.00" created_at="[null]"/>
+
+ <duplications_index project_snapshot_id="1" snapshot_id="1"
+ hash="bb" index_in_file="0" start_line="0" end_line="0"/>
+
+ <reviews id="1" project_id="1" resource_id="1" status="OPEN"
+ rule_failure_permanent_id="1" resolution="[null]" created_at="[null]" updated_at="[null]" resource_line="200" severity="BLOCKER"
+ user_id="300" assignee_id="300" rule_id="500" manual_violation="[true]" manual_severity="[false]" title="[null]"/>
+
+
+
+ <!-- The following is not purged but is kept for DBUnit -->
+ <snapshots id="2"
+ project_id="2" parent_snapshot_id="[null]" root_project_id="2" root_snapshot_id="[null]"
+ status="P" islast="false" purge_status="1"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" version="[null]" path="[null]"/>
+
+ <snapshot_sources ID="2" SNAPSHOT_ID="2" DATA="foo"/>
+
+ <rule_failures ID="2" SNAPSHOT_ID="2"
+ switched_off="[null]" permanent_id="[null]" RULE_ID="1" FAILURE_LEVEL="2"
+ MESSAGE="msg1" LINE="[null]" COST="[null]"
+ created_at="2008-12-02 13:58:00.00"
+ checksum="[null]" committer="[null]"/>
+
+ <project_measures ID="2" project_id="2" SNAPSHOT_ID="2" RULE_ID="[null]" characteristic_id="[null]"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]"
+ variation_value_5="[null]" rule_priority="[null]" alert_text="[null]" VALUE="10.0" METRIC_ID="1" rules_category_id="[null]"
+ text_value="[null]" tendency="[null]" measure_date="[null]" alert_status="[null]" description="[null]"/>
+
+ <measure_data id="2" measure_id="2" snapshot_id="2" data="[null]"/>
+
+ <dependencies id="3" from_resource_id="33" from_snapshot_id="33" to_resource_id="44" to_snapshot_id="44"
+ parent_dependency_id="[null]" project_snapshot_id="[null]"
+ dep_usage="USES" dep_weight="1" from_scope="PRJ" to_scope="LIB"/>
+
+ <dependencies id="4" from_resource_id="55" from_snapshot_id="55" to_resource_id="66" to_snapshot_id="66"
+ parent_dependency_id="[null]" project_snapshot_id="2"
+ dep_usage="USES" dep_weight="1" from_scope="LIB" to_scope="PRJ"/>
+
+ <events id="2" resource_id="2" snapshot_id="2"
+ category="VERSION" description="[null]" name="Version 1.0" event_date="2008-12-02 13:58:00.00" created_at="[null]"/>
+
+ <duplications_index project_snapshot_id="2" snapshot_id="2"
+ hash="bb" index_in_file="0" start_line="0" end_line="0"/>
+
+ <reviews id="2" project_id="2" resource_id="2" status="OPEN"
+ rule_failure_permanent_id="1" resolution="[null]" created_at="[null]" updated_at="[null]" resource_line="200" severity="BLOCKER"
+ user_id="300" assignee_id="300" rule_id="500" manual_violation="[true]" manual_severity="[false]" title="[null]"/>
+
+</dataset> \ No newline at end of file