From 9f256f139f244a23c5fe0c48f6a14a725e757335 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 22 Mar 2016 15:35:34 +0100 Subject: [PATCH] SONAR-7330 Remove measures on rules in DB --- .../1120_delete_measures_with_rule_id.rb | 31 ++++++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 5 +- .../version/v55/DeleteMeasuresWithRuleId.java | 54 ++++++++++++++++++ .../org/sonar/db/version/rows-h2.sql | 1 + .../db/version/MigrationStepModuleTest.java | 2 +- .../v55/DeleteMeasuresWithRuleIdTest.java | 56 +++++++++++++++++++ .../DeleteMeasuresWithRuleIdTest/before.xml | 7 +++ .../DeleteMeasuresWithRuleIdTest/schema.sql | 25 +++++++++ 9 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1120_delete_measures_with_rule_id.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleId.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/before.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/schema.sql diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1120_delete_measures_with_rule_id.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1120_delete_measures_with_rule_id.rb new file mode 100644 index 00000000000..f901d2c0ba8 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1120_delete_measures_with_rule_id.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-7425 +# +class DeleteMeasuresWithRuleId < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v55.DeleteMeasuresWithRuleId') + 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 bcfa9d1a242..adc1dfa4d8d 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 = 1119; + public static final int LAST_VERSION = 1120; /** * 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 550de251638..4337d7ac509 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 @@ -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.DeleteMeasuresWithCharacteristicId; +import org.sonar.db.version.v55.DeleteMeasuresWithRuleId; import org.sonar.db.version.v55.DropActiveRulesDateColumns; import org.sonar.db.version.v55.DropRulesDatesAndCharacteristics; import org.sonar.db.version.v55.FeedActiveRulesLongDateColumns; @@ -155,7 +156,7 @@ public class MigrationStepModule extends Module { FeedIssueTypes.class, DropRulesDatesAndCharacteristics.class, DropActiveRulesDateColumns.class, - FeedRulesTypes.class - ); + FeedRulesTypes.class, + DeleteMeasuresWithRuleId.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleId.java b/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleId.java new file mode 100644 index 00000000000..1342c6f7c79 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleId.java @@ -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 DeleteMeasuresWithRuleId extends BaseDataChange { + + public DeleteMeasuresWithRuleId(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT id FROM project_measures WHERE rule_id IS NOT NULL"); + massUpdate.update("DELETE FROM project_measures WHERE id=?"); + massUpdate.rowPluralName("measures with rule"); + 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 67b26290e70..d4ca8b75db1 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 @@ -397,6 +397,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1116'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1117'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1118'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1119'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1120'); 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 a4ed4bd9e31..d9664da2499 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(61); + assertThat(container.size()).isEqualTo(62); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest.java new file mode 100644 index 00000000000..4fae6b684b5 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest.java @@ -0,0 +1,56 @@ +/* + * 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.Before; +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 DeleteMeasuresWithRuleIdTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteMeasuresWithRuleIdTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + migration = new DeleteMeasuresWithRuleId(db.database()); + } + + @Test + public void delete_measures_with_rule_id() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + migration.execute(); + + List> rows = db.select("select id from project_measures"); + assertThat(rows).hasSize(1); + assertThat(rows.get(0).get("ID")).isEqualTo(10L); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/before.xml b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/before.xml new file mode 100644 index 00000000000..be95650fcce --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/before.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/schema.sql new file mode 100644 index 00000000000..1aae50c211d --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/DeleteMeasuresWithRuleIdTest/schema.sql @@ -0,0 +1,25 @@ +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, + "SNAPSHOT_ID" INTEGER, + "RULE_ID" INTEGER, + "RULES_CATEGORY_ID" INTEGER, + "TEXT_VALUE" VARCHAR(4000), + "TENDENCY" INTEGER, + "MEASURE_DATE" TIMESTAMP, + "PROJECT_ID" INTEGER, + "ALERT_STATUS" VARCHAR(5), + "ALERT_TEXT" VARCHAR(4000), + "URL" VARCHAR(2000), + "DESCRIPTION" VARCHAR(4000), + "RULE_PRIORITY" INTEGER, + "CHARACTERISTIC_ID" INTEGER, + "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(167772150) +); -- 2.39.5