]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23299 Remove historical data from software quality rating based measures
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Fri, 25 Oct 2024 16:08:14 +0000 (18:08 +0200)
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 [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasures.java [new file with mode: 0644]

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
new file mode 100644 (file)
index 0000000..9ec43b5
--- /dev/null
@@ -0,0 +1,82 @@
+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);
+  }
+}
index a766c70ab7e2318a0834588a2bee6fafa84b9740..04f4dd3cc633409df78c5435739b080f4bb91ab2 100644 (file)
@@ -58,7 +58,8 @@ public class DbVersion108 implements DbVersion {
       .add(10_8_015, "Add column 'impacts' in 'active_rules' table", AddImpactsColumnInActiveRulesTable.class)
       .add(10_8_016, "Create 'project_dependencies' table", CreateProjectDependenciesTable.class)
       .add(10_8_017, "Enable specific MQR mode", EnableSpecificMqrMode.class)
-      .add(10_8_018, "Make columns 'published_at' and 'last_modified_at' nullable on the 'cves' table", AlterCveColumnsToNullable.class);
+      .add(10_8_018, "Make columns 'published_at' and 'last_modified_at' nullable on the 'cves' table", AlterCveColumnsToNullable.class)
+      .add(10_8_019, "Delete Software Quality ratings from project_measures", DeleteSoftwareQualityRatingFromProjectMeasures.class);
   }
 
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasures.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasures.java
new file mode 100644 (file)
index 0000000..92dfcaf
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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;
+    });
+  }
+}