]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19556 Fix DB migration and increase test coverage
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 19 Jun 2023 20:36:35 +0000 (15:36 -0500)
committersonartech <sonartech@sonarsource.com>
Tue, 20 Jun 2023 13:10:19 +0000 (13:10 +0000)
14 files changed:
server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeQueueImpl.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/CreateIndexOnColumn.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRoles.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameMainComponentUuidInCeActivity.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest/schema.sql [new file with mode: 0644]
server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/TagsAction.java

index 0a78e1a58041ad6b0471ff447d73322a6de01ad8..3ec3c21e114ae89477decc07825a9a18bb31091f 100644 (file)
@@ -34,9 +34,9 @@ import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+import org.slf4j.LoggerFactory;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.System2;
-import org.slf4j.LoggerFactory;
 import org.sonar.ce.task.CeTask;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.core.util.stream.MoreCollectors;
index 88557064bb3f4e9e17224d7423560624825c0dcc..fc73320b3d8b817f6271240f68d24aaeed320d23 100644 (file)
@@ -31,7 +31,7 @@ public abstract class CreateIndexOnColumn extends DdlChange {
   private final String columnName;
   private final boolean unique;
 
-  public CreateIndexOnColumn(Database db, String table, String columnName, boolean unique) {
+  protected CreateIndexOnColumn(Database db, String table, String columnName, boolean unique) {
     super(db);
     this.table = table;
     this.columnName = columnName;
index 1453651969b94e3a903dcc362292b63a368bc6bd..daa0af835748d93988780b8b2dcf59193072a860 100644 (file)
@@ -24,7 +24,7 @@ import org.sonar.server.platform.db.migration.step.CreateIndexOnColumn;
 
 public class CreateIndexEntityUuidInUserRoles extends CreateIndexOnColumn {
 
-  private static final String TABLE_NAME = "group_roles";
+  private static final String TABLE_NAME = "user_roles";
   private static final String COLUMN_NAME = "entity_uuid";
 
   public CreateIndexEntityUuidInUserRoles(Database db) {
index 2acbca0a99e258f81158b5d66e8f1e812a9ac00e..c41576a18d42c2db984a2324685e604645467673 100644 (file)
@@ -27,7 +27,6 @@ public class RenameMainComponentUuidInCeActivity extends RenameVarcharColumnChan
   private static final String TABLE_NAME = "ce_activity";
   private static final String OLD_COLUMN_NAME = "main_component_uuid";
   private static final String NEW_COLUMN_NAME = "entity_uuid";
-  private static final String OLD_INDEX_NAME = "ce_activity_main_component";
 
   public RenameMainComponentUuidInCeActivity(Database db) {
     super(db, TABLE_NAME, OLD_COLUMN_NAME, NEW_COLUMN_NAME);
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest.java
new file mode 100644 (file)
index 0000000..dde2a93
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v102;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class CreateIndexEntityUuidInCeActivityTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexEntityUuidInCeActivityTest.class, "schema.sql");
+
+  private final CreateIndexEntityUuidInCeActivity createIndex = new CreateIndexEntityUuidInCeActivity(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist("ce_activity", "ce_activity_entity_uuid");
+
+    createIndex.execute();
+
+    db.assertIndex("ce_activity", "ce_activity_entity_uuid", "entity_uuid");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    createIndex.execute();
+    createIndex.execute();
+
+    db.assertIndex("ce_activity", "ce_activity_entity_uuid", "entity_uuid");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest.java
new file mode 100644 (file)
index 0000000..01e7355
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v102;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class CreateIndexEntityUuidInCeQueueTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexEntityUuidInCeQueueTest.class, "schema.sql");
+
+  private final CreateIndexEntityUuidInCeQueue createIndex = new CreateIndexEntityUuidInCeQueue(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist("ce_queue", "ce_queue_entity_uuid");
+
+    createIndex.execute();
+
+    db.assertIndex("ce_queue", "ce_queue_entity_uuid", "entity_uuid");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    createIndex.execute();
+    createIndex.execute();
+
+    db.assertIndex("ce_queue", "ce_queue_entity_uuid", "entity_uuid");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest.java
new file mode 100644 (file)
index 0000000..b637bb3
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v102;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class CreateIndexEntityUuidInGroupRolesTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexEntityUuidInGroupRolesTest.class, "schema.sql");
+
+  private final CreateIndexEntityUuidInGroupRoles createIndex = new CreateIndexEntityUuidInGroupRoles(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist("group_roles", "group_roles_entity_uuid");
+
+    createIndex.execute();
+
+    db.assertIndex("group_roles", "group_roles_entity_uuid", "entity_uuid");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    createIndex.execute();
+    createIndex.execute();
+
+    db.assertIndex("group_roles", "group_roles_entity_uuid", "entity_uuid");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest.java
new file mode 100644 (file)
index 0000000..ed65c02
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v102;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class CreateIndexEntityUuidInUserRolesTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexEntityUuidInUserRolesTest.class, "schema.sql");
+
+  private final CreateIndexEntityUuidInUserRoles createIndex = new CreateIndexEntityUuidInUserRoles(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist("user_roles", "user_roles_entity_uuid");
+
+    createIndex.execute();
+
+    db.assertIndex("user_roles", "user_roles_entity_uuid", "entity_uuid");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    createIndex.execute();
+    createIndex.execute();
+
+    db.assertIndex("user_roles", "user_roles_entity_uuid", "entity_uuid");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest/schema.sql
new file mode 100644 (file)
index 0000000..153197b
--- /dev/null
@@ -0,0 +1,31 @@
+CREATE TABLE "CE_ACTIVITY"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "TASK_TYPE" CHARACTER VARYING(40) NOT NULL,
+    "ENTITY_UUID" CHARACTER VARYING(40),
+    "COMPONENT_UUID" CHARACTER VARYING(40),
+    "STATUS" CHARACTER VARYING(15) NOT NULL,
+    "MAIN_IS_LAST" BOOLEAN NOT NULL,
+    "MAIN_IS_LAST_KEY" CHARACTER VARYING(55) NOT NULL,
+    "IS_LAST" BOOLEAN NOT NULL,
+    "IS_LAST_KEY" CHARACTER VARYING(55) NOT NULL,
+    "SUBMITTER_UUID" CHARACTER VARYING(255),
+    "SUBMITTED_AT" BIGINT NOT NULL,
+    "STARTED_AT" BIGINT,
+    "EXECUTED_AT" BIGINT,
+    "EXECUTION_COUNT" INTEGER NOT NULL,
+    "EXECUTION_TIME_MS" BIGINT,
+    "ANALYSIS_UUID" CHARACTER VARYING(50),
+    "ERROR_MESSAGE" CHARACTER VARYING(1000),
+    "ERROR_STACKTRACE" CHARACTER LARGE OBJECT,
+    "ERROR_TYPE" CHARACTER VARYING(20),
+    "WORKER_UUID" CHARACTER VARYING(40),
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL,
+    "NODE_NAME" CHARACTER VARYING(100)
+);
+ALTER TABLE "CE_ACTIVITY" ADD CONSTRAINT "PK_CE_ACTIVITY" PRIMARY KEY("UUID");
+CREATE INDEX "CE_ACTIVITY_COMPONENT" ON "CE_ACTIVITY"("COMPONENT_UUID" NULLS FIRST);
+CREATE INDEX "CE_ACTIVITY_ISLAST" ON "CE_ACTIVITY"("IS_LAST" NULLS FIRST, "STATUS" NULLS FIRST);
+CREATE INDEX "CE_ACTIVITY_ISLAST_KEY" ON "CE_ACTIVITY"("IS_LAST_KEY" NULLS FIRST);
+CREATE INDEX "CE_ACTIVITY_MAIN_ISLAST" ON "CE_ACTIVITY"("MAIN_IS_LAST" NULLS FIRST, "STATUS" NULLS FIRST);
+CREATE INDEX "CE_ACTIVITY_MAIN_ISLAST_KEY" ON "CE_ACTIVITY"("MAIN_IS_LAST_KEY" NULLS FIRST);
\ No newline at end of file
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest/schema.sql
new file mode 100644 (file)
index 0000000..9968533
--- /dev/null
@@ -0,0 +1,15 @@
+CREATE TABLE "CE_QUEUE"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "TASK_TYPE" CHARACTER VARYING(40) NOT NULL,
+    "ENTITY_UUID" CHARACTER VARYING(40),
+    "COMPONENT_UUID" CHARACTER VARYING(40),
+    "STATUS" CHARACTER VARYING(15),
+    "SUBMITTER_UUID" CHARACTER VARYING(255),
+    "STARTED_AT" BIGINT,
+    "WORKER_UUID" CHARACTER VARYING(40),
+    "EXECUTION_COUNT" INTEGER NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "CE_QUEUE" ADD CONSTRAINT "PK_CE_QUEUE" PRIMARY KEY("UUID");
+CREATE INDEX "CE_QUEUE_COMPONENT" ON "CE_QUEUE"("COMPONENT_UUID" NULLS FIRST);
\ No newline at end of file
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest/schema.sql
new file mode 100644 (file)
index 0000000..6af8c34
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE "GROUP_ROLES"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "ROLE" CHARACTER VARYING(64) NOT NULL,
+    "ENTITY_UUID" CHARACTER VARYING(40),
+    "GROUP_UUID" CHARACTER VARYING(40)
+);
+ALTER TABLE "GROUP_ROLES" ADD CONSTRAINT "PK_GROUP_ROLES" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_GROUP_ROLES" ON "GROUP_ROLES"("GROUP_UUID" NULLS FIRST, "ENTITY_UUID" NULLS FIRST, "ROLE" NULLS FIRST);
\ No newline at end of file
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest/schema.sql
new file mode 100644 (file)
index 0000000..f5c5e45
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE "USER_ROLES"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "ROLE" CHARACTER VARYING(64) NOT NULL,
+    "ENTITY_UUID" CHARACTER VARYING(40),
+    "USER_UUID" CHARACTER VARYING(255)
+);
+ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID");
+CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_UUID" NULLS FIRST);
\ No newline at end of file
index 38a07f8df2b303363e96f2dd7e76c3568a3ac100..eeda6a0799e7ca76ab8cbb259b91b7a8908c4054 100644 (file)
@@ -49,7 +49,7 @@ public class Project {
   /**
    * Should use {@link org.sonar.server.project.Project#fromProjectDtoWithTags(org.sonar.db.project.ProjectDto)} instead
    */
-  @Deprecated
+  @Deprecated(since = "10.2")
   public static Project from(ComponentDto project) {
     return new Project(project.uuid(), project.getKey(), project.name(), project.description(), emptyList());
   }
index 8d075a46f95b79565f0fe1ba56b86874470e609d..8bebeae11276f4b2492f55036b548c0e05a4160c 100644 (file)
@@ -141,8 +141,7 @@ public class TagsAction implements IssuesWsAction {
       switch (entity.getQualifier()) {
         case Qualifiers.PROJECT -> issueQueryBuilder.projectUuids(Set.of(entity.getUuid()));
         case Qualifiers.VIEW, Qualifiers.APP -> issueQueryBuilder.viewUuids(Set.of(entity.getUuid()));
-        default ->
-          throw new IllegalArgumentException(String.format("Entity of type '%s' is not supported", entity.getQualifier()));
+        default -> throw new IllegalArgumentException(String.format("Entity of type '%s' is not supported", entity.getQualifier()));
       }
 
       if (branch != null && !branch.isMain()) {