From cf6d8c9713c5c95303902918fa84fdadd3745f29 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Mon, 28 Oct 2024 10:37:34 +0100 Subject: [PATCH] SONAR-23299 Remove rating measures introduced in 10.7 during data migration --- ...areQualityRatingFromProjectMeasuresIT.java | 19 ++++++++++ ...grateBranchesLiveMeasuresToMeasuresIT.java | 35 ++++++++++++++++--- ...atePortfoliosLiveMeasuresToMeasuresIT.java | 29 +++++++++++++-- ...AbstractMigrateLiveMeasuresToMeasures.java | 7 +++- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasuresIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasuresIT.java index 9ec43b5e6da..1bc0c0be4c7 100644 --- a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasuresIT.java +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasuresIT.java @@ -1,3 +1,22 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v108; import java.sql.SQLException; diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java index 025bcfa74a9..a3ff63478f3 100644 --- a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java @@ -24,6 +24,8 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.event.Level; @@ -74,7 +76,6 @@ class MigrateBranchesLiveMeasuresToMeasuresIT { .doesNotThrowAnyException(); } - @Test void log_the_item_uuid_when_the_migration_fails() { String nclocMetricUuid = insertMetric("ncloc", "INT"); @@ -161,6 +162,34 @@ class MigrateBranchesLiveMeasuresToMeasuresIT { .containsOnly(tuple(component2, branch1, "{\"metric_with_data\":\"some data\"}", -4524184678167636687L)); } + @Test + void should_not_migrate_measures_planned_for_deletion() throws SQLException { + String nclocMetricUuid = insertMetric("ncloc", "INT"); + Set deletedMetricUuid = DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.stream().map(e -> insertMetric(e, "INT")) + .collect(Collectors.toSet()); + + String branch1 = "branch_4"; + insertNotMigratedBranch(branch1); + String component1 = uuidFactory.create(); + String component2 = uuidFactory.create(); + insertMeasure(branch1, component1, nclocMetricUuid, Map.of("value", 120)); + deletedMetricUuid.forEach(metricUuid -> insertMeasure(branch1, component1, metricUuid, Map.of("value", 120))); + deletedMetricUuid.forEach(metricUuid -> insertMeasure(branch1, component2, metricUuid, Map.of("value", 120))); + + underTest.execute(); + + assertBranchMigrated(branch1); + assertThat(db.countRowsOfTable("measures")).isEqualTo(1); + + assertThat(db.select(format(SELECT_MEASURE, component1))) + .hasSize(1) + .extracting(t -> t.get("component_uuid"), t -> t.get("branch_uuid"), t -> t.get("json_value"), t -> t.get("json_value_hash")) + .containsOnly(tuple(component1, branch1, "{\"ncloc\":120.0}", -1557106439558598045L)); + + assertThat(db.select(format(SELECT_MEASURE, component2))) + .isEmpty(); + } + private void assertBranchMigrated(String branch) { List> result = db.select(format("select %s as \"MIGRATED\" from project_branches where uuid = '%s'", MEASURES_MIGRATED_COLUMN, branch)); assertThat(result) @@ -212,9 +241,7 @@ class MigrateBranchesLiveMeasuresToMeasuresIT { "need_issue_sync", false, "is_main", true, "created_at", 12L, - "updated_at", 12L - ); + "updated_at", 12L); } - } diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java index 94d2e0e0701..8cb720943d8 100644 --- a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java @@ -24,6 +24,8 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.event.Level; @@ -160,6 +162,30 @@ class MigratePortfoliosLiveMeasuresToMeasuresIT { .containsOnly(tuple(component2, portfolio1, "{\"metric_with_data\":\"some data\"}", -4524184678167636687L)); } + @Test + void should_not_migrate_measures_planned_for_deletion() throws SQLException { + String nclocMetricUuid = insertMetric("ncloc", "INT"); + Set deletedMetricUuid = DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.stream().map(e -> insertMetric(e, "INT")) + .collect(Collectors.toSet()); + + String portfolio1 = "portfolio_4"; + insertNotMigratedPortfolio(portfolio1); + String component1 = uuidFactory.create(); + String component2 = uuidFactory.create(); + insertMeasure(portfolio1, component1, nclocMetricUuid, Map.of("value", 120)); + deletedMetricUuid.forEach(metricUuid -> insertMeasure(portfolio1, component1, metricUuid, Map.of("value", 120))); + deletedMetricUuid.forEach(metricUuid -> insertMeasure(portfolio1, component2, metricUuid, Map.of("value", 120))); + + underTest.execute(); + + assertPortfolioMigrated(portfolio1); + + assertThat(db.select(format(SELECT_MEASURE, component1))) + .hasSize(1) + .extracting(t -> t.get("component_uuid"), t -> t.get("branch_uuid"), t -> t.get("json_value"), t -> t.get("json_value_hash")) + .containsOnly(tuple(component1, portfolio1, "{\"ncloc\":120.0}", -1557106439558598045L)); + } + private void assertPortfolioMigrated(String portfolio) { List> result = db.select(format("select %s as \"MIGRATED\" from portfolios where uuid = '%s'", MEASURES_MIGRATED_COLUMN, portfolio)); assertThat(result) @@ -211,7 +237,6 @@ class MigratePortfoliosLiveMeasuresToMeasuresIT { "selection_mode", "MANUAL", MEASURES_MIGRATED_COLUMN, migrated, "created_at", 12L, - "updated_at", 12L - ); + "updated_at", 12L); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java index 58f79a98af0..5ca1074bd78 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java @@ -206,11 +206,16 @@ public abstract class AbstractMigrateLiveMeasuresToMeasures extends DataChange { byte[] data = row.getBytes(6); Object metricValue = getMetricValue(data, textValue, valueType, numericValue); - if (metricValue != null) { + if (metricValue != null + && !measuresPlannedForDeletion(metricName)) { measureValues.put(metricName, metricValue); } } + private static boolean measuresPlannedForDeletion(String metricName) { + return DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.contains(metricName); + } + private static Object getMetricValue(@Nullable byte[] data, @Nullable String textValue, String valueType, Double numericValue) { return TEXT_VALUE_TYPES.contains(valueType) ? getTextValue(data, textValue) : numericValue; } -- 2.39.5