From 63c1441080c51dbc2e390c3d826b2b8bcf73f10a Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Mon, 1 May 2023 15:22:23 +0200 Subject: [PATCH] SONAR-19085 Create 'external_groups' table. --- server/sonar-db-dao/src/schema/schema-sq.ddl | 7 ++ .../v101/CreateExternalGroupsTable.java | 54 +++++++++++++++ .../version/v101/CreateScmAccountsTable.java | 3 + .../migration/version/v101/DbVersion101.java | 3 +- .../v101/CreateExternalGroupsTableTest.java | 66 +++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTableTest.java diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 07f05419329..197259b24e8 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -311,6 +311,13 @@ ALTER TABLE "EVENTS" ADD CONSTRAINT "PK_EVENTS" PRIMARY KEY("UUID"); CREATE INDEX "EVENTS_ANALYSIS" ON "EVENTS"("ANALYSIS_UUID" NULLS FIRST); CREATE INDEX "EVENTS_COMPONENT_UUID" ON "EVENTS"("COMPONENT_UUID" NULLS FIRST); +CREATE TABLE "EXTERNAL_GROUPS"( + "GROUP_UUID" CHARACTER VARYING(40) NOT NULL, + "EXTERNAL_GROUP_ID" CHARACTER VARYING(255) NOT NULL, + "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL +); +ALTER TABLE "EXTERNAL_GROUPS" ADD CONSTRAINT "PK_EXTERNAL_GROUPS" PRIMARY KEY("GROUP_UUID"); + CREATE TABLE "FILE_SOURCES"( "UUID" CHARACTER VARYING(40) NOT NULL, "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTable.java new file mode 100644 index 00000000000..efd816d6036 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTable.java @@ -0,0 +1,54 @@ +/* + * 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.v101; + +import com.google.common.annotations.VisibleForTesting; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; +import org.sonar.server.platform.db.migration.step.CreateTableChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateExternalGroupsTable extends CreateTableChange { + + @VisibleForTesting + static final String TABLE_NAME = "external_groups"; + @VisibleForTesting + static final String GROUP_UUID_COLUMN_NAME = "group_uuid"; + @VisibleForTesting + static final String EXTERNAL_GROUP_ID_COLUMN_NAME = "external_group_id"; + @VisibleForTesting + static final String EXTERNAL_IDENTITY_PROVIDER_COLUMN_NAME = "external_identity_provider"; + + public CreateExternalGroupsTable(Database db) { + super(db, TABLE_NAME); + } + + @Override + public void execute(Context context, String tableName) throws SQLException { + context.execute(new CreateTableBuilder(getDialect(), tableName) + .addPkColumn(newVarcharColumnDefBuilder().setColumnName(GROUP_UUID_COLUMN_NAME).setIsNullable(false).setLimit(UUID_SIZE).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName(EXTERNAL_GROUP_ID_COLUMN_NAME).setIsNullable(false).setLimit(255).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName(EXTERNAL_IDENTITY_PROVIDER_COLUMN_NAME).setIsNullable(false).setLimit(100).build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateScmAccountsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateScmAccountsTable.java index 443b6899522..aab62cc409b 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateScmAccountsTable.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/CreateScmAccountsTable.java @@ -29,8 +29,11 @@ import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_U import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; class CreateScmAccountsTable extends CreateTableChange { + @VisibleForTesting static final String TABLE_NAME = "scm_accounts"; + @VisibleForTesting static final String USER_UUID_COLUMN_NAME = "user_uuid"; + @VisibleForTesting static final String SCM_ACCOUNT_COLUMN_NAME = "scm_account"; @VisibleForTesting diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java index 0ff5b82f439..f92e293bc9e 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java @@ -55,6 +55,7 @@ public class DbVersion101 implements DbVersion { .add(10_1_011, "Populate 'project_key' in 'user_tokens'", PopulateProjectUuidInUserTokens.class) .add(10_1_012, "Drop column 'project_key' in 'user_tokens", DropProjectKeyInUserTokens.class) .add(10_1_013, "Increase size of 'ce_queue.task_type' from 15 to 40 characters", IncreaseTaskTypeColumnSizeInCeQueue.class) - .add(10_1_014, "Increase size of 'ce_activity.task_type' from 15 to 40 characters", IncreaseTaskTypeColumnSizeInCeActivity.class); + .add(10_1_014, "Increase size of 'ce_activity.task_type' from 15 to 40 characters", IncreaseTaskTypeColumnSizeInCeActivity.class) + .add(10_1_015, "Add 'external_groups' table.", CreateExternalGroupsTable.class); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTableTest.java new file mode 100644 index 00000000000..04d3a88bb96 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/CreateExternalGroupsTableTest.java @@ -0,0 +1,66 @@ +/* + * 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.v101; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonar.server.platform.db.migration.version.v101.CreateExternalGroupsTable.EXTERNAL_GROUP_ID_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v101.CreateExternalGroupsTable.EXTERNAL_IDENTITY_PROVIDER_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v101.CreateExternalGroupsTable.GROUP_UUID_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v101.CreateExternalGroupsTable.TABLE_NAME; + +public class CreateExternalGroupsTableTest { + + @Rule + public final CoreDbTester db = CoreDbTester.createEmpty(); + + private final DdlChange createExternalGroupsTable = new CreateExternalGroupsTable(db.database()); + + @Test + public void migration_should_create_a_table() throws SQLException { + db.assertTableDoesNotExist(TABLE_NAME); + + createExternalGroupsTable.execute(); + + db.assertTableExists(TABLE_NAME); + db.assertColumnDefinition(TABLE_NAME, GROUP_UUID_COLUMN_NAME, Types.VARCHAR, UUID_SIZE, false); + db.assertColumnDefinition(TABLE_NAME, EXTERNAL_GROUP_ID_COLUMN_NAME, Types.VARCHAR, 255, false); + db.assertColumnDefinition(TABLE_NAME, EXTERNAL_IDENTITY_PROVIDER_COLUMN_NAME, Types.VARCHAR, 100, false); + db.assertPrimaryKey(TABLE_NAME, "pk_external_groups", GROUP_UUID_COLUMN_NAME); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + db.assertTableDoesNotExist(TABLE_NAME); + + createExternalGroupsTable.execute(); + // re-entrant + createExternalGroupsTable.execute(); + + db.assertTableExists(TABLE_NAME); + } + +} -- 2.39.5