diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2024-10-25 18:08:14 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-11-05 20:03:01 +0000 |
commit | 2aba1d50b601aee3e70059808d571229ef57053b (patch) | |
tree | 315d50f3845661c428f2c7cf95d18b4ededee910 /server/sonar-db-migration | |
parent | cdfc361c1253726fca1a1d9ac561a5c68126476a (diff) | |
download | sonarqube-2aba1d50b601aee3e70059808d571229ef57053b.tar.gz sonarqube-2aba1d50b601aee3e70059808d571229ef57053b.zip |
SONAR-23299 Remove historical data from software quality rating based measures
Diffstat (limited to 'server/sonar-db-migration')
3 files changed, 154 insertions, 1 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 new file mode 100644 index 00000000000..9ec43b5e6da --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasuresIT.java @@ -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); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java index a766c70ab7e..04f4dd3cc63 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java @@ -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 index 00000000000..92dfcaff2e8 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DeleteSoftwareQualityRatingFromProjectMeasures.java @@ -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; + }); + } +} |