From 24654671b37c9a14c9f6fe6316e0983f1b1d6844 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 25 Jan 2012 18:15:52 +0100 Subject: [PATCH] SONAR-2757 close reviews when disabling resources + add unit tests --- .../java/org/sonar/core/purge/PurgeDao.java | 12 ++- .../org/sonar/core/purge/PurgeMapper.java | 2 + .../org/sonar/core/purge/PurgeMapper.xml | 5 + .../org/sonar/core/purge/PurgeDaoTest.java | 21 ++++ .../shouldPurgeSnapshot-result.xml | 99 +++++++++++++++++++ .../PurgeDaoTest/shouldPurgeSnapshot.xml | 92 +++++++++++++++++ 6 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldPurgeSnapshot.xml 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 reviews set status='CLOSED', updated_at=CURRENT_TIMESTAMP where resource_id=#{id} + + + delete from resource_index where resource_id=#{id} 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.39.5