--- /dev/null
+package org.sonar.server.platform.db.migration.version.v108;
+
+import java.sql.SQLException;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.core.util.UuidFactoryImpl;
+import org.sonar.db.MigrationDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class DeleteSoftwareQualityRatingFromProjectMeasuresIT {
+
+ @RegisterExtension
+ public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(DeleteSoftwareQualityRatingFromProjectMeasures.class);
+
+ private final DeleteSoftwareQualityRatingFromProjectMeasures underTest = new DeleteSoftwareQualityRatingFromProjectMeasures(db.database());
+
+ @Test
+ void execute_whenMeasuresExists_shouldDeleteMeasures() throws SQLException {
+ DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.forEach(key -> {
+ String metricUUid = insertMetric(key);
+ insertProjectMeasure(metricUUid);
+ });
+
+ assertThat(db.countSql("select count(1) from project_measures;"))
+ .isEqualTo(DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.size());
+
+ underTest.execute();
+
+ assertThat(db.countSql("select count(1) from project_measures;")).isZero();
+ }
+
+ @Test
+ void execute_whenOtherMeasuresExists_shouldNotDeleteMeasures() throws SQLException {
+ String metricUUid = insertMetric("other_metric");
+ insertProjectMeasure(metricUUid);
+
+ assertThat(db.countSql("select count(1) from project_measures;")).isEqualTo(1);
+
+ underTest.execute();
+
+ assertThat(db.countSql("select count(1) from project_measures;")).isEqualTo(1);
+ }
+
+ @Test
+ void execute_shouldBeReentrant() throws SQLException {
+ DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.forEach(key -> {
+ String metricUUid = insertMetric(key);
+ insertProjectMeasure(metricUUid);
+ });
+
+ String metricUUid = insertMetric("other_metric");
+ insertProjectMeasure(metricUUid);
+
+ assertThat(db.countSql("select count(1) from project_measures;"))
+ .isEqualTo(DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.size() + 1);
+
+ underTest.execute();
+ underTest.execute();
+
+ assertThat(db.countSql("select count(1) from project_measures;")).isOne();
+ }
+
+ private String insertMetric(String key) {
+ String uuid = UuidFactoryImpl.INSTANCE.create();
+ Map<String, Object> map = Map.ofEntries(
+ Map.entry("UUID", uuid),
+ Map.entry("NAME", key));
+ db.executeInsert("metrics", map);
+ return uuid;
+ }
+
+ private void insertProjectMeasure(String metricUuid) {
+ Map<String, Object> map = Map.ofEntries(
+ Map.entry("UUID", UuidFactoryImpl.INSTANCE.create()),
+ Map.entry("METRIC_UUID", metricUuid),
+ Map.entry("COMPONENT_UUID", UuidFactoryImpl.INSTANCE.create()),
+ Map.entry("ANALYSIS_UUID", UuidFactoryImpl.INSTANCE.create()));
+ db.executeInsert("project_measures", map);
+ }
+}
--- /dev/null
+/*
+ * 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.Set;
+import java.util.stream.Collectors;
+import org.sonar.core.metric.SoftwareQualitiesMetrics;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+
+public class DeleteSoftwareQualityRatingFromProjectMeasures extends DataChange {
+
+ public static final Set<String> SOFTWARE_QUALITY_METRICS_TO_DELETE = Set.of(
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_MAINTAINABILITY_RATING_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_MAINTAINABILITY_RATING_KEY,
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_RELIABILITY_RATING_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_RELIABILITY_RATING_KEY,
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_RATING_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_SECURITY_RATING_KEY,
+ SoftwareQualitiesMetrics.EFFORT_TO_REACH_SOFTWARE_QUALITY_MAINTAINABILITY_RATING_A_KEY,
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY,
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY,
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY,
+ SoftwareQualitiesMetrics.SOFTWARE_QUALITY_MAINTAINABILITY_DEBT_RATIO_KEY,
+ SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_MAINTAINABILITY_DEBT_RATIO_KEY);
+
+ private static final String SELECT_QUERY = """
+ select pm.uuid from project_measures pm
+ inner join metrics m on pm.metric_uuid = m.uuid
+ where m.name in (%s)
+ """.formatted(SOFTWARE_QUALITY_METRICS_TO_DELETE.stream().map(s -> "'" + s + "'").collect(Collectors.joining(",")));
+
+ public DeleteSoftwareQualityRatingFromProjectMeasures(Database db) {
+ super(db);
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select(SELECT_QUERY);
+ massUpdate.update("delete from project_measures where uuid = ?");
+
+ massUpdate.execute((row, update, index) -> {
+ update.setString(1, row.getString(1));
+ return true;
+ });
+ }
+}