]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7470 Delete manual rules from DB
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 24 Mar 2016 17:03:34 +0000 (18:03 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 29 Mar 2016 17:12:01 +0000 (19:12 +0200)
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1124_delete_manual_rules.rb [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualRules.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualRulesTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualRulesTest/before.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualRulesTest/schema.sql [new file with mode: 0644]

diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1124_delete_manual_rules.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1124_delete_manual_rules.rb
new file mode 100644 (file)
index 0000000..7a4b932
--- /dev/null
@@ -0,0 +1,31 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 5.5
+# SONAR-7472
+#
+class DeleteManualRules < ActiveRecord::Migration
+
+  def self.up
+    execute_java_migration('org.sonar.db.version.v55.DeleteManualRules')
+  end
+
+end
index fd8d63f7bc07d2716795032d97cc29d3c9c5bf8f..4b0508ef27fc79f98f1487112a7d3d771213881e 100644 (file)
@@ -29,7 +29,7 @@ import org.sonar.db.MyBatis;
 
 public class DatabaseVersion {
 
-  public static final int LAST_VERSION = 1123;
+  public static final int LAST_VERSION = 1124;
 
   /**
    * The minimum supported version which can be upgraded. Lower
index 7877908e6fd34ba92b00545db4f5a661bf2e80bd..2ba4db579a7eaa69866bbee43224f35d113d00b3 100644 (file)
@@ -73,6 +73,7 @@ import org.sonar.db.version.v55.AddActiveRulesLongDateColumns;
 import org.sonar.db.version.v55.AddIssuesType;
 import org.sonar.db.version.v55.AddRulesColumns;
 import org.sonar.db.version.v55.DeleteManualIssues;
+import org.sonar.db.version.v55.DeleteManualRules;
 import org.sonar.db.version.v55.DeleteMeasuresWithCharacteristicId;
 import org.sonar.db.version.v55.DeleteMeasuresWithRuleId;
 import org.sonar.db.version.v55.DropActiveRulesDateColumns;
@@ -159,7 +160,8 @@ public class MigrationStepModule extends Module {
       DropActiveRulesDateColumns.class,
       FeedRulesTypes.class,
       DeleteMeasuresWithRuleId.class,
-      DeleteManualIssues.class
+      DeleteManualIssues.class,
+      DeleteManualRules.class
     );
   }
 }
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualRules.java b/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualRules.java
new file mode 100644 (file)
index 0000000..43e9175
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v55;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+import org.sonar.db.version.Select;
+import org.sonar.db.version.SqlStatement;
+
+public class DeleteManualRules extends BaseDataChange {
+
+  public DeleteManualRules(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("SELECT id FROM rules WHERE plugin_name='manual'");
+    massUpdate.update("DELETE FROM rules WHERE id=?");
+    massUpdate.rowPluralName("manual rules");
+    massUpdate.execute(new MigrationHandler());
+  }
+
+  private static class MigrationHandler implements MassUpdate.Handler {
+
+    @Override
+    public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
+      Long id = row.getNullableLong(1);
+      update.setLong(1, id);
+      return true;
+    }
+  }
+
+}
index 511020c42c7d028c68b8e7af2c126a4057be7aaf..9b36923ad6c47e72bc7d5f84762a6c90c64eb100 100644 (file)
@@ -401,6 +401,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1120');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1121');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1122');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1123');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1124');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
index daaef18b78834a790eaf81e15141da528757b053..20c4b0d35370fb64aba1c0faf0dc26619328410b 100644 (file)
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
   public void verify_count_of_added_MigrationStep_types() {
     ComponentContainer container = new ComponentContainer();
     new MigrationStepModule().configure(container);
-    assertThat(container.size()).isEqualTo(63);
+    assertThat(container.size()).isEqualTo(64);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualRulesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualRulesTest.java
new file mode 100644 (file)
index 0000000..ab9b2e9
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.db.version.v55;
+
+import java.util.List;
+import java.util.Map;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.version.MigrationStep;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DeleteManualRulesTest {
+
+  @Rule
+  public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteManualRulesTest.class, "schema.sql");
+
+  private MigrationStep migration = new DeleteManualRules(db.database());;
+
+  @Test
+  public void delete_manual_issues() throws Exception {
+    db.prepareDbUnit(getClass(), "before.xml");
+
+    migration.execute();
+
+    List<Map<String, Object>> rows = db.select("select id from rules");
+    assertThat(rows).hasSize(1);
+    assertThat(rows.get(0).get("ID")).isEqualTo(1L);
+  }
+
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualRulesTest/before.xml b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualRulesTest/before.xml
new file mode 100644 (file)
index 0000000..41cfb1d
--- /dev/null
@@ -0,0 +1,24 @@
+<dataset>
+
+  <!-- Non manual rule : should not be removed -->
+
+  <rules id="1" name="Null Pointer" plugin_rule_key="S001"
+         plugin_config_key="S1" plugin_name="java" description="[null]" priority="4" status="READY"
+         is_template="[false]" template_id="[null]"
+         tags="bug,performance" system_tags="cwe"
+         created_at="1500000000000" updated_at="1600000000000"
+  />
+
+  <!-- Manual rule : should be removed -->
+
+  <rules id="2" name="Manual" plugin_rule_key="man"
+         plugin_config_key="[null]" plugin_name="manual" description="[null]" priority="4" status="READY"
+         is_template="[false]" template_id="[null]"
+         tags="bug,performance" system_tags="cwe"
+         created_at="1500000000000" updated_at="1600000000000"
+  />
+
+
+</dataset>
+
+
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualRulesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualRulesTest/schema.sql
new file mode 100644 (file)
index 0000000..71b842e
--- /dev/null
@@ -0,0 +1,31 @@
+CREATE TABLE "RULES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "PLUGIN_RULE_KEY" VARCHAR(200) NOT NULL,
+  "PLUGIN_NAME" VARCHAR(255) NOT NULL,
+  "DESCRIPTION" VARCHAR(16777215),
+  "DESCRIPTION_FORMAT" VARCHAR(20),
+  "PRIORITY" INTEGER,
+  "IS_TEMPLATE" BOOLEAN DEFAULT FALSE,
+  "TEMPLATE_ID" INTEGER,
+  "PLUGIN_CONFIG_KEY" VARCHAR(200),
+  "NAME" VARCHAR(200),
+  "STATUS" VARCHAR(40),
+  "LANGUAGE" VARCHAR(20),
+  "NOTE_DATA" CLOB(2147483647),
+  "NOTE_USER_LOGIN" VARCHAR(255),
+  "NOTE_CREATED_AT" TIMESTAMP,
+  "NOTE_UPDATED_AT" TIMESTAMP,
+  "REMEDIATION_FUNCTION" VARCHAR(20),
+  "DEF_REMEDIATION_FUNCTION" VARCHAR(20),
+  "REMEDIATION_GAP_MULT" VARCHAR(20),
+  "DEF_REMEDIATION_GAP_MULT" VARCHAR(20),
+  "REMEDIATION_BASE_EFFORT" VARCHAR(20),
+  "DEF_REMEDIATION_BASE_EFFORT" VARCHAR(20),
+  "GAP_DESCRIPTION" VARCHAR(4000),
+  "TAGS" VARCHAR(4000),
+  "SYSTEM_TAGS" VARCHAR(4000),
+  "RULE_TYPE" TINYINT,
+  "CREATED_AT" BIGINT,
+  "UPDATED_AT" BIGINT
+);
+