From: Duarte Meneses Date: Mon, 19 Jun 2023 20:36:35 +0000 (-0500) Subject: SONAR-19556 Fix DB migration and increase test coverage X-Git-Tag: 10.2.0.77647~614 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=08158412b69bab7eb3fbbc28ca584d121ba9fff3;p=sonarqube.git SONAR-19556 Fix DB migration and increase test coverage --- diff --git a/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeQueueImpl.java b/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeQueueImpl.java index 0a78e1a5804..3ec3c21e114 100644 --- a/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeQueueImpl.java +++ b/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeQueueImpl.java @@ -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; diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/CreateIndexOnColumn.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/CreateIndexOnColumn.java index 88557064bb3..fc73320b3d8 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/CreateIndexOnColumn.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/CreateIndexOnColumn.java @@ -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; diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRoles.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRoles.java index 1453651969b..daa0af83574 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRoles.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRoles.java @@ -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) { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameMainComponentUuidInCeActivity.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameMainComponentUuidInCeActivity.java index 2acbca0a99e..c41576a18d4 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameMainComponentUuidInCeActivity.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameMainComponentUuidInCeActivity.java @@ -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 index 00000000000..dde2a931208 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest.java @@ -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 index 00000000000..01e735560cb --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest.java @@ -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 index 00000000000..b637bb323ec --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest.java @@ -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 index 00000000000..ed65c024327 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest.java @@ -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 index 00000000000..153197bdebd --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeActivityTest/schema.sql @@ -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 index 00000000000..9968533f779 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInCeQueueTest/schema.sql @@ -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 index 00000000000..6af8c34c780 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInGroupRolesTest/schema.sql @@ -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 index 00000000000..f5c5e45fc29 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexEntityUuidInUserRolesTest/schema.sql @@ -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 diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java b/server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java index 38a07f8df2b..eeda6a0799e 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java @@ -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()); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/TagsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/TagsAction.java index 8d075a46f95..8bebeae1127 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/TagsAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/TagsAction.java @@ -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()) {