From ec85c6ac6c0a29f27eb241dff4445be8ad07e727 Mon Sep 17 00:00:00 2001 From: Pierre Date: Fri, 20 May 2022 15:43:22 +0200 Subject: [PATCH] SONAR-16419 drop rules_metadata table --- server/sonar-db-dao/src/schema/schema-sq.ddl | 17 ------- .../db/migration/version/v95/DbVersion95.java | 1 + .../version/v95/DropRuleMetadataTable.java | 48 +++++++++++++++++ .../v95/DropRuleMetadataTableTest.java | 51 +++++++++++++++++++ .../v95/DropRuleMetadataTableTest/schema.sql | 16 ++++++ 5 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest/schema.sql diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index dacc969bb29..ce605b46a5b 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -865,23 +865,6 @@ CREATE TABLE "RULES"( ALTER TABLE "RULES" ADD CONSTRAINT "PK_RULES" PRIMARY KEY("UUID"); CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES"("PLUGIN_RULE_KEY" NULLS FIRST, "PLUGIN_NAME" NULLS FIRST); -CREATE TABLE "RULES_METADATA"( - "RULE_UUID" CHARACTER VARYING(40) NOT NULL, - "NOTE_DATA" CHARACTER LARGE OBJECT, - "NOTE_USER_UUID" CHARACTER VARYING(255), - "NOTE_CREATED_AT" BIGINT, - "NOTE_UPDATED_AT" BIGINT, - "REMEDIATION_FUNCTION" CHARACTER VARYING(20), - "REMEDIATION_GAP_MULT" CHARACTER VARYING(20), - "REMEDIATION_BASE_EFFORT" CHARACTER VARYING(20), - "TAGS" CHARACTER VARYING(4000), - "AD_HOC_NAME" CHARACTER VARYING(200), - "AD_HOC_DESCRIPTION" CHARACTER LARGE OBJECT, - "AD_HOC_SEVERITY" CHARACTER VARYING(10), - "AD_HOC_TYPE" TINYINT -); -ALTER TABLE "RULES_METADATA" ADD CONSTRAINT "PK_RULES_METADATA" PRIMARY KEY("RULE_UUID"); - CREATE TABLE "RULES_PARAMETERS"( "UUID" CHARACTER VARYING(40) NOT NULL, "NAME" CHARACTER VARYING(128) NOT NULL, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DbVersion95.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DbVersion95.java index 45271f2d98a..2a3661ac40b 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DbVersion95.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DbVersion95.java @@ -40,6 +40,7 @@ public class DbVersion95 implements DbVersion { .add(6412, "Add rules_metadata columns to rules table", AddRulesMetadataColumnsToRulesTable.class) .add(6413, "Populate rules metadata in rules table", PopulateRulesMetadataInRuleTable.class) + .add(6414, "Drop rules_metadata table", DropRuleMetadataTable.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTable.java new file mode 100644 index 00000000000..f7e6e14c5c5 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTable.java @@ -0,0 +1,48 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v95; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.DropTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropRuleMetadataTable extends DdlChange { + + private static final String TABLE_NAME = "rules_metadata"; + + public DropRuleMetadataTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + if (tableExists()) { + context.execute(new DropTableBuilder(getDialect(), TABLE_NAME).build()); + } + } + + private boolean tableExists() throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.tableExists(TABLE_NAME, connection); + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest.java new file mode 100644 index 00000000000..1cafd68d788 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v95; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropRuleMetadataTableTest { + private static final String TABLE_NAME = "rules_metadata"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(DropRuleMetadataTableTest.class, "schema.sql"); + + private final DdlChange underTest = new DropRuleMetadataTable(db.database()); + + @Test + public void migration_should_drop_rules_metadata_table() throws SQLException { + db.assertTableExists(TABLE_NAME); + underTest.execute(); + db.assertTableDoesNotExist(TABLE_NAME); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + db.assertTableExists(TABLE_NAME); + underTest.execute(); + // re-entrant + underTest.execute(); + db.assertTableDoesNotExist(TABLE_NAME); + } +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest/schema.sql new file mode 100644 index 00000000000..2532fec9593 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v95/DropRuleMetadataTableTest/schema.sql @@ -0,0 +1,16 @@ +CREATE TABLE "RULES_METADATA"( + "RULE_UUID" CHARACTER VARYING(40) NOT NULL, + "NOTE_DATA" CHARACTER LARGE OBJECT, + "NOTE_USER_UUID" CHARACTER VARYING(255), + "NOTE_CREATED_AT" BIGINT, + "NOTE_UPDATED_AT" BIGINT, + "REMEDIATION_FUNCTION" CHARACTER VARYING(20), + "REMEDIATION_GAP_MULT" CHARACTER VARYING(20), + "REMEDIATION_BASE_EFFORT" CHARACTER VARYING(20), + "TAGS" CHARACTER VARYING(4000), + "AD_HOC_NAME" CHARACTER VARYING(200), + "AD_HOC_DESCRIPTION" CHARACTER LARGE OBJECT, + "AD_HOC_SEVERITY" CHARACTER VARYING(10), + "AD_HOC_TYPE" TINYINT +); +ALTER TABLE "RULES_METADATA" ADD CONSTRAINT "PK_RULES_METADATA" PRIMARY KEY("RULE_UUID"); \ No newline at end of file -- 2.39.5