]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23299 Remove rating measures introduced in 10.7 during data migration
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Mon, 28 Oct 2024 09:37:34 +0000 (10:37 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 5 Nov 2024 20:03:01 +0000 (20:03 +0000)
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasuresIT.java
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java

index 9ec43b5e6dae41bd3df390adacabf1ac280ddd33..1bc0c0be4c732c0bd42d78e292b0e77b6552add4 100644 (file)
@@ -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;
index 025bcfa74a9f714006ad57298f41be60aba4f3f3..a3ff63478f3ec2c0cf758949f05cab2fe3886709 100644 (file)
@@ -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<String> 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<Map<String, Object>> 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);
   }
 
-
 }
index 94d2e0e07012886a7714b2f8cf31d8d875567dee..8cb720943d82fbe9ba7c18f9336da07a3373199c 100644 (file)
@@ -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<String> 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<Map<String, Object>> 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);
   }
 }
index 58f79a98af04b20efe59f05f322c510997b2a4d4..5ca1074bd787f043f39e35b83671fef963f3061e 100644 (file)
@@ -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;
   }