]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8746 Remove measures related to periods 2 to 5 1671/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 15 Feb 2017 09:22:38 +0000 (10:22 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 15 Feb 2017 11:08:19 +0000 (12:08 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValue.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValueTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValueTest/project_measures.sql [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql

index 39cac92ce80d13ce71cf372afe60f6ca3c478392..c2815a97bcf34543af2822f03c3a8cb112f9542e 100644 (file)
@@ -48,6 +48,8 @@ public class DbVersion63 implements DbVersion {
       .add(1513, "Make default organization guarded", MakeDefaultOrganizationGuarded.class)
       .add(1514, "Delete some entries in PROPERTIES", DeleteUselessProperties.class)
       .add(1515, "Unset user root flags", UnsetUserRootFlags.class)
-      .add(1516, "Add ORGANIZATIONS.USER_ID", AddUserIdToOrganizations.class);
+      .add(1516, "Add ORGANIZATIONS.USER_ID", AddUserIdToOrganizations.class)
+      .add(1517, "Delete PROJECT_MEASURES rows having no value", DeleteMeasuresHavingNoValue.class)
+    ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValue.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValue.java
new file mode 100644 (file)
index 0000000..e2aa14b
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v63;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+
+/**
+ * Delete measures having no value in order to remove measures related to periods 2 to 5.
+ * <br/>
+ * Here are some example of measures to remove :
+ * <ul>
+ * <li>VALUE=[null], TEXT_VALUE=[null], MEASURE_DATA=[null], VARIATION_VALUE_1=[null], VARIATION_VALUE_2=[null], VARIATION_VALUE_3=[null]</li>
+ * <li>VALUE=[null], TEXT_VALUE=[null], MEASURE_DATA=[null], VARIATION_VALUE_1=[null], VARIATION_VALUE_2=20, VARIATION_VALUE_3=[null]</li>
+ * <li>VALUE=[null], TEXT_VALUE=[null], MEASURE_DATA=[null], VARIATION_VALUE_1=[null], VARIATION_VALUE_2=10, VARIATION_VALUE_3=15</li>
+ * </ul>
+ */
+public class DeleteMeasuresHavingNoValue extends DataChange {
+
+  public DeleteMeasuresHavingNoValue(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("SELECT pm.id FROM project_measures pm " +
+      "WHERE pm.value IS NULL AND pm.text_value IS NULL AND pm.variation_value_1 IS NULL AND pm.measure_data IS NULL ");
+    massUpdate.update("DELETE FROM project_measures WHERE id=?");
+    massUpdate.rowPluralName("measures");
+    massUpdate.execute((row, update) -> {
+      update.setLong(1, row.getLong(1));
+      return true;
+    });
+  }
+
+}
index 8d6fbfc49ddda405dc5f6e065491e132f683c7e2..e9c26d7fefad69b4342849d9363a341ec595644d 100644 (file)
@@ -41,7 +41,7 @@ public class DbVersion63Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 17);
+    verifyMigrationCount(underTest, 18);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValueTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValueTest.java
new file mode 100644 (file)
index 0000000..b65234b
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v63;
+
+import com.google.common.collect.ImmutableMap;
+import java.nio.charset.StandardCharsets;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static java.lang.String.valueOf;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DeleteMeasuresHavingNoValueTest {
+
+  private static final String TABLE_MEASURES = "project_measures";
+
+  @Rule
+  public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteMeasuresHavingNoValueTest.class, "project_measures.sql");
+
+  private DeleteMeasuresHavingNoValue underTest = new DeleteMeasuresHavingNoValue(db.database());
+
+  @Test
+  public void migration_has_no_effect_on_empty_tables() throws SQLException {
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isZero();
+  }
+
+  @Test
+  public void migration_does_not_remove_measures_with_value() throws SQLException {
+    insertMeasure(5d, null, null, null);
+    insertMeasure(null, "text", null, null);
+    insertMeasure(null, null, "data", null);
+    insertMeasure(null, null, null, 50d);
+    db.commit();
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(4);
+  }
+
+  @Test
+  public void migration_removes_measures_with_no_values() throws SQLException {
+    insertMeasure(null, null, null, null);
+    insertMeasure(null, null, null, null);
+    insertMeasure(null, null, null, null);
+    db.commit();
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isZero();
+  }
+
+  @Test
+  public void migration_does_not_remove_measures_having_variation_on_leak_period() throws SQLException {
+    insertMeasureOnlyOnVariations(10d, null, null, null, null);
+    insertMeasureOnlyOnVariations(11d, 2d, null, null, null);
+    insertMeasureOnlyOnVariations(12d, null, 3d, 4d, 5d);
+    insertMeasureOnlyOnVariations(12d, 2d, 3d, 4d, 5d);
+    db.commit();
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(4);
+  }
+
+  @Test
+  public void migration_removes_measures_having_only_variation_on_periods2_to_5() throws SQLException {
+    insertMeasureOnlyOnVariations(null, 2d, null, null, null);
+    insertMeasureOnlyOnVariations(null, null, 3d, null, null);
+    insertMeasureOnlyOnVariations(null, null, null, 4d, null);
+    insertMeasureOnlyOnVariations(null, null, null, null, 5d);
+    insertMeasureOnlyOnVariations(null, 2d, 3d, 4d, 5d);
+    db.commit();
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isZero();
+  }
+
+  @Test
+  public void migration_is_reentrant() throws SQLException {
+    insertMeasure(5d, null, null, null);
+    insertMeasure(null, "text", null, null);
+    insertMeasure(null, null, null, null);
+    insertMeasure(null, null, null, null);
+    db.commit();
+
+    underTest.execute();
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(2);
+
+    underTest.execute();
+    assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(2);
+  }
+
+  private void insertMeasure(@Nullable Double value, @Nullable String textValue, @Nullable String data, @Nullable Double variation) {
+    Map<String, Object> values = new HashMap<>(ImmutableMap.<String, Object>builder()
+      .put("METRIC_ID", valueOf(10))
+      .put("COMPONENT_UUID", randomAlphanumeric(10))
+      .put("ANALYSIS_UUID", randomAlphanumeric(10))
+      .put("VARIATION_VALUE_2", 2d)
+      .put("VARIATION_VALUE_3", 3d)
+      .put("VARIATION_VALUE_4", 4d)
+      .put("VARIATION_VALUE_5", 5d)
+      .build());
+    if (value != null) {
+      values.put("VALUE", valueOf(value));
+    }
+    if (textValue != null) {
+      values.put("TEXT_VALUE", textValue);
+    }
+    if (variation != null) {
+      values.put("VARIATION_VALUE_1", variation);
+    }
+    if (data != null) {
+      values.put("MEASURE_DATA", data.getBytes(StandardCharsets.UTF_8));
+    }
+    db.executeInsert(TABLE_MEASURES, values);
+  }
+
+  private void insertMeasureOnlyOnVariations(@Nullable Double variation1, @Nullable Double variation2, @Nullable Double variation3, @Nullable Double variation4,
+    @Nullable Double variation5) {
+    Map<String, Object> values = new HashMap<>(ImmutableMap.of(
+      "METRIC_ID", valueOf(20),
+      "COMPONENT_UUID", randomAlphanumeric(10),
+      "ANALYSIS_UUID", randomAlphanumeric(10)));
+    if (variation1 != null) {
+      values.put("VARIATION_VALUE_1", valueOf(variation1));
+    }
+    if (variation2 != null) {
+      values.put("VARIATION_VALUE_2", valueOf(variation2));
+    }
+    if (variation3 != null) {
+      values.put("VARIATION_VALUE_3", valueOf(variation3));
+    }
+    if (variation4 != null) {
+      values.put("VARIATION_VALUE_4", valueOf(variation4));
+    }
+    if (variation5 != null) {
+      values.put("VARIATION_VALUE_4", valueOf(variation5));
+    }
+    db.executeInsert(TABLE_MEASURES, values);
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValueTest/project_measures.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/DeleteMeasuresHavingNoValueTest/project_measures.sql
new file mode 100644 (file)
index 0000000..00ef5e2
--- /dev/null
@@ -0,0 +1,21 @@
+CREATE TABLE "PROJECT_MEASURES" (
+  "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "VALUE" DOUBLE,
+  "METRIC_ID" INTEGER NOT NULL,
+  "COMPONENT_UUID" VARCHAR(50) NOT NULL,
+  "ANALYSIS_UUID" VARCHAR(50) NOT NULL,
+  "TEXT_VALUE" VARCHAR(4000),
+  "ALERT_STATUS" VARCHAR(5),
+  "ALERT_TEXT" VARCHAR(4000),
+  "DESCRIPTION" VARCHAR(4000),
+  "PERSON_ID" INTEGER,
+  "VARIATION_VALUE_1" DOUBLE,
+  "VARIATION_VALUE_2" DOUBLE,
+  "VARIATION_VALUE_3" DOUBLE,
+  "VARIATION_VALUE_4" DOUBLE,
+  "VARIATION_VALUE_5" DOUBLE,
+  "MEASURE_DATA" BINARY
+);
+CREATE INDEX "MEASURES_COMPONENT_UUID" ON "PROJECT_MEASURES" ("COMPONENT_UUID");
+CREATE INDEX "MEASURES_ANALYSIS_METRIC" ON "PROJECT_MEASURES" ("ANALYSIS_UUID", "METRIC_ID");
+CREATE INDEX "MEASURES_PERSON" ON "PROJECT_MEASURES" ("PERSON_ID");
index a996126ea4462774e557862296d517a4fd6982a6..5892eee83ae6be2d1106e8e385b9b2f658b86a26 100644 (file)
@@ -530,6 +530,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1513');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1514');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1515');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1516');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1517');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', false, '1418215735482', '1418215735482');
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;