diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2019-01-04 14:57:52 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-01-08 20:21:07 +0100 |
commit | 5b69e096962a17caba0385a72daa032a5a978f85 (patch) | |
tree | a774ce4f132c6e6dfe7f2a108d39cbe504c99118 /server/sonar-db-migration | |
parent | d63621dffbe21b0204c41593bfa63c267a33ea3d (diff) | |
download | sonarqube-5b69e096962a17caba0385a72daa032a5a978f85.tar.gz sonarqube-5b69e096962a17caba0385a72daa032a5a978f85.zip |
SONAR-11577 Fix direction of certain metrics
metrics: tests, conditions_to_cover, new_conditions_to_cover
Diffstat (limited to 'server/sonar-db-migration')
5 files changed, 183 insertions, 2 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76.java index 8d7a7896bc4..235b8af2800 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76.java @@ -29,6 +29,8 @@ public class DbVersion76 implements DbVersion { registry .add(2500, "Create table USER_PROPERTIES", CreateUserPropertiesTable.class) .add(2501, "Add index in table USER_PROPERTIES", AddUniqueIndexInUserPropertiesTable.class) - .add(2506, "Migrate quality gate conditions using warning, period and no more supported operations", MigrateNoMoreUsedQualityGateConditions.class); + .add(2505, "Fix the direction values of certain metrics (prepare for migration of conditions)", FixDirectionOfMetrics.class) + .add(2506, "Migrate quality gate conditions using warning, period and no more supported operations", MigrateNoMoreUsedQualityGateConditions.class) + ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetrics.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetrics.java new file mode 100644 index 00000000000..8bbc6992fbe --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetrics.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v76; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.SupportsBlueGreen; +import org.sonar.server.platform.db.migration.step.DataChange; + +@SupportsBlueGreen +public class FixDirectionOfMetrics extends DataChange { + + public FixDirectionOfMetrics(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + String sql = "UPDATE metrics SET direction = ? WHERE name = ? AND direction != ?"; + + context.prepareUpsert(sql) + .setInt(1, 1) + .setString(2, "tests") + .setInt(3, 1) + .execute() + .commit(); + + context.prepareUpsert(sql) + .setInt(1, -1) + .setString(2, "conditions_to_cover") + .setInt(3, -1) + .execute() + .commit(); + + context.prepareUpsert(sql) + .setInt(1, -1) + .setString(2, "new_conditions_to_cover") + .setInt(3, -1) + .execute() + .commit(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76Test.java index 88ba43ce533..bec86f8565a 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/DbVersion76Test.java @@ -35,7 +35,7 @@ public class DbVersion76Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 3); + verifyMigrationCount(underTest, 4); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest.java new file mode 100644 index 00000000000..5e8d1898db7 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest.java @@ -0,0 +1,101 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v76; + +import com.google.common.collect.ImmutableMap; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(DataProviderRunner.class) +public class FixDirectionOfMetricsTest { + + private static final String TESTS_METRIC_NAME = "tests"; + private static final String CONDITIONS_TO_COVER_METRIC_NAME = "conditions_to_cover"; + private static final String NEW_CONDITIONS_TO_COVER_METRIC_NAME = "new_conditions_to_cover"; + private static final String UNRELATED_METRIC_NAME = "unrelated"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(FixDirectionOfMetricsTest.class, "metrics.sql"); + + private static long idCounter = 1; + + private FixDirectionOfMetrics underTest = new FixDirectionOfMetrics(db.database()); + + @Test + @UseDataProvider("interestingCombinationsOfInitialMetricsAndValues") + public void fixes_direction_of_relevant_metrics(Map<String, Integer> initialMetricsAndValues) throws SQLException { + insertMetric(TESTS_METRIC_NAME, initialMetricsAndValues.get(TESTS_METRIC_NAME)); + insertMetric(CONDITIONS_TO_COVER_METRIC_NAME, initialMetricsAndValues.get(CONDITIONS_TO_COVER_METRIC_NAME)); + insertMetric(NEW_CONDITIONS_TO_COVER_METRIC_NAME, initialMetricsAndValues.get(NEW_CONDITIONS_TO_COVER_METRIC_NAME)); + insertMetric(UNRELATED_METRIC_NAME, initialMetricsAndValues.get(UNRELATED_METRIC_NAME)); + + underTest.execute(); + + assertThat(selectMetricDirection(TESTS_METRIC_NAME)).isEqualTo(1L); + assertThat(selectMetricDirection(CONDITIONS_TO_COVER_METRIC_NAME)).isEqualTo(-1L); + assertThat(selectMetricDirection(NEW_CONDITIONS_TO_COVER_METRIC_NAME)).isEqualTo(-1L); + assertThat(selectMetricDirection(UNRELATED_METRIC_NAME)).isEqualTo((long) initialMetricsAndValues.get(UNRELATED_METRIC_NAME)); + } + + @DataProvider + public static Object[][] interestingCombinationsOfInitialMetricsAndValues() { + List<Map<String, Integer>> mappings = new ArrayList<>(); + int[] directions = {-1, 0, 1}; + for (int unrelatedDirection : directions) { + // the target metrics with old (wrong) direction, and an unrelated metric with any direction + mappings.add(ImmutableMap.of( + TESTS_METRIC_NAME, -1, + CONDITIONS_TO_COVER_METRIC_NAME, 0, + NEW_CONDITIONS_TO_COVER_METRIC_NAME, 0, + UNRELATED_METRIC_NAME, unrelatedDirection)); + + // the target metrics with correct direction, and an unrelated metric with any direction + mappings.add(ImmutableMap.of( + TESTS_METRIC_NAME, 1, + CONDITIONS_TO_COVER_METRIC_NAME, -1, + NEW_CONDITIONS_TO_COVER_METRIC_NAME, -1, + UNRELATED_METRIC_NAME, unrelatedDirection)); + } + return mappings.stream().map(m -> new Object[] {m}).toArray(Object[][]::new); + } + + private void insertMetric(String name, int direction) { + long id = idCounter++; + db.executeInsert("METRICS", + "ID", id, + "NAME", name, + "DIRECTION", direction); + } + + private long selectMetricDirection(String metricName) { + return (long) db.selectFirst("SELECT direction FROM metrics WHERE name = '" + metricName + "'").get("DIRECTION"); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest/metrics.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest/metrics.sql new file mode 100644 index 00000000000..ced0a62e6d2 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v76/FixDirectionOfMetricsTest/metrics.sql @@ -0,0 +1,19 @@ +CREATE TABLE "METRICS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(64) NOT NULL, + "DESCRIPTION" VARCHAR(255), + "DIRECTION" INTEGER NOT NULL DEFAULT 0, + "DOMAIN" VARCHAR(64), + "SHORT_NAME" VARCHAR(64), + "QUALITATIVE" BOOLEAN NOT NULL DEFAULT FALSE, + "VAL_TYPE" VARCHAR(8), + "USER_MANAGED" BOOLEAN DEFAULT FALSE, + "ENABLED" BOOLEAN DEFAULT TRUE, + "WORST_VALUE" DOUBLE, + "BEST_VALUE" DOUBLE, + "OPTIMIZED_BEST_VALUE" BOOLEAN, + "HIDDEN" BOOLEAN, + "DELETE_HISTORICAL_DATA" BOOLEAN, + "DECIMAL_SCALE" INTEGER +); +CREATE UNIQUE INDEX "METRICS_UNIQUE_NAME" ON "METRICS" ("NAME"); |