]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8520 index on DEPRECATED_RULE_KEYS.RULE_ID must not be unique
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 19 Jul 2019 14:26:41 +0000 (16:26 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 30 Jul 2019 18:24:26 +0000 (20:24 +0200)
this is an old error which slipped through the radar because H2 schema was correct

server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/DbVersion80.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUnique.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/DbVersion80Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest/deprecated_rule_keys.sql [new file with mode: 0644]

index f3f0a818d1e280871a288f7c43eb50239754fc40..a11fd4e4156016a820f3f865974c7fc8b284cd26 100644 (file)
@@ -27,6 +27,7 @@ public class DbVersion80 implements DbVersion {
   public void addSteps(MigrationStepRegistry registry) {
     registry
       .add(3000, "Set Organizations#guarded column nullable", MakeOrganizationsGuardedNullable.class)
-      .add(3001, "Create ProjectQualityGates table", CreateProjectQualityGatesTable.class);
+      .add(3001, "Create ProjectQualityGates table", CreateProjectQualityGatesTable.class)
+      .add(3002, "Make index on DEPRECATED_RULE_KEYS.RULE_ID non unique", MakeDeprecatedRuleKeysRuleIdIndexNonUnique.class);
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUnique.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUnique.java
new file mode 100644 (file)
index 0000000..192af37
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v80;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.IntegerColumnDef;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.sql.DropIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class MakeDeprecatedRuleKeysRuleIdIndexNonUnique extends DdlChange {
+  private static final String TABLE_NAME = "deprecated_rule_keys";
+  private static final IntegerColumnDef RULE_ID_COLUMN = IntegerColumnDef.newIntegerColumnDefBuilder()
+    .setColumnName("rule_id")
+    .setIsNullable(false)
+    .build();
+
+  public MakeDeprecatedRuleKeysRuleIdIndexNonUnique(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new DropIndexBuilder(getDialect())
+      .setTable(TABLE_NAME)
+      .setName("rule_id_deprecated_rule_keys")
+      .build());
+
+    context.execute(new CreateIndexBuilder()
+      .setTable(TABLE_NAME)
+      .setName("rule_id_deprecated_rule_keys")
+      .setUnique(false)
+      .addColumn(RULE_ID_COLUMN)
+      .build());
+  }
+}
index 3552b825d633b1059f7c36208d5a1a6799825fdf..cefa4368acf0df029c74841d310f18c05ab16796 100644 (file)
@@ -35,7 +35,7 @@ public class DbVersion80Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 2);
+    verifyMigrationCount(underTest, 3);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest.java
new file mode 100644 (file)
index 0000000..13a7806
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v80;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest {
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest.class, "deprecated_rule_keys.sql");
+
+  private MakeDeprecatedRuleKeysRuleIdIndexNonUnique underTest = new MakeDeprecatedRuleKeysRuleIdIndexNonUnique(db.database());
+
+  @Test
+  public void execute_makes_index_non_unique() throws SQLException {
+    db.assertUniqueIndex("deprecated_rule_keys", "rule_id_deprecated_rule_keys", "rule_id");
+
+    underTest.execute();
+
+    db.assertIndex("deprecated_rule_keys", "rule_id_deprecated_rule_keys", "rule_id");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest/deprecated_rule_keys.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v80/MakeDeprecatedRuleKeysRuleIdIndexNonUniqueTest/deprecated_rule_keys.sql
new file mode 100644 (file)
index 0000000..b46329a
--- /dev/null
@@ -0,0 +1,11 @@
+CREATE TABLE "DEPRECATED_RULE_KEYS" (
+  "UUID"  VARCHAR(40) NOT NULL,
+  "RULE_ID" INTEGER NOT NULL,
+  "OLD_REPOSITORY_KEY" VARCHAR(200) NOT NULL,
+  "OLD_RULE_KEY" VARCHAR(255) NOT NULL,
+  "CREATED_AT" BIGINT,
+
+  CONSTRAINT "PK_DEPRECATED_RULE_KEYS" PRIMARY KEY ("UUID")
+);
+CREATE UNIQUE INDEX "UNIQ_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS" ("OLD_REPOSITORY_KEY", "OLD_RULE_KEY");
+CREATE UNIQUE INDEX "RULE_ID_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS" ("RULE_ID");