From 046911d801c040ce4f0ad371ad2f4751522077d0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 8 Nov 2016 15:20:13 +0100 Subject: [PATCH] SONAR-8355 Drop tables "measure_filters" and "measure_filter_favourites" --- .../1421_drop_measure_filters_tables.rb | 29 +++++++++ .../org/sonar/db/version/DatabaseVersion.java | 6 +- .../sonar/db/version/MigrationStepModule.java | 4 +- .../version/v62/DropMeasureFiltersTables.java | 65 +++++++++++++++++++ .../org/sonar/db/version/rows-h2.sql | 1 + .../org/sonar/db/version/schema-h2.ddl | 22 ------- .../db/version/MigrationStepModuleTest.java | 2 +- .../v62/DropMeasureFiltersTableTest.java | 52 +++++++++++++++ .../DropMeasureFiltersTableTest/schema.sql | 20 ++++++ 9 files changed, 174 insertions(+), 27 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1421_drop_measure_filters_tables.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v62/DropMeasureFiltersTables.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v62/DropMeasureFiltersTableTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v62/DropMeasureFiltersTableTest/schema.sql diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1421_drop_measure_filters_tables.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1421_drop_measure_filters_tables.rb new file mode 100644 index 00000000000..922b87c2950 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1421_drop_measure_filters_tables.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.2 +# +class DropMeasureFiltersTables < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v62.DropMeasureFiltersTables') + 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 3dc598bce86..c68e40547fb 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_420; + public static final int LAST_VERSION = 1_421; /** * The minimum supported version which can be upgraded. Lower @@ -50,6 +50,8 @@ public class DatabaseVersion { "active_dashboards", "activities", "dashboards", + "measure_filters", + "measure_filter_favourites", "widgets", "widget_properties"); @@ -79,8 +81,6 @@ public class DatabaseVersion { "issue_filter_favourites", "loaded_templates", "manual_measures", - "measure_filters", - "measure_filter_favourites", "metrics", "notifications", "organizations", 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 5e4014e7c0c..a18e3c1dbfe 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 @@ -167,6 +167,7 @@ import org.sonar.db.version.v62.AddOrganizationUuidToUserRoles; import org.sonar.db.version.v62.CreateDefaultOrganization; import org.sonar.db.version.v62.CreateTableOrganizations; import org.sonar.db.version.v62.DeletePermissionShareDashboard; +import org.sonar.db.version.v62.DropMeasureFiltersTables; import org.sonar.db.version.v62.DropRelatedDashboardTables; import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnGroupRoles; import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnGroups; @@ -378,6 +379,7 @@ public class MigrationStepModule extends Module { PopulateOrganizationUuidOfGroupRoles.class, MakeOrganizationUuidNotNullOnGroupRoles.class, UpdateQualityGateConditionsOnCoverage.class, - DropRelatedDashboardTables.class); + DropRelatedDashboardTables.class, + DropMeasureFiltersTables.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/DropMeasureFiltersTables.java b/sonar-db/src/main/java/org/sonar/db/version/v62/DropMeasureFiltersTables.java new file mode 100644 index 00000000000..76c9e83fb67 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/DropMeasureFiltersTables.java @@ -0,0 +1,65 @@ +/* + * 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.v62; + +import com.google.common.collect.ImmutableList; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.db.version.DdlChange; +import org.sonar.db.version.DropTableBuilder; + +import static org.sonar.core.util.stream.Collectors.toList; + +public class DropMeasureFiltersTables extends DdlChange { + + private static final Logger LOGGER = Loggers.get(DropMeasureFiltersTables.class); + + private static final List TABLES_TO_DROP = ImmutableList.of("measure_filters", "measure_filter_favourites"); + + private final Database db; + + public DropMeasureFiltersTables(Database db) { + super(db); + this.db = db; + } + + @Override + public void execute(Context context) throws SQLException { + List tablesToDrop = getEffectiveTablesToDrop(); + LOGGER.info("Removing tables {}", tablesToDrop); + context.execute(tablesToDrop + .stream() + .flatMap(table -> new DropTableBuilder(db.getDialect(), table).build().stream()) + .collect(toList())); + } + + private List getEffectiveTablesToDrop() throws SQLException { + try (Connection connection = db.getDataSource().getConnection()) { + return TABLES_TO_DROP.stream() + .filter(table -> DatabaseUtils.tableExists(table, connection)) + .collect(toList()); + } + } +} 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 31346602831..ebc66acf092 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 @@ -509,6 +509,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1417'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1418'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1419'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1420'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1421'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482'); 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 a98df497e24..67583b26b67 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 @@ -426,28 +426,6 @@ CREATE TABLE "AUTHORS" ( CREATE UNIQUE INDEX "UNIQ_AUTHOR_LOGINS" ON "AUTHORS" ("LOGIN"); -CREATE TABLE "MEASURE_FILTERS" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "NAME" VARCHAR(100) NOT NULL, - "SHARED" BOOLEAN NOT NULL DEFAULT FALSE, - "USER_ID" INTEGER, - "DESCRIPTION" VARCHAR(4000), - "DATA" CLOB(2147483647), - "CREATED_AT" TIMESTAMP, - "UPDATED_AT" TIMESTAMP -); -CREATE INDEX "MEASURE_FILTERS_NAME" ON "MEASURE_FILTERS" ("NAME"); - - -CREATE TABLE "MEASURE_FILTER_FAVOURITES" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "USER_ID" INTEGER NOT NULL, - "MEASURE_FILTER_ID" INTEGER NOT NULL, - "CREATED_AT" TIMESTAMP -); -CREATE INDEX "MEASURE_FILTER_FAVS_USERID" ON "MEASURE_FILTER_FAVOURITES" ("USER_ID"); - - CREATE TABLE "ISSUES" ( "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "KEE" VARCHAR(50) UNIQUE NOT NULL, 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 700344fb7a7..f9576af847e 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(161); + assertThat(container.size()).isEqualTo(162); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/DropMeasureFiltersTableTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/DropMeasureFiltersTableTest.java new file mode 100644 index 00000000000..6b6479d55af --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/DropMeasureFiltersTableTest.java @@ -0,0 +1,52 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +public class DropMeasureFiltersTableTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, DropMeasureFiltersTableTest.class, "schema.sql"); + + private DropMeasureFiltersTables underTest = new DropMeasureFiltersTables(db.database()); + + @Test + public void delete_tables() throws SQLException { + underTest.execute(); + + db.assertTableDoesNotExist("measure_filters"); + db.assertTableDoesNotExist("measure_filter_favourites"); + } + + @Test + public void migration_is_re_entrant() throws Exception { + underTest.execute(); + underTest.execute(); + + db.assertTableDoesNotExist("measure_filters"); + db.assertTableDoesNotExist("measure_filter_favourites"); + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/DropMeasureFiltersTableTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/DropMeasureFiltersTableTest/schema.sql new file mode 100644 index 00000000000..f7331d4bb8a --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/DropMeasureFiltersTableTest/schema.sql @@ -0,0 +1,20 @@ +CREATE TABLE "MEASURE_FILTERS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL, + "SHARED" BOOLEAN NOT NULL DEFAULT FALSE, + "USER_ID" INTEGER, + "DESCRIPTION" VARCHAR(4000), + "DATA" CLOB(2147483647), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); +CREATE INDEX "MEASURE_FILTERS_NAME" ON "MEASURE_FILTERS" ("NAME"); + + +CREATE TABLE "MEASURE_FILTER_FAVOURITES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "USER_ID" INTEGER NOT NULL, + "MEASURE_FILTER_ID" INTEGER NOT NULL, + "CREATED_AT" TIMESTAMP +); +CREATE INDEX "MEASURE_FILTER_FAVS_USERID" ON "MEASURE_FILTER_FAVOURITES" ("USER_ID"); -- 2.39.5