]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4366 Remove conditions when the associated metric disappears
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 13 Mar 2014 13:30:23 +0000 (14:30 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 13 Mar 2014 15:16:21 +0000 (16:16 +0100)
sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java
sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionMapper.java
sonar-core/src/main/resources/org/sonar/core/qualitygate/db/QualityGateConditionMapper.xml
sonar-core/src/test/java/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest.java
sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions.xml [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/startup/RegisterMetrics.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterMetricsTest.java
sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml [deleted file]
sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts.xml [deleted file]

index 8d532995e0e7454232d5f31eee44ab423c8ddd38..280cf1418659f0bb7471e880a96796ee6d46e947 100644 (file)
@@ -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);
   }
index 27202c5c8b71422cf7e03f4160030984bbb82d1b..5d2d348fb51983eef63bce26f72f200aa6068df2 100644 (file)
@@ -32,4 +32,6 @@ public interface QualityGateConditionMapper {
   QualityGateConditionDto selectById(long id);
 
   void delete(long id);
+
+  void deleteConditionsWithInvalidMetrics();
 }
index afc56564b42f39b7a9d98d2705542f203499c8cc..d709af2693dcbb84e27d03a43f6e4a5d4d42a93e 100644 (file)
     where id=#{id}
   </update>
 
+  <delete id="deleteConditionsWithInvalidMetrics">
+    delete from quality_gate_conditions
+    where metric_id not in (select id from metrics where enabled=true)
+  </delete>
+
 </mapper>
 
index c4a67a2215e72e3dd93758b7c6fe772cc0835697..5ac75ac4b01af5b07823ed1dc239124153854860 100644 (file)
@@ -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-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions-result.xml
new file mode 100644 (file)
index 0000000..1dd295d
--- /dev/null
@@ -0,0 +1,19 @@
+<dataset>
+
+  <metrics delete_historical_data="false" id="1" name="key1" val_type="INT" description="description"  domain="domain"
+           short_name="name1" qualitative="false" user_managed="false" enabled="true" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false" />
+
+  <metrics delete_historical_data="false" id="2" name="disabledkey2" val_type="INT" description="description"  domain="domain"
+           short_name="name2" qualitative="false" user_managed="false" enabled="false" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false"/>
+
+  <!-- ok -->
+  <quality_gate_conditions id="1" qgate_id="1" metric_id="1" operator="GT" value_error="30" value_warning="[null]" period="[null]"/>
+  <quality_gate_conditions id="2" qgate_id="2" metric_id="1" operator="GT" value_error="[null]" value_warning="150" period="[null]"/>
+
+  <!-- disabled metric -->
+  <!--<quality_gate_conditions id="3" qgate_id="1" metric_id="2" operator="GT" value_error="30" value_warning="[null]" period="[null]"/>-->
+
+  <!-- unknown metric -->
+  <!--<quality_gate_conditions id="4" qgate_id="1" metric_id="999" operator="GT" value_error="30" value_warning="[null]" period="[null]"/>-->
+
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions.xml b/sonar-core/src/test/resources/org/sonar/core/qualitygate/db/QualityGateConditionDaoTest/shouldCleanConditions.xml
new file mode 100644 (file)
index 0000000..73a9a36
--- /dev/null
@@ -0,0 +1,19 @@
+<dataset>
+
+  <metrics delete_historical_data="false" id="1" name="key1" val_type="INT" description="description"  domain="domain"
+           short_name="name1" qualitative="false" user_managed="false" enabled="true" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false"/>
+
+  <metrics delete_historical_data="false" id="2" name="disabledkey2" val_type="INT" description="description"  domain="domain"
+           short_name="name2" qualitative="false" user_managed="false" enabled="false" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false"/>
+
+  <!-- ok -->
+  <quality_gate_conditions id="1" qgate_id="1" metric_id="1" operator="GT" value_error="30" value_warning="[null]" period="[null]"/>
+  <quality_gate_conditions id="2" qgate_id="2" metric_id="1" operator="GT" value_error="[null]" value_warning="150" period="[null]"/>
+
+  <!-- disabled metric -->
+  <quality_gate_conditions id="3" qgate_id="1" metric_id="2" operator="GT" value_error="30" value_warning="[null]" period="[null]"/>
+
+  <!-- unknown metric -->
+  <quality_gate_conditions id="4" qgate_id="1" metric_id="999" operator="GT" value_error="30" value_warning="[null]" period="[null]"/>
+
+</dataset>
\ No newline at end of file
index 43027bf58cf91ef24fb5732534db608783f917dd..5668b8e297edef4ba751a92c57d9c25d69c260d2 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.server.startup;
 
+import org.sonar.core.qualitygate.db.QualityGateConditionDao;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
@@ -47,11 +49,13 @@ public class RegisterMetrics {
   private final MeasuresDao measuresDao;
   private final Metrics[] metricsRepositories;
   private final DatabaseSession session;
+  private final QualityGateConditionDao conditionDao;
 
-  public RegisterMetrics(DatabaseSession session, MeasuresDao measuresDao, Metrics[] metricsRepositories) {
+  public RegisterMetrics(DatabaseSession session, MeasuresDao measuresDao, QualityGateConditionDao conditionDao, Metrics[] metricsRepositories) {
     this.session = session;
     this.measuresDao = measuresDao;
     this.metricsRepositories = metricsRepositories;
+    this.conditionDao = conditionDao;
   }
 
   public void start() {
@@ -105,6 +109,8 @@ public class RegisterMetrics {
 
   protected void cleanAlerts() {
     LOG.info("cleaning alert thresholds...");
+    conditionDao.deleteConditionsWithInvalidMetrics();
+/*
     Query query = session.createQuery("delete from " + Alert.class.getSimpleName() + " a where NOT EXISTS(FROM Metric m WHERE m=a.metric))");
     query.executeUpdate();
 
@@ -112,6 +118,7 @@ public class RegisterMetrics {
     query.setParameter("enabled", Boolean.TRUE);
     query.executeUpdate();
     session.commit();
+*/
   }
 
   protected void register(List<Metric> metrics) {
index 51d7fb06485039a1fd6d4fb846c9b714b899fbdd..59a4e6d3ce138d59c395956168df4a21122a3d39 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.measures.Metrics;
+import org.sonar.core.qualitygate.db.QualityGateConditionDao;
 import org.sonar.jpa.dao.MeasuresDao;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
@@ -31,8 +32,7 @@ import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 public class RegisterMetricsTest extends AbstractDbUnitTestCase {
 
@@ -55,7 +55,7 @@ public class RegisterMetricsTest extends AbstractDbUnitTestCase {
         .setUserManaged(false)
         .create();
 
-    RegisterMetrics synchronizer = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), null);
+    RegisterMetrics synchronizer = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), mock(QualityGateConditionDao.class), null);
     synchronizer.register(Arrays.asList(metric1, metric2));
     checkTables("shouldSaveIfNew", "metrics");
   }
@@ -64,7 +64,7 @@ public class RegisterMetricsTest extends AbstractDbUnitTestCase {
   public void shouldUpdateIfAlreadyExists() {
     setupData("shouldUpdateIfAlreadyExists");
 
-    RegisterMetrics synchronizer = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), null);
+    RegisterMetrics synchronizer = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), mock(QualityGateConditionDao.class), null);
     synchronizer.register(newArrayList(new Metric.Builder("key", "new short name", Metric.ValueType.FLOAT)
         .setDescription("new description")
         .setDirection(-1)
@@ -88,7 +88,7 @@ public class RegisterMetricsTest extends AbstractDbUnitTestCase {
         .create()));
 
     MeasuresDao measuresDao = new MeasuresDao(getSession());
-    RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, new Metrics[]{metrics});
+    RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, mock(QualityGateConditionDao.class), new Metrics[]{metrics});
     List<Metric> result = loader.getMetricsRepositories();
 
     assertThat(result).hasSize(1);
@@ -108,7 +108,7 @@ public class RegisterMetricsTest extends AbstractDbUnitTestCase {
         .create()));
 
     MeasuresDao measuresDao = new MeasuresDao(getSession());
-    RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, new Metrics[]{metrics});
+    RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, mock(QualityGateConditionDao.class), new Metrics[]{metrics});
     List<Metric> result = loader.getMetricsRepositories();
 
     assertThat(result).isEmpty();
@@ -119,7 +119,7 @@ public class RegisterMetricsTest extends AbstractDbUnitTestCase {
     setupData("shouldEnableOnlyLoadedMetrics");
 
     MeasuresDao measuresDao = new MeasuresDao(getSession());
-    RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, null);
+    RegisterMetrics loader = new RegisterMetrics(getSession(), measuresDao, mock(QualityGateConditionDao.class), null);
     loader.start();
 
     assertThat(measuresDao.getMetric("deprecated").getEnabled()).isFalse();
@@ -128,11 +128,9 @@ public class RegisterMetricsTest extends AbstractDbUnitTestCase {
 
   @Test
   public void shouldCleanAlerts() {
-    setupData("shouldCleanAlerts");
-
-    RegisterMetrics loader = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), null);
+    QualityGateConditionDao conditionDao = mock(QualityGateConditionDao.class);
+    RegisterMetrics loader = new RegisterMetrics(getSession(), new MeasuresDao(getSession()), conditionDao, null);
     loader.cleanAlerts();
-
-    checkTables("shouldCleanAlerts", "metrics", "alerts");
+    verify(conditionDao).deleteConditionsWithInvalidMetrics();
   }
 }
diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts-result.xml
deleted file mode 100644 (file)
index 4d716fe..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<dataset>
-
-  <metrics delete_historical_data="false" id="1" name="key1" val_type="INT" description="description"  domain="domain"
-           short_name="name1" qualitative="false" user_managed="false" enabled="true" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false" />
-
-  <metrics delete_historical_data="false" id="2" name="disabledkey2" val_type="INT" description="description"  domain="domain"
-           short_name="name2" qualitative="false" user_managed="false" enabled="false" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false"/>
-
-  <rules_profiles id="1" version="1" used_profile="true" name="profile1" language="JAV" />
-  <rules_profiles id="2" version="1" used_profile="true" name="profile2" language="JAV" />
-
-  <!-- ok -->
-  <alerts id="1" profile_id="1" metric_id="1" operator=">" value_error="30" value_warning="[null]" period="[null]"/>
-  <alerts id="2" profile_id="2" metric_id="1" operator=">" value_error="[null]" value_warning="150" period="[null]"/>
-
-  <!-- disabled metric -->
-  <!--<alerts id="3" profile_id="1" metric_id="2" operator=">" value_error="30" value_warning="[null]" period="[null]"/>-->
-
-  <!-- unknown metric -->
-  <!--<alerts id="4" profile_id="1" metric_id="999" operator=">" value_error="30" value_warning="[null]" period="[null]"/>-->
-
-</dataset>
\ No newline at end of file
diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts.xml b/sonar-server/src/test/resources/org/sonar/server/startup/RegisterMetricsTest/shouldCleanAlerts.xml
deleted file mode 100644 (file)
index 1443f99..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<dataset>
-
-  <metrics delete_historical_data="false" id="1" name="key1" val_type="INT" description="description"  domain="domain"
-           short_name="name1" qualitative="false" user_managed="false" enabled="true" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false"/>
-
-  <metrics delete_historical_data="false" id="2" name="disabledkey2" val_type="INT" description="description"  domain="domain"
-           short_name="name2" qualitative="false" user_managed="false" enabled="false" origin="JAV" worst_value="[null]" optimized_best_value="false" best_value="[null]" direction="1" hidden="false"/>
-
-
-  <rules_profiles id="1" version="1" used_profile="true" name="profile1" language="JAV" />
-  <rules_profiles id="2" version="1" used_profile="true" name="profile2" language="JAV" />
-
-  <!-- ok -->
-  <alerts id="1" profile_id="1" metric_id="1" operator=">" value_error="30" value_warning="[null]" period="[null]"/>
-  <alerts id="2" profile_id="2" metric_id="1" operator=">" value_error="[null]" value_warning="150" period="[null]"/>
-
-  <!-- disabled metric -->
-  <alerts id="3" profile_id="1" metric_id="2" operator=">" value_error="30" value_warning="[null]" period="[null]"/>
-
-  <!-- unknown metric -->
-  <alerts id="4" profile_id="1" metric_id="999" operator=">" value_error="30" value_warning="[null]" period="[null]"/>
-
-</dataset>
\ No newline at end of file