From c0d6299259de839375a9653f0ff2b19cba2ce042 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 25 Jan 2012 19:26:18 +0100 Subject: SONAR-2061 add the flag Metric#deleteHistoricalData. Default value is false. * New column METRICS.DELETE_HISTORICAL_DATA (boolean, nullable) * Hidden core metrics and some data metrics have this new field to true --- .../main/java/org/sonar/core/purge/PurgeDao.java | 7 ++-- .../java/org/sonar/core/purge/PurgeMapper.java | 4 +- .../java/org/sonar/jpa/entity/SchemaMigration.java | 2 +- .../org/sonar/core/persistence/rows-derby.sql | 1 + .../org/sonar/core/persistence/schema-derby.ddl | 3 +- .../resources/org/sonar/core/purge/PurgeMapper.xml | 9 ++--- .../java/org/sonar/core/purge/PurgeDaoTest.java | 34 ++++++++++++++++- .../core/metric/CacheMetricFinderTest/shared.xml | 6 +-- .../core/metric/DefaultMetricFinderTest/shared.xml | 6 +-- ...ouldCloseReviewWhenDisablingResource-result.xml | 15 ++++++++ .../shouldCloseReviewWhenDisablingResource.xml | 16 ++++++++ ...eteWastedMeasuresWhenPurgingSnapshot-result.xml | 44 ++++++++++++++++++++++ ...ouldDeleteWastedMeasuresWhenPurgingSnapshot.xml | 44 ++++++++++++++++++++++ 13 files changed, 169 insertions(+), 22 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot.xml (limited to 'sonar-core') 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 fc8ff41e816..8bbe33b44b8 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 @@ -67,6 +67,7 @@ public class PurgeDao { purgeSnapshot(snapshotId, purgeMapper); } }); + // must be executed at the end for reentrance purgeSnapshot(projectSnapshotId, purgeMapper); } @@ -159,11 +160,9 @@ public class PurgeDao { mapper.deleteSnapshotDuplications(snapshotId); mapper.deleteSnapshotSource(snapshotId); mapper.deleteSnapshotViolations(snapshotId); - mapper.deleteSnapshotRuleMeasures(snapshotId); - mapper.deleteSnapshotCharacteristicMeasures(snapshotId); - // TODO SONAR-2061 delete wasted measures (!metric.keepHistory) + mapper.deleteSnapshotWastedMeasures(snapshotId); mapper.updatePurgeStatusToOne(snapshotId); - return 8; // nb of SQL requests + return 7; // nb of SQL requests } @VisibleForTesting 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 65536f6acc1..38cccfe0e60 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 @@ -41,9 +41,7 @@ public interface PurgeMapper { void deleteSnapshotViolations(long snapshotId); - void deleteSnapshotRuleMeasures(long snapshotId); - - void deleteSnapshotCharacteristicMeasures(long snapshotId); + void deleteSnapshotWastedMeasures(long snapshotId); void updatePurgeStatusToOne(long snapshotId); diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java index a6e8c7653e0..4481fef39a7 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java @@ -34,7 +34,7 @@ public class SchemaMigration { public final static int VERSION_UNKNOWN = -1; - public static final int LAST_VERSION = 253; + public static final int LAST_VERSION = 254; public static final int VERSION_2_13 = 241; public final static String TABLE_NAME = "schema_migrations"; diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql index 6b842872b00..fd4626b9267 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql @@ -172,6 +172,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('250'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('251'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('252'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('253'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('254'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl index 81491225d86..1dcd74b9e03 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl @@ -450,7 +450,8 @@ CREATE TABLE "METRICS" ( "WORST_VALUE" DECIMAL(30,20), "BEST_VALUE" DECIMAL(30,20), "OPTIMIZED_BEST_VALUE" BOOLEAN, - "HIDDEN" BOOLEAN + "HIDDEN" BOOLEAN, + "DELETE_HISTORICAL_DATA" BOOLEAN ); CREATE TABLE "LOADED_TEMPLATES" ( 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 44eaed90b43..3f942d66c6b 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 @@ -85,12 +85,9 @@ delete from snapshots where id=#{id} - - delete from project_measures where snapshot_id=#{id} and rule_id is not null - - - - delete from project_measures where snapshot_id=#{id} and characteristic_id is not null + + delete from project_measures where snapshot_id=#{id} and + (characteristic_id is not null or rule_id is not null or metric_id in (select id from metrics where delete_historical_data=${_true})) 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 88f56b9e083..b6644f9b275 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 @@ -58,7 +58,6 @@ public class PurgeDaoTest extends DaoTestCase { /** * Test that all related data is purged. */ - @Test public void shouldPurgeSnapshot() { setupData("shouldPurgeSnapshot"); @@ -76,6 +75,39 @@ public class PurgeDaoTest extends DaoTestCase { "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "duplications_index", "events", "dependencies", "reviews"); } + @Test + public void shouldDeleteWastedMeasuresWhenPurgingSnapshot() { + setupData("shouldDeleteWastedMeasuresWhenPurgingSnapshot"); + + 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("shouldDeleteWastedMeasuresWhenPurgingSnapshot", "project_measures"); + } + + @Test + public void shouldCloseReviewWhenDisablingResource() { + setupData("shouldCloseReviewWhenDisablingResource"); + + SqlSession session = getMyBatis().openSession(); + try { + // this method does not commit and close the session + dao.disableResource(1L, session.getMapper(PurgeMapper.class)); + session.commit(); + + } finally { + MyBatis.closeQuietly(session); + } + checkTables("shouldCloseReviewWhenDisablingResource", /* excluded column */new String[]{"updated_at"}, "reviews"); + } + + @Test public void shouldPurgeProject() { setupData("shouldPurgeProject"); diff --git a/sonar-core/src/test/resources/org/sonar/core/metric/CacheMetricFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/metric/CacheMetricFinderTest/shared.xml index 85709bcbf98..dd645d66ec1 100644 --- a/sonar-core/src/test/resources/org/sonar/core/metric/CacheMetricFinderTest/shared.xml +++ b/sonar-core/src/test/resources/org/sonar/core/metric/CacheMetricFinderTest/shared.xml @@ -1,12 +1,12 @@ - \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml index 85709bcbf98..dd645d66ec1 100644 --- a/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml +++ b/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml @@ -1,12 +1,12 @@ - \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource-result.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource-result.xml new file mode 100644 index 00000000000..562d1940e0d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource-result.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource.xml new file mode 100644 index 00000000000..6f6ca9ca0a1 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldCloseReviewWhenDisablingResource.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot-result.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot-result.xml new file mode 100644 index 00000000000..fd36604b248 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot-result.xml @@ -0,0 +1,44 @@ + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot.xml new file mode 100644 index 00000000000..17db23c661a --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeDaoTest/shouldDeleteWastedMeasuresWhenPurgingSnapshot.xml @@ -0,0 +1,44 @@ + + + \ No newline at end of file -- cgit v1.2.3