From a94f2aed100dfa59cf3964c0d1f13f000a967fb3 Mon Sep 17 00:00:00 2001 From: Jacek Date: Fri, 14 Feb 2020 12:31:09 +0100 Subject: [PATCH] SONAR-12962 modify migration to delete `security_review_rating_effort` measures --- .../db/migration/version/v82/DbVersion82.java | 2 +- .../DeleteSecurityReviewRatingMeasures.java | 46 +++-- ...eleteSecurityReviewRatingMeasuresTest.java | 178 +++++++++++++++--- 3 files changed, 184 insertions(+), 42 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DbVersion82.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DbVersion82.java index e55f28070cf..f14b923bf22 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DbVersion82.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DbVersion82.java @@ -34,6 +34,6 @@ public class DbVersion82 implements DbVersion { .add(3205, "Add PROJECTS table", CreateProjectsTable.class) .add(3206, "Populate PROJECTS table", PopulateProjectsTable.class) .add(3207, "Drop 'TAGS' column from COMPONENTS table", DropTagsColumnFromComponentsTable.class) - .add(3208, "Remove old Security Review Rating measures", DeleteSecurityReviewRatingMeasures.class); + .add(3209, "Remove old Security Review Rating measures", DeleteSecurityReviewRatingMeasures.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasures.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasures.java index 798da563fe4..4701bd7c254 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasures.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasures.java @@ -28,6 +28,7 @@ import org.sonar.server.platform.db.migration.step.MassUpdate; public class DeleteSecurityReviewRatingMeasures extends DataChange { private static final String SECURITY_REVIEW_RATING_METRIC_KEY = "security_review_rating"; + private static final String SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY = "security_review_rating_effort"; private static final String SELECT_COMPONENTS_STATEMENT = "select c.uuid from components c where c.scope in ('PRJ') and c.qualifier in ('VW', 'SVW', 'APP', 'TRK')"; public DeleteSecurityReviewRatingMeasures(Database db) { @@ -36,42 +37,59 @@ public class DeleteSecurityReviewRatingMeasures extends DataChange { @Override protected void execute(Context context) throws SQLException { - Integer metricId = getSecurityReviewRatingMetricId(context); - if (metricId != null) { - deleteFromProjectMeasures(context, metricId); - deleteFromLiveMeasures(context, metricId); + Integer reviewRatingId = getMetricId(context, SECURITY_REVIEW_RATING_METRIC_KEY); + Integer reviewRatingEffortId = getMetricId(context, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY); + if (reviewRatingId != null) { + deleteFromProjectMeasures(context, reviewRatingId, reviewRatingEffortId); + deleteFromLiveMeasures(context, reviewRatingId, reviewRatingEffortId); } } @Nullable - private static Integer getSecurityReviewRatingMetricId(Context context) throws SQLException { + private static Integer getMetricId(Context context, String metricName) throws SQLException { return context.prepareSelect("select id from metrics where name = ?") - .setString(1, SECURITY_REVIEW_RATING_METRIC_KEY) + .setString(1, metricName) .get(row -> row.getNullableInt(1)); } - private static void deleteFromLiveMeasures(Context context, Integer metricId) throws SQLException { + private static void deleteFromLiveMeasures(Context context, Integer reviewRatingId, @Nullable Integer reviewRatingEffortId) throws SQLException { MassUpdate deleteFromLiveMeasures = context.prepareMassUpdate(); deleteFromLiveMeasures.select(SELECT_COMPONENTS_STATEMENT); - deleteFromLiveMeasures.update("delete from live_measures where project_uuid = ? and metric_id = ?"); + if (reviewRatingEffortId != null) { + deleteFromLiveMeasures.update("delete from live_measures where project_uuid = ? and metric_id in (?, ?)"); + } else { + deleteFromLiveMeasures.update("delete from live_measures where project_uuid = ? and metric_id = ?"); + } deleteFromLiveMeasures.execute((row, update) -> { - update.setString(1, row.getString(1)); - update.setInt(2, metricId); + String projectUuid = row.getString(1); + update.setString(1, projectUuid) + .setInt(2, reviewRatingId); + if (reviewRatingEffortId != null) { + update.setInt(3, reviewRatingEffortId); + } return true; }); } - private static void deleteFromProjectMeasures(Context context, Integer metricId) throws SQLException { + private static void deleteFromProjectMeasures(Context context, Integer reviewRatingId, @Nullable Integer reviewRatingEffortId) throws SQLException { MassUpdate deleteFromProjectMeasures = context.prepareMassUpdate(); deleteFromProjectMeasures.select(SELECT_COMPONENTS_STATEMENT); - deleteFromProjectMeasures.update("delete from project_measures where component_uuid = ? and metric_id = ?"); + if (reviewRatingEffortId != null) { + deleteFromProjectMeasures.update("delete from project_measures where component_uuid = ? and metric_id in (?, ?)"); + } else { + deleteFromProjectMeasures.update("delete from project_measures where component_uuid = ? and metric_id = ?"); + } deleteFromProjectMeasures.execute((row, update) -> { - update.setString(1, row.getString(1)); - update.setInt(2, metricId); + String componentUuid = row.getString(1); + update.setString(1, componentUuid) + .setInt(2, reviewRatingId); + if (reviewRatingEffortId != null) { + update.setInt(3, reviewRatingEffortId); + } return true; }); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasuresTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasuresTest.java index 3be55d29ac9..162cac3e402 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasuresTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasuresTest.java @@ -24,6 +24,7 @@ import java.util.Random; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.db.CoreDbTester; @@ -38,22 +39,29 @@ public class DeleteSecurityReviewRatingMeasuresTest { private static final int SECURITY_REVIEW_RATING_METRIC_ID = 200; private static final String SECURITY_REVIEW_RATING_METRIC_KEY = "security_review_rating"; + private static final int SECURITY_REVIEW_RATING_EFFORT_METRIC_ID = 201; + private static final String SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY = "security_review_rating_effort"; - private static final Random RANDOM = new Random(); + private static final int OTHER_METRIC_ID_1 = 1; + private static final int OTHER_METRIC_ID_2 = 2; + private static final int OTHER_METRIC_MEASURES_COUNT = 20; @Rule public CoreDbTester db = CoreDbTester.createForSchema(DeleteSecurityReviewRatingMeasuresTest.class, "schema.sql"); private DataChange underTest = new DeleteSecurityReviewRatingMeasures(db.database()); - @Test - public void should_not_fail_if_metric_not_defined() throws SQLException { - insertMetric(1, "another metric#1"); - insertMetric(2, "another metric#2"); + @Before + public void before() { + insertMetric(OTHER_METRIC_ID_1, "another metric#1"); + insertMetric(OTHER_METRIC_ID_2, "another metric#2"); + } + @Test + public void not_fail_if_metrics_not_defined() throws SQLException { String projectUuid = insertComponent("PRJ", "TRK"); - insertMeasure(4, SECURITY_REVIEW_RATING_METRIC_ID, projectUuid); - insertLiveMeasure("uuid-4", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, projectUuid); + insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, projectUuid); + insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, projectUuid); underTest.execute(); @@ -62,10 +70,119 @@ public class DeleteSecurityReviewRatingMeasuresTest { } @Test - public void should_remove_security_rating_review_from_measures_and_live_measures() throws SQLException { + public void not_fail_if_security_review_rating_effort_metric_not_found() throws SQLException { insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY); - insertMetric(1, "another metric#1"); - insertMetric(2, "another metric#2"); + + String applicationUuid = insertComponent("PRJ", "TRK"); + + insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid); + insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid); + + generateOtherMetricMeasures(2, applicationUuid); + generateOtherMetricsLiveMeasures(applicationUuid); + + underTest.execute(); + + assertSecurityReviewRatingMeasuresDeleted(); + assertSecurityReviewRatingLiveMeasuresDeleted(); + + assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + + // should not fail if called twice + underTest.execute(); + } + + @Test + public void remove_security_rating_review_from_measures_and_live_measures_projects() throws SQLException { + insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY); + insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY); + + String applicationUuid = insertComponent("PRJ", "TRK"); + + insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid); + insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid); + + generateOtherMetricMeasures(2, applicationUuid); + generateOtherMetricsLiveMeasures(applicationUuid); + + underTest.execute(); + + assertSecurityReviewRatingMeasuresDeleted(); + assertSecurityReviewRatingLiveMeasuresDeleted(); + + assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + + // should not fail if called twice + underTest.execute(); + } + + @Test + public void remove_security_rating_review_from_measures_and_live_measures_for_portfolios() throws SQLException { + insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY); + insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY); + + String portfolioUuid = insertComponent("PRJ", "VW"); + String subPortfolioUuid = insertComponent("PRJ", "SVW"); + + insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid); + insertMeasure(2, SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid); + + insertMeasure(3, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid); + insertMeasure(4, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid); + + insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid, portfolioUuid); + insertLiveMeasure("uuid-2", SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid, subPortfolioUuid); + + insertLiveMeasure("uuid-3", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid, portfolioUuid); + insertLiveMeasure("uuid-4", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid, subPortfolioUuid); + + + generateOtherMetricMeasures(5, portfolioUuid); + generateOtherMetricsLiveMeasures(portfolioUuid); + + underTest.execute(); + + assertSecurityReviewRatingMeasuresDeleted(); + assertSecurityReviewRatingLiveMeasuresDeleted(); + + assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + + // should not fail if called twice + underTest.execute(); + } + + @Test + public void remove_security_rating_review_from_measures_and_live_measures_applications() throws SQLException { + insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY); + insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY); + + String applicationUuid = insertComponent("PRJ", "APP"); + + insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid); + insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid); + + generateOtherMetricMeasures(2, applicationUuid); + generateOtherMetricsLiveMeasures(applicationUuid); + + underTest.execute(); + + assertSecurityReviewRatingMeasuresDeleted(); + assertSecurityReviewRatingLiveMeasuresDeleted(); + + assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + + // should not fail if called twice + underTest.execute(); + } + + @Test + public void remove_security_rating_review_from_measures_and_live_measures_mixed() throws SQLException { + insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY); + insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY); String portfolioUuid = insertComponent("PRJ", "VW"); String subPortfolioUuid = insertComponent("PRJ", "SVW"); @@ -77,12 +194,8 @@ public class DeleteSecurityReviewRatingMeasuresTest { insertMeasure(3, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid); insertMeasure(4, SECURITY_REVIEW_RATING_METRIC_ID, projectUuid); - // other random metrics - int totalOtherMeasures = IntStream.range(5, 10 + RANDOM.nextInt(10)) - .peek(i -> insertMeasure(i, RANDOM.nextInt(100), projectUuid)) - .boxed() - .collect(Collectors.toList()) - .size(); + insertMeasure(5, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid); + insertMeasure(6, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid); insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid, portfolioUuid); insertLiveMeasure("uuid-2", SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid, subPortfolioUuid); @@ -91,30 +204,27 @@ public class DeleteSecurityReviewRatingMeasuresTest { insertLiveMeasure("uuid-5", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, getRandomUuid()); insertLiveMeasure("uuid-6", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, getRandomUuid()); - // other random metrics - long totalOtherLiveMeasures = IntStream.range(0, 10 + RANDOM.nextInt(10)) - .peek(i -> insertLiveMeasure("uuid-other-" + i, RANDOM.nextInt(100), projectUuid, getRandomUuid())) - .boxed() - .collect(Collectors.toList()) - .size(); + insertLiveMeasure("uuid-7", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid, portfolioUuid); + insertLiveMeasure("uuid-8", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid, subPortfolioUuid); + + generateOtherMetricMeasures(7, projectUuid); + generateOtherMetricsLiveMeasures(projectUuid); underTest.execute(); assertSecurityReviewRatingMeasuresDeleted(); assertSecurityReviewRatingLiveMeasuresDeleted(); - assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(totalOtherMeasures); - assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(totalOtherLiveMeasures); + assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); + assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT); // should not fail if called twice underTest.execute(); } @Test - public void should_not_fail_if_empty_tables() throws SQLException { + public void not_fail_if_empty_tables() throws SQLException { insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY); - insertMetric(1, "another metric#1"); - insertMetric(2, "another metric#2"); underTest.execute(); @@ -122,6 +232,20 @@ public class DeleteSecurityReviewRatingMeasuresTest { assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(0); } + private void generateOtherMetricsLiveMeasures(String componentUuid) { + IntStream.range(0, OTHER_METRIC_MEASURES_COUNT) + .peek(i -> insertLiveMeasure("uuid-other-" + i, i, componentUuid, getRandomUuid())) + .boxed() + .collect(Collectors.toList()); + } + + private void generateOtherMetricMeasures(int startId, String componentUuid) { + IntStream.range(startId, startId + OTHER_METRIC_MEASURES_COUNT) + .peek(i -> insertMeasure(i, new Random().nextBoolean() ? OTHER_METRIC_ID_1 : OTHER_METRIC_ID_2, componentUuid)) + .boxed() + .collect(Collectors.toList()); + } + private void assertSecurityReviewRatingLiveMeasuresDeleted() { assertThat(db.countSql("select count(uuid) from LIVE_MEASURES where metric_id = " + SECURITY_REVIEW_RATING_METRIC_ID)) .isEqualTo(0); -- 2.39.5