From f14651c6acb854fa6de10afc713a97711caf42aa Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sat, 11 Jun 2016 11:00:49 +0200 Subject: [PATCH] SONAR-7501 drop DB columns --- .../1207_drop_unused_measures_columns.rb | 29 ++++++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 4 +- .../v60/DropUnusedMeasuresColumns.java | 57 +++++++++++++++++++ .../sonar/db/version/v60/package-info.java | 1 - .../org/sonar/db/version/rows-h2.sql | 2 +- .../org/sonar/db/version/schema-h2.ddl | 7 --- .../db/version/MigrationStepModuleTest.java | 2 +- .../v60/DropUnusedMeasuresColumnsTest.java | 50 ++++++++++++++++ 9 files changed, 142 insertions(+), 12 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1207_drop_unused_measures_columns.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v60/DropUnusedMeasuresColumns.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v60/DropUnusedMeasuresColumnsTest.java diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1207_drop_unused_measures_columns.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1207_drop_unused_measures_columns.rb new file mode 100644 index 00000000000..f3a4817a991 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1207_drop_unused_measures_columns.rb @@ -0,0 +1,29 @@ +# +# 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 6.0 +# +class DropUnusedMeasuresColumns < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v60.DropUnusedMeasuresColumns') + 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 92a8efd43bb..5e4ed02478b 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 @@ -30,7 +30,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1_206; + public static final int LAST_VERSION = 1_207; /** * 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 31a7dfe683e..cde89e3adb0 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 @@ -86,6 +86,7 @@ import org.sonar.db.version.v56.FixTypeOfRuleTypeOnMysql; import org.sonar.db.version.v60.AddUuidColumnsToResourceIndex; import org.sonar.db.version.v60.CleanOrphanRowsInResourceIndex; import org.sonar.db.version.v60.DropIdColumnsFromResourceIndex; +import org.sonar.db.version.v60.DropUnusedMeasuresColumns; import org.sonar.db.version.v60.MakeUuidColumnsNotNullOnResourceIndex; import org.sonar.db.version.v60.PopulateUuidColumnsOfResourceIndex; @@ -177,6 +178,7 @@ public class MigrationStepModule extends Module { PopulateUuidColumnsOfResourceIndex.class, CleanOrphanRowsInResourceIndex.class, MakeUuidColumnsNotNullOnResourceIndex.class, - DropIdColumnsFromResourceIndex.class); + DropIdColumnsFromResourceIndex.class, + DropUnusedMeasuresColumns.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/DropUnusedMeasuresColumns.java b/sonar-db/src/main/java/org/sonar/db/version/v60/DropUnusedMeasuresColumns.java new file mode 100644 index 00000000000..40a0693829e --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/DropUnusedMeasuresColumns.java @@ -0,0 +1,57 @@ +/* + * 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.v60; + +import com.google.common.annotations.VisibleForTesting; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.version.DdlChange; +import org.sonar.db.version.DropColumnsBuilder; + +/** + * Drop the following columns from the project_measures table : + * - rule_category_id + * - tendency + * - url + * - measure_date + * - url + * - rule_priority + * - rule_id + * - Characteristic_id + */ +public class DropUnusedMeasuresColumns extends DdlChange { + + public DropUnusedMeasuresColumns(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(generateSql()); + } + + @VisibleForTesting + String generateSql() { + return new DropColumnsBuilder(getDatabase().getDialect(), "project_measures", + "rules_category_id", "tendency", "measure_date", "url", "rule_priority", "characteristic_id", "rule_id") + .build(); + } + +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/package-info.java b/sonar-db/src/main/java/org/sonar/db/version/v60/package-info.java index 4b742e678a6..0b1db5a714c 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/package-info.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/package-info.java @@ -21,4 +21,3 @@ package org.sonar.db.version.v60; import javax.annotation.ParametersAreNonnullByDefault; - 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 aa808f28acb..ea7ecb61914 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 @@ -404,7 +404,6 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1124'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1125'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1150'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1151'); -INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1152'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1200'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1201'); @@ -413,6 +412,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1203'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1204'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1205'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1206'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1207'); 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/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index 66739471b15..effcaec7dfd 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -197,18 +197,11 @@ CREATE TABLE "PROJECT_MEASURES" ( "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, 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 b5d86617502..5de4c3e4fdf 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(70); + assertThat(container.size()).isEqualTo(71); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/DropUnusedMeasuresColumnsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/DropUnusedMeasuresColumnsTest.java new file mode 100644 index 00000000000..a5ecc9c9961 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/DropUnusedMeasuresColumnsTest.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.v60; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.db.Database; +import org.sonar.db.dialect.PostgreSql; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DropUnusedMeasuresColumnsTest { + + DropUnusedMeasuresColumns underTest; + Database database; + + @Before + public void setUp() { + database = mock(Database.class); + underTest = new DropUnusedMeasuresColumns(database); + } + + @Test + public void generate_sql_on_postgresql() { + when(database.getDialect()).thenReturn(new PostgreSql()); + assertThat(underTest.generateSql()) + .isEqualTo( + "ALTER TABLE project_measures DROP COLUMN rules_category_id, DROP COLUMN tendency, DROP COLUMN measure_date, DROP COLUMN url, DROP COLUMN rule_priority, DROP COLUMN characteristic_id, DROP COLUMN rule_id"); + } + +} -- 2.39.5