]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12962 modify migration to delete `security_review_rating_effort` measures
authorJacek <jacek.poreda@sonarsource.com>
Fri, 14 Feb 2020 11:31:09 +0000 (12:31 +0100)
committerSonarTech <sonartech@sonarsource.com>
Wed, 19 Feb 2020 19:46:11 +0000 (20:46 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DbVersion82.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasures.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v82/DeleteSecurityReviewRatingMeasuresTest.java

index e55f28070cffe40bfcc75c96382fd744c5ad09f6..f14b923bf22d720def2f5d43271d1298837cf857 100644 (file)
@@ -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);
   }
 }
index 798da563fe42105de00f9a7d8425f4efbc1ca44a..4701bd7c254c30c2ce96bcf2f7b7df1cf056233e 100644 (file)
@@ -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;
     });
   }
index 3be55d29ac9e58ec3b7d4067822ec005fae71d24..162cac3e402c5a799f79546be9eccea639f50b04 100644 (file)
@@ -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);