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);
}
QualityGateConditionDto selectById(long id);
void delete(long id);
+
+ void deleteConditionsWithInvalidMetrics();
}
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>
checkTable("update", "quality_gate_conditions", COLUMNS_WITHOUT_TIMESTAMPS);
}
+ public void shouldCleanConditions() throws Exception {
+ setupData("shouldCleanConditions");
+ dao.deleteConditionsWithInvalidMetrics();
+ checkTables("shouldCleanConditions", "quality_gate_conditions");
+ }
}
--- /dev/null
+<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
--- /dev/null
+<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
*/
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;
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() {
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();
query.setParameter("enabled", Boolean.TRUE);
query.executeUpdate();
session.commit();
+*/
}
protected void register(List<Metric> metrics) {
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;
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 {
.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");
}
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)
.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);
.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();
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();
@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();
}
}
+++ /dev/null
-<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
+++ /dev/null
-<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