From 4d75df2feac832421fa6e6b54167e441f1e51836 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Thu, 13 Mar 2014 14:30:23 +0100 Subject: [PATCH] SONAR-4366 Remove conditions when the associated metric disappears --- .../db/QualityGateConditionDao.java | 14 ++++++++++++ .../db/QualityGateConditionMapper.java | 2 ++ .../db/QualityGateConditionMapper.xml | 5 +++++ .../db/QualityGateConditionDaoTest.java | 5 +++++ .../shouldCleanConditions-result.xml | 11 ++++------ .../shouldCleanConditions.xml | 12 ++++------ .../sonar/server/startup/RegisterMetrics.java | 9 +++++++- .../server/startup/RegisterMetricsTest.java | 22 +++++++++---------- 8 files changed, 52 insertions(+), 28 deletions(-) rename sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml => sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml (51%) rename sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts.xml => sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions.xml (52%) diff --git a/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java b/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java index 8d532995e0e..280cf141865 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java @@ -104,6 +104,20 @@ public class QualityGateConditionDao { getMapper(session).update(qGate.setUpdatedAt(new Date())); } + public void deleteConditionsWithInvalidMetrics() { + SqlSession session = myBatis.openSession(); + try { + deleteConditionsWithInvalidMetrics(session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + } + + public void deleteConditionsWithInvalidMetrics(SqlSession session) { + getMapper(session).deleteConditionsWithInvalidMetrics(); + } + private QualityGateConditionMapper getMapper(SqlSession session) { return session.getMapper(QualityGateConditionMapper.class); } diff --git a/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionMapper.java b/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionMapper.java index 27202c5c8b7..5d2d348fb51 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionMapper.java @@ -32,4 +32,6 @@ public interface QualityGateConditionMapper { QualityGateConditionDto selectById(long id); void delete(long id); + + void deleteConditionsWithInvalidMetrics(); } diff --git a/sonar-core/src/main/resources/org/sonar/core/qualitygate/db/QualityGateConditionMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualitygate/db/QualityGateConditionMapper.xml index afc56564b42..d709af2693d 100644 --- a/sonar-core/src/main/resources/org/sonar/core/qualitygate/db/QualityGateConditionMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/qualitygate/db/QualityGateConditionMapper.xml @@ -37,5 +37,10 @@ where id=#{id} + + delete from quality_gate_conditions + where metric_id not in (select id from metrics where enabled=true) + + diff --git a/sonar-core/src/test/java/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest.java b/sonar-core/src/test/java/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest.java index c4a67a2215e..5ac75ac4b01 100644 --- a/sonar-core/src/test/java/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest.java @@ -84,4 +84,9 @@ public class QualityGateConditionDaoTest extends AbstractDaoTestCase { checkTable("update", "quality_gate_conditions", COLUMNS_WITHOUT_TIMESTAMPS); } + public void shouldCleanConditions() throws Exception { + setupData("shouldCleanConditions"); + dao.deleteConditionsWithInvalidMetrics(); + checkTables("shouldCleanConditions", "quality_gate_conditions"); + } } diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml similarity index 51% rename from sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml rename to sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml index 4d716fe29cc..1dd295d9e98 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml @@ -6,17 +6,14 @@