+/*
+ * 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;
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;
.doesNotThrowAnyException();
}
-
@Test
void log_the_item_uuid_when_the_migration_fails() {
String nclocMetricUuid = insertMetric("ncloc", "INT");
.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)
"need_issue_sync", false,
"is_main", true,
"created_at", 12L,
- "updated_at", 12L
- );
+ "updated_at", 12L);
}
-
}
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;
.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)
"selection_mode", "MANUAL",
MEASURES_MIGRATED_COLUMN, migrated,
"created_at", 12L,
- "updated_at", 12L
- );
+ "updated_at", 12L);
}
}
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;
}