aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-13 09:58:18 +0100
committerGitHub <noreply@github.com>2016-12-13 09:58:18 +0100
commitfb9f4f8314cc44d45aef2f41a82520d969080444 (patch)
tree9c6986ac840e82a7ace298feca6aff36009d79e9 /sonar-db/src
parent457ef6b42ba71ddc8ce44c24b49249edab5779e5 (diff)
downloadsonarqube-fb9f4f8314cc44d45aef2f41a82520d969080444.tar.gz
sonarqube-fb9f4f8314cc44d45aef2f41a82520d969080444.zip
SONAR-8534 delete ACTIVITY rows which don't have profileKey field
Signed-off-by: Simon Brandhof <simon.brandhof@sonarsource.com>
Diffstat (limited to 'sonar-db/src')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java14
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java25
2 files changed, 30 insertions, 9 deletions
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<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;
}
}
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) {