From 7c4cce15e0214eed46edcfea64bf0b085ef0ad80 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 4 Dec 2017 10:19:58 +0100 Subject: [PATCH] SONAR-10134 Remove unique index on QUALITY_GATES.NAME As quality gates are now associated to organization, it should be possible to use the same name in different organizations --- .../org/sonar/db/version/schema-h2.ddl | 1 - .../db/migration/version/v70/DbVersion70.java | 2 +- .../DropUniqueIndexOnQualityGatesName.java | 40 +++++++++++++++ .../version/v70/DbVersion70Test.java | 2 +- ...DropUniqueIndexOnQualityGatesNameTest.java | 51 +++++++++++++++++++ .../quality_gates.sql | 10 ++++ 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesName.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest/quality_gates.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 1b1db706784..a70eeaa3e1e 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -235,7 +235,6 @@ CREATE TABLE "QUALITY_GATES" ( "CREATED_AT" TIMESTAMP, "UPDATED_AT" TIMESTAMP, ); -CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME"); CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES_UUID" ON "QUALITY_GATES" ("UUID"); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java index f11b627b5d4..2b86136c4cf 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java @@ -19,7 +19,6 @@ */ package org.sonar.server.platform.db.migration.version.v70; -import java.util.UUID; import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; import org.sonar.server.platform.db.migration.version.DbVersion; @@ -43,6 +42,7 @@ public class DbVersion70 implements DbVersion { .add(1912, "Create QUALITY_GATES.UUID", AddUuidToQualityGates.class) .add(1913, "Populate QUALITY_GATES.UUID", PopulateUuidOnQualityGates.class) .add(1914, "Make QUALITY_GATES.UUID not nullable", MakeUuidNotNullableOnQualityGates.class) + .add(1915, "Drop unique index on QUALITY_GATES.NAME", DropUniqueIndexOnQualityGatesName.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesName.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesName.java new file mode 100644 index 00000000000..b9b113467e5 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesName.java @@ -0,0 +1,40 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.sql.DropIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropUniqueIndexOnQualityGatesName extends DdlChange { + + public DropUniqueIndexOnQualityGatesName(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new DropIndexBuilder(getDialect()) + .setTable("quality_gates") + .setName("uniq_quality_gates") + .build()); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java index 3ad5338b4e6..ef2fe5a9b1b 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java @@ -35,7 +35,7 @@ public class DbVersion70Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 15); + verifyMigrationCount(underTest, 16); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest.java new file mode 100644 index 00000000000..64ec99bbe3d --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +public class DropUniqueIndexOnQualityGatesNameTest { + + @Rule + public final CoreDbTester dbTester = CoreDbTester.createForSchema(DropUniqueIndexOnQualityGatesNameTest.class, "quality_gates.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private DropUniqueIndexOnQualityGatesName underTest = new DropUniqueIndexOnQualityGatesName(dbTester.database()); + + @Test + public void unique_index_on_name_is_removed() throws SQLException { + underTest.execute(); + + dbTester.assertIndexDoesNotExist("quality_gates", "uniq_quality_gates"); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest/quality_gates.sql new file mode 100644 index 00000000000..a3af576cd68 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DropUniqueIndexOnQualityGatesNameTest/quality_gates.sql @@ -0,0 +1,10 @@ +CREATE TABLE "QUALITY_GATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "UUID" VARCHAR(40) NOT NULL, + "NAME" VARCHAR(100) NOT NULL, + "IS_BUILT_IN" BOOLEAN NOT NULL, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, +); +CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME"); +CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES_UUID" ON "QUALITY_GATES" ("UUID"); \ No newline at end of file -- 2.39.5