From: Simon Brandhof Date: Tue, 13 Dec 2016 08:58:18 +0000 (+0100) Subject: SONAR-8534 delete ACTIVITY rows which don't have profileKey field X-Git-Tag: 6.3-RC1~793 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fb9f4f8314cc44d45aef2f41a82520d969080444;p=sonarqube.git SONAR-8534 delete ACTIVITY rows which don't have profileKey field Signed-off-by: Simon Brandhof --- diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java index fa9f0877115..345703f2657 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java @@ -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 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; } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java index 4bad1469671..701cae6ff43 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java @@ -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) {