From 4325cef7f932d26dc3fbf171836c15a15d2e4903 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 24 Mar 2016 18:04:37 +0100 Subject: [PATCH] SONAR-7472 Delete manual issues from DB --- .../db/migrate/1123_delete_manual_issues.rb | 31 ++++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 5 +- .../db/version/v55/DeleteManualIssues.java | 55 +++++++++++++ .../org/sonar/db/version/rows-h2.sql | 1 + .../db/version/MigrationStepModuleTest.java | 2 +- .../version/v55/DeleteManualIssuesTest.java | 50 ++++++++++++ .../v55/DeleteManualIssuesTest/before.xml | 79 +++++++++++++++++++ .../v55/DeleteManualIssuesTest/schema.sql | 60 ++++++++++++++ 9 files changed, 282 insertions(+), 3 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1123_delete_manual_issues.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualIssues.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualIssuesTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/before.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/schema.sql diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1123_delete_manual_issues.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1123_delete_manual_issues.rb new file mode 100644 index 00000000000..00f726f1994 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1123_delete_manual_issues.rb @@ -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 DeleteManualIssues < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v55.DeleteManualIssues') + end + +end diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index d6e3e3c87f2..fd8d63f7bc0 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -29,7 +29,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1122; + public static final int LAST_VERSION = 1123; /** * The minimum supported version which can be upgraded. Lower diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index 4337d7ac509..7877908e6fd 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -72,6 +72,7 @@ import org.sonar.db.version.v54.RemovePreviewPermission; 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.DeleteMeasuresWithCharacteristicId; import org.sonar.db.version.v55.DeleteMeasuresWithRuleId; import org.sonar.db.version.v55.DropActiveRulesDateColumns; @@ -157,6 +158,8 @@ public class MigrationStepModule extends Module { DropRulesDatesAndCharacteristics.class, DropActiveRulesDateColumns.class, FeedRulesTypes.class, - DeleteMeasuresWithRuleId.class); + DeleteMeasuresWithRuleId.class, + DeleteManualIssues.class + ); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualIssues.java b/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualIssues.java new file mode 100644 index 00000000000..911feed1bf8 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteManualIssues.java @@ -0,0 +1,55 @@ +/* + * 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 DeleteManualIssues extends BaseDataChange { + + public DeleteManualIssues(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT i.id FROM issues i " + + "INNER JOIN rules r ON r.id=i.rule_id AND r.plugin_name='manual'"); + massUpdate.update("DELETE FROM issues WHERE id=?"); + massUpdate.rowPluralName("manual issues"); + 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; + } + } + +} diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 9b1fe44c018..511020c42c7 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -400,6 +400,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1119'); 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 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; diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index d9664da2499..daaef18b788 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -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(62); + assertThat(container.size()).isEqualTo(63); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualIssuesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualIssuesTest.java new file mode 100644 index 00000000000..26f5f716d76 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteManualIssuesTest.java @@ -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 DeleteManualIssuesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteManualIssuesTest.class, "schema.sql"); + + private MigrationStep migration = new DeleteManualIssues(db.database());; + + @Test + public void delete_manual_issues() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + migration.execute(); + + List> rows = db.select("select id from issues"); + assertThat(rows).hasSize(1); + assertThat(rows.get(0).get("ID")).isEqualTo(100L); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/before.xml b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/before.xml new file mode 100644 index 00000000000..b6a34b757e8 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/before.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/schema.sql new file mode 100644 index 00000000000..780ba0d71b2 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteManualIssuesTest/schema.sql @@ -0,0 +1,60 @@ +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 +); + +CREATE TABLE "ISSUES" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "KEE" VARCHAR(50) UNIQUE NOT NULL, + "COMPONENT_UUID" VARCHAR(50), + "PROJECT_UUID" VARCHAR(50), + "RULE_ID" INTEGER, + "SEVERITY" VARCHAR(10), + "MANUAL_SEVERITY" BOOLEAN NOT NULL, + "MESSAGE" VARCHAR(4000), + "LINE" INTEGER, + "GAP" DOUBLE, + "EFFORT" INTEGER, + "STATUS" VARCHAR(20), + "RESOLUTION" VARCHAR(20), + "CHECKSUM" VARCHAR(1000), + "REPORTER" VARCHAR(255), + "ASSIGNEE" VARCHAR(255), + "AUTHOR_LOGIN" VARCHAR(255), + "ACTION_PLAN_KEY" VARCHAR(50) NULL, + "ISSUE_ATTRIBUTES" VARCHAR(4000), + "TAGS" VARCHAR(4000), + "ISSUE_CREATION_DATE" BIGINT, + "ISSUE_CLOSE_DATE" BIGINT, + "ISSUE_UPDATE_DATE" BIGINT, + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT, + "LOCATIONS" BLOB(167772150), + "ISSUE_TYPE" TINYINT +); -- 2.39.5