]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9881 Purge unused settings in database
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 3 Oct 2017 16:00:49 +0000 (18:00 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 4 Oct 2017 13:34:23 +0000 (15:34 +0200)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PurgeTableProperties.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PurgeTablePropertiesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PurgeTablePropertiesTest/properties.sql [new file with mode: 0644]

index bbaf6c984bf981fab8dbdecb26b11d7aef97063f..6ab87afd264585ca57f31bb09ab6d0c8fced4775 100644 (file)
@@ -39,6 +39,8 @@ public class DbVersion66 implements DbVersion {
       .add(1810, "Add ce_activity.error_type", AddErrorTypeColumnToCeActivityTable.class)
       .add(1811, "Create table qprofile_edit_users", CreateTableQProfileEditUsers.class)
       .add(1812, "Create table qprofile_edit_groups", CreateTableQProfileEditGroups.class)
-      .add(1813, "Drop PR columns from project_brances", DropPrColumnsFromProjectBranches.class);
+      .add(1813, "Drop PR columns from project_branches", DropPrColumnsFromProjectBranches.class)
+      .add(1814, "Purge table properties", PurgeTableProperties.class)
+    ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PurgeTableProperties.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PurgeTableProperties.java
new file mode 100644 (file)
index 0000000..28e4dec
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * 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.v66;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+public class PurgeTableProperties extends DataChange {
+
+  public PurgeTableProperties(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    deleteByKey(context, "views.analysisDelayingInMinutes");
+    deleteByKey(context, "views.status");
+    deleteByKey(context, "sonar.issuesdensity.weight");
+    deleteByKey(context, "sonar.core.version");
+    deleteByKeyPrefix(context, "sonar.reports.");
+    deleteByKeyPrefix(context, "sonar.report.license");
+    deleteByKeyPrefix(context, "sonar.sqale.");
+    deleteByKeyPrefix(context, "sqale.license");
+    deleteByKeyPrefix(context, "devcockpit.");
+    deleteByKeyPrefix(context, "masterproject.");
+    deleteByKeyPrefix(context, "sonar.natural.");
+    deleteByKeyPrefix(context, "sonarsource.natural.");
+    deleteByKeyPrefix(context, "sonarsource.identity.");
+    deleteByKeyPrefix(context, "sonar.build-stability.");
+  }
+
+  private static void deleteByKey(Context context, String key) throws SQLException {
+    context.prepareUpsert("delete from properties where prop_key = ?")
+      .setString(1, key)
+      .execute()
+      .commit();
+  }
+
+  private static void deleteByKeyPrefix(Context context, String key) throws SQLException {
+    context.prepareUpsert("delete from properties where prop_key like ?")
+      .setString(1, key + "%")
+      .execute()
+      .commit();
+  }
+}
index de7450360cfbb85e98a3f3c1c783074f6043be0c..073819a0ee4b86405311774d5c25f1c778748fe3 100644 (file)
@@ -36,7 +36,7 @@ public class DbVersion66Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 13);
+    verifyMigrationCount(underTest, 14);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PurgeTablePropertiesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PurgeTablePropertiesTest.java
new file mode 100644 (file)
index 0000000..c2e7b1c
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * 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.v66;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PurgeTablePropertiesTest {
+
+  private static final String TABLE_PROPERTIES = "properties";
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(PurgeTablePropertiesTest.class, "properties.sql");
+
+  private PurgeTableProperties underTest = new PurgeTableProperties(db.database());
+
+  @Test
+  public void migration_has_no_effect_on_empty_db() throws Exception {
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_PROPERTIES)).isEqualTo(0);
+  }
+
+  @Test
+  public void migration_deletes_properties_by_keys() throws Exception {
+    // to be deleted
+    insert("views.analysisDelayingInMinutes");
+    insert("views.status");
+    insert("sonar.issuesdensity.weight");
+    // to be kept
+    insert("views.status.differentSuffix");
+    insert("views.foo");
+
+    underTest.execute();
+
+    verifyRemainingKeys("views.status.differentSuffix", "views.foo");
+  }
+
+  @Test
+  public void migration_deletes_properties_by_key_prefixes() throws Exception {
+    // to be deleted
+    insert("sonar.sqale.foo");
+    insert("sonar.sqale.bar");
+    // to be kept
+    insert("sonar.sqale");
+    insert("sqale");
+
+    underTest.execute();
+
+    verifyRemainingKeys("sonar.sqale", "sqale");
+  }
+
+  private void insert(String key) {
+    // test the different combinations of keys
+    db.executeInsert(TABLE_PROPERTIES, "prop_key", key, "text_value", "foo", "is_empty", false);
+    db.executeInsert(TABLE_PROPERTIES, "prop_key", key, "text_value", "foo", "is_empty", false, "user_id", 100);
+    db.executeInsert(TABLE_PROPERTIES, "prop_key", key, "text_value", "foo", "is_empty", false, "resource_id", 200);
+  }
+
+  private void verifyRemainingKeys(String... expectedKeys) {
+    assertThat(selectKeys())
+      // verify that the 3 different combinations of rows are still present
+      .hasSize(expectedKeys.length * 3)
+      .containsOnly(expectedKeys);
+  }
+
+  private List<String> selectKeys() {
+    List<Map<String, Object>> rows = db.select("select prop_key as \"key\" from properties");
+    return rows.stream()
+      .map(row -> (String) row.get("key"))
+      .collect(Collectors.toList());
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PurgeTablePropertiesTest/properties.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PurgeTablePropertiesTest/properties.sql
new file mode 100644 (file)
index 0000000..dfc39d8
--- /dev/null
@@ -0,0 +1,11 @@
+CREATE TABLE "PROPERTIES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "PROP_KEY" VARCHAR(512) NOT NULL,
+  "RESOURCE_ID" INTEGER,
+  "USER_ID" INTEGER,
+  "IS_EMPTY" BOOLEAN NOT NULL,
+  "TEXT_VALUE" VARCHAR(4000),
+  "CLOB_VALUE" CLOB(2147483647),
+  "CREATED_AT" BIGINT
+);
+CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY");