]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8534 delete ACTIVITY rows which don't have profileKey field
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 13 Dec 2016 08:58:18 +0000 (09:58 +0100)
committerGitHub <noreply@github.com>
Tue, 13 Dec 2016 08:58:18 +0000 (09:58 +0100)
Signed-off-by: Simon Brandhof <simon.brandhof@sonarsource.com>
sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java
sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java

index fa9f0877115d47b8e7188e1504d23117395dfca1..345703f26572352ce442aec4a5e4bb31a42e52e1 100644 (file)
@@ -29,8 +29,7 @@ import org.sonar.db.version.MassUpdate;
 import org.sonar.db.version.Select;
 import org.sonar.db.version.SqlStatement;
 
-import static com.google.common.base.Preconditions.checkState;
-import static org.apache.commons.lang.StringUtils.isNotBlank;
+import static org.apache.commons.lang.StringUtils.isBlank;
 
 public class PopulateProfileKeyOfActivities extends BaseDataChange {
 
@@ -45,6 +44,11 @@ public class PopulateProfileKeyOfActivities extends BaseDataChange {
     massUpdate.update("update activities set profile_key=?, data_field=? where id=?");
     massUpdate.rowPluralName("activities");
     massUpdate.execute(PopulateProfileKeyOfActivities::handle);
+
+    // SONAR-8534 delete orphans
+    context.prepareUpsert("delete from activities where profile_key is null")
+      .execute()
+      .commit();
   }
 
   private static boolean handle(Select.Row row, SqlStatement update) throws SQLException {
@@ -52,12 +56,12 @@ public class PopulateProfileKeyOfActivities extends BaseDataChange {
     String data = row.getString(2);
     Map<String, String> fields = KeyValueFormat.parse(data);
     String profileKey = fields.remove("profileKey");
-    checkState(isNotBlank(profileKey), "No profile key found in db row of activities.data_field", id);
-
+    if (isBlank(profileKey)) {
+      return false;
+    }
     update.setString(1, profileKey);
     update.setString(2, KeyValueFormat.format(fields));
     update.setInt(3, id);
-
     return true;
   }
 }
index 4bad146967126f45f2fff0176721604ce82c633d..701cae6ff43e989de166ae5bfb91089968e580aa 100644 (file)
@@ -72,13 +72,30 @@ public class PopulateProfileKeyOfActivitiesTest {
   }
 
   @Test
-  public void fail_if_activity_data_is_not_well_formatted() throws SQLException {
-    expectedException.expect(IllegalStateException.class);
-    expectedException.expectMessage("Error during processing of row: ");
-
+  public void delete_the_rows_of_ACTIVITIES_that_do_not_have_profileKey() throws SQLException {
     db.executeInsert(ACTIVITIES_TABLE, "data_field", "key=fakeKey");
 
     underTest.execute();
+
+    assertThat(db.countRowsOfTable(ACTIVITIES_TABLE)).isEqualTo(0);
+  }
+
+  @Test
+  public void delete_the_rows_of_ACTIVITIES_that_have_empty_profileKey() throws SQLException {
+    insertActivity("");
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(ACTIVITIES_TABLE)).isEqualTo(0);
+  }
+
+  @Test
+  public void delete_the_rows_of_ACTIVITIES_that_have_blank_profileKey() throws SQLException {
+    insertActivity("    ");
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(ACTIVITIES_TABLE)).isEqualTo(0);
   }
 
   private void assertCountActivitiesWithProfile(String profileKey, int expectedNumberOfActivities) {