From: Nolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com> Date: Wed, 21 Aug 2024 09:25:34 +0000 (+0200) Subject: SONAR-22807 Implement required DB migrations to rename github_perms_mapping table... X-Git-Tag: 10.7.0.96327~217 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=11eed2c0e23bc971bcd5f5a3eb03de4c64733228;p=sonarqube.git SONAR-22807 Implement required DB migrations to rename github_perms_mapping table to devops_perms_mapping --- diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index e2c92046c64..3378740caa0 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -269,6 +269,15 @@ ALTER TABLE "DEPRECATED_RULE_KEYS" ADD CONSTRAINT "PK_DEPRECATED_RULE_KEYS" PRIM CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS"("OLD_REPOSITORY_KEY" NULLS FIRST, "OLD_RULE_KEY" NULLS FIRST); CREATE INDEX "RULE_UUID_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS"("RULE_UUID" NULLS FIRST); +CREATE TABLE "DEVOPS_PERMS_MAPPING"( + "UUID" CHARACTER VARYING(40) NOT NULL, + "DEVOPS_PLATFORM_ROLE" CHARACTER VARYING(100) NOT NULL, + "SONARQUBE_PERMISSION" CHARACTER VARYING(64) NOT NULL, + "DEVOPS_PLATFORM" CHARACTER VARYING(40) DEFAULT 'github' NOT NULL +); +ALTER TABLE "DEVOPS_PERMS_MAPPING" ADD CONSTRAINT "PK_DEVOPS_PERMS_MAPPING" PRIMARY KEY("UUID"); +CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_DEVOPS_PERMS_MAPPING" ON "DEVOPS_PERMS_MAPPING"("DEVOPS_PLATFORM" NULLS FIRST, "DEVOPS_PLATFORM_ROLE" NULLS FIRST, "SONARQUBE_PERMISSION" NULLS FIRST); + CREATE TABLE "DUPLICATIONS_INDEX"( "UUID" CHARACTER VARYING(40) NOT NULL, "ANALYSIS_UUID" CHARACTER VARYING(50) NOT NULL, @@ -358,14 +367,6 @@ CREATE TABLE "GITHUB_ORGS_GROUPS"( ); ALTER TABLE "GITHUB_ORGS_GROUPS" ADD CONSTRAINT "PK_GITHUB_ORGS_GROUPS" PRIMARY KEY("GROUP_UUID"); -CREATE TABLE "GITHUB_PERMS_MAPPING"( - "UUID" CHARACTER VARYING(40) NOT NULL, - "GITHUB_ROLE" CHARACTER VARYING(100) NOT NULL, - "SONARQUBE_PERMISSION" CHARACTER VARYING(64) NOT NULL -); -ALTER TABLE "GITHUB_PERMS_MAPPING" ADD CONSTRAINT "PK_GITHUB_PERMS_MAPPING" PRIMARY KEY("UUID"); -CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_GITHUB_PERM_MAPPINGS" ON "GITHUB_PERMS_MAPPING"("GITHUB_ROLE" NULLS FIRST, "SONARQUBE_PERMISSION" NULLS FIRST); - CREATE TABLE "GROUP_ROLES"( "UUID" CHARACTER VARYING(40) NOT NULL, "ROLE" CHARACTER VARYING(64) NOT NULL, diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddDevopsPlatformColumnInDevopsPermsMappingIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddDevopsPlatformColumnInDevopsPermsMappingIT.java new file mode 100644 index 00000000000..0c9547dc723 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddDevopsPlatformColumnInDevopsPermsMappingIT.java @@ -0,0 +1,72 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import java.sql.SQLException; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; + +import static java.sql.Types.VARCHAR; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.sonar.server.platform.db.migration.version.v107.AddDevopsPlatformColumnInDevopsPermsMapping.DEVOPS_PLATFORM_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.AddDevopsPlatformColumnInDevopsPermsMapping.DEFAULT_COLUMN_VALUE; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +class AddDevopsPlatformColumnInDevopsPermsMappingIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddDevopsPlatformColumnInDevopsPermsMapping.class); + + private final AddDevopsPlatformColumnInDevopsPermsMapping underTest = new AddDevopsPlatformColumnInDevopsPermsMapping(db.database()); + + @Test + void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { + db.assertColumnDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, DEVOPS_PLATFORM_COLUMN_NAME); + underTest.execute(); + assertColumnExists(); + } + + @Test + void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { + underTest.execute(); + assertColumnExists(); + underTest.execute(); + } + + @Test + void execute_whenDataAlreadyExists_shouldCreateColumnWithDefaultValue() throws SQLException { + db.executeInsert(DEVOPS_PERMS_MAPPING_TABLE_NAME, "uuid", "UUID", "devops_platform_role", "uniqAdmin", "sonarqube_permission", "uniqPermission"); + underTest.execute(); + assertDevopsPlatformColumnSetToDefault(); + assertColumnExists(); + } + + private void assertDevopsPlatformColumnSetToDefault() { + Map selectResult = db.selectFirst("select devops_platform from devops_perms_mapping where uuid = 'UUID'"); + assertThat(selectResult.get("devops_platform")).isEqualTo(DEFAULT_COLUMN_VALUE); + } + + private void assertColumnExists() { + db.assertColumnDefinition(DEVOPS_PERMS_MAPPING_TABLE_NAME, DEVOPS_PLATFORM_COLUMN_NAME, VARCHAR, 40, false); + } + +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyConstraintOnDevopsPermsMappingTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyConstraintOnDevopsPermsMappingTableIT.java new file mode 100644 index 00000000000..7ebc59c99ee --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyConstraintOnDevopsPermsMappingTableIT.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; + +import static org.sonar.server.platform.db.migration.version.v107.CreatePrimaryKeyConstraintOnDevopsPermsMappingTable.UUID_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +class CreatePrimaryKeyConstraintOnDevopsPermsMappingTableIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreatePrimaryKeyConstraintOnDevopsPermsMappingTable.class); + private final CreatePrimaryKeyConstraintOnDevopsPermsMappingTable underTest = new CreatePrimaryKeyConstraintOnDevopsPermsMappingTable(db.database()); + private static final String PK_DEVOPS_PERMS_MAPPING = "pk_devops_perms_mapping"; + + @Test + void execute_whenPrimaryKeyDoesNotExist_shouldCreateIt() throws Exception { + db.assertNoPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME); + underTest.execute(); + db.assertPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME, PK_DEVOPS_PERMS_MAPPING, UUID_COLUMN_NAME); + } + + @Test + void execute_whenPrimaryKeyAlreadyExists_shouldDoNothing() throws Exception { + underTest.execute(); + underTest.execute(); + db.assertPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME, PK_DEVOPS_PERMS_MAPPING, UUID_COLUMN_NAME); + } + +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateUniqueIndexOnDevopsPermsMappingTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateUniqueIndexOnDevopsPermsMappingTableIT.java new file mode 100644 index 00000000000..17798921108 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateUniqueIndexOnDevopsPermsMappingTableIT.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.AddDevopsPlatformColumnInDevopsPermsMapping.DEVOPS_PLATFORM_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.CreateUniqueIndexOnDevopsPermsMappingTable.DEVOPS_ROLE_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.CreateUniqueIndexOnDevopsPermsMappingTable.INDEX_NAME; +import static org.sonar.server.platform.db.migration.version.v107.CreateUniqueIndexOnDevopsPermsMappingTable.SONARQUBE_PERMISSION_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +class CreateUniqueIndexOnDevopsPermsMappingTableIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreateUniqueIndexOnDevopsPermsMappingTable.class); + private final DdlChange underTest = new CreateUniqueIndexOnDevopsPermsMappingTable(db.database()); + + @Test + void execute_givenIndexDoNotExist_shouldCreateIndex() throws Exception { + db.assertIndexDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, INDEX_NAME); + underTest.execute(); + db.assertUniqueIndex(DEVOPS_PERMS_MAPPING_TABLE_NAME, INDEX_NAME, DEVOPS_ROLE_COLUMN_NAME, SONARQUBE_PERMISSION_COLUMN_NAME, DEVOPS_PLATFORM_COLUMN_NAME); + } + + @Test + void execute_givenIndexAlreadyExist_shouldBeReentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + db.assertUniqueIndex(DEVOPS_PERMS_MAPPING_TABLE_NAME, INDEX_NAME, DEVOPS_ROLE_COLUMN_NAME, SONARQUBE_PERMISSION_COLUMN_NAME, DEVOPS_PLATFORM_COLUMN_NAME); + } + +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropGithubPermsMappingTableIfDevopsPermsMappingTableExistsIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropGithubPermsMappingTableIfDevopsPermsMappingTableExistsIT.java new file mode 100644 index 00000000000..dca7661cfbe --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropGithubPermsMappingTableIfDevopsPermsMappingTableExistsIT.java @@ -0,0 +1,85 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.GITHUB_PERMS_MAPPING_TABLE_NAME; + +class DropGithubPermsMappingTableIfDevopsPermsMappingTableExistsIT { + + @RegisterExtension + public static MigrationDbTester db = MigrationDbTester.createForMigrationStep(DropGithubPermsMappingTableIfDevopsPermsMappingTableExists.class); + + private final DropGithubPermsMappingTableIfDevopsPermsMappingTableExists underTest = new DropGithubPermsMappingTableIfDevopsPermsMappingTableExists(db.database()); + + @Test + void execute_givenGithubDevopsPermsMappingTableExistsAndDevopsPermsMappingTableDoesNotExist_doNothing() throws SQLException { + db.assertTableExists(GITHUB_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME); + + underTest.execute(); + + db.assertTableExists(GITHUB_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME); + } + + @Test + void execute_givenDevopsPermsMappingTableExistsAndGithubPermsMappingTableDoNotExist_doNothing() throws SQLException { + db.executeDdl("CREATE TABLE " + DEVOPS_PERMS_MAPPING_TABLE_NAME + " (id INT PRIMARY KEY)"); + db.executeDdl("DROP TABLE " + GITHUB_PERMS_MAPPING_TABLE_NAME); + db.assertTableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + + underTest.execute(); + + db.assertTableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + } + + @Test + void execute_givenDevopsPermsMappingTableExistsAndGithubPermsMappingTableExist_dropGithubPermsMappingTable() throws SQLException { + db.executeDdl("CREATE TABLE " + DEVOPS_PERMS_MAPPING_TABLE_NAME + " (id INT PRIMARY KEY)"); + db.assertTableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME); + db.assertTableExists(GITHUB_PERMS_MAPPING_TABLE_NAME); + + underTest.execute(); + + db.assertTableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + } + + @Test + void execute_givenDevopsPermsMappingTableDoesNotExistsAndGithubPermsMappingTableDoesNotExist_doNothing() throws SQLException { + db.executeDdl("DROP TABLE " + GITHUB_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + + underTest.execute(); + + db.assertTableDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + } + +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropIndexUniqGithubPermsMappingInDevopsPermsMappingTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropIndexUniqGithubPermsMappingInDevopsPermsMappingTableIT.java new file mode 100644 index 00000000000..d10c1140aa2 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropIndexUniqGithubPermsMappingInDevopsPermsMappingTableIT.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable.UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +class DropIndexUniqGithubPermsMappingInDevopsPermsMappingTableIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable.class); + private final DdlChange underTest = new DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable(db.database()); + + @Test + void execute_givenIndexExists_dropsIndex() throws Exception { + db.assertUniqueIndex(DEVOPS_PERMS_MAPPING_TABLE_NAME, UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME, "devops_platform_role", "sonarqube_permission"); + underTest.execute(); + db.assertIndexDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME); + } + + @Test + void execute_is_reentrant() throws Exception { + db.assertUniqueIndex(DEVOPS_PERMS_MAPPING_TABLE_NAME, UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME, "devops_platform_role", "sonarqube_permission"); + underTest.execute(); + underTest.execute(); + db.assertIndexDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME); + } + +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropPrimaryKeyOnDevopsPermsMappingTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropPrimaryKeyOnDevopsPermsMappingTableIT.java new file mode 100644 index 00000000000..c0670cdb916 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/DropPrimaryKeyOnDevopsPermsMappingTableIT.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder; +import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator; + +import static org.sonar.server.platform.db.migration.version.v107.DropPrimaryKeyOnDevopsPermsMappingTable.CONSTRAINT_NAME; +import static org.sonar.server.platform.db.migration.version.v107.DropPrimaryKeyOnDevopsPermsMappingTable.UUID_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +class DropPrimaryKeyOnDevopsPermsMappingTableIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(DropPrimaryKeyOnDevopsPermsMappingTable.class); + + private final DbPrimaryKeyConstraintFinder dbPrimaryKeyConstraintFinder = new DbPrimaryKeyConstraintFinder(db.database()); + private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), dbPrimaryKeyConstraintFinder); + private final DropPrimaryKeyOnDevopsPermsMappingTable underTest = new DropPrimaryKeyOnDevopsPermsMappingTable(db.database(), dropPrimaryKeySqlGenerator, dbPrimaryKeyConstraintFinder); + + @Test + void execute_shouldRemoveExistingPrimaryKey() throws Exception { + db.assertPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME, CONSTRAINT_NAME, UUID_COLUMN_NAME); + underTest.execute(); + db.assertNoPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME); + } + + @Test + void execute_when_reentrant_shouldRemoveExistingPrimaryKey() throws Exception { + db.assertPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME, CONSTRAINT_NAME, UUID_COLUMN_NAME); + underTest.execute(); + underTest.execute(); + db.assertNoPrimaryKey(DEVOPS_PERMS_MAPPING_TABLE_NAME); + } + +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubPermsMappingTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubPermsMappingTableIT.java new file mode 100644 index 00000000000..2e39000d345 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubPermsMappingTableIT.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.RenameTableChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.GITHUB_PERMS_MAPPING_TABLE_NAME; + +class RenameGithubPermsMappingTableIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(RenameGithubPermsMappingTable.class); + private final RenameTableChange underTest = new RenameGithubPermsMappingTable(db.database()); + + @Test + void migration_shouldUpdateTableName() throws SQLException { + db.assertTableExists(GITHUB_PERMS_MAPPING_TABLE_NAME); + underTest.execute(); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + db.assertTableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME); + } + + @Test + void migration_is_reentrant() throws SQLException { + db.assertTableExists(GITHUB_PERMS_MAPPING_TABLE_NAME); + underTest.execute(); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + underTest.execute(); + db.assertTableDoesNotExist(GITHUB_PERMS_MAPPING_TABLE_NAME); + db.assertTableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubRoleInDevopsPermsMappingIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubRoleInDevopsPermsMappingIT.java new file mode 100644 index 00000000000..881a014b06b --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubRoleInDevopsPermsMappingIT.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; + +import static java.sql.Types.VARCHAR; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubRoleInDevopsPermsMapping.OLD_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubRoleInDevopsPermsMapping.NEW_COLUMN_NAME; + +class RenameGithubRoleInDevopsPermsMappingIT { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(RenameGithubRoleInDevopsPermsMapping.class); + + private final RenameGithubRoleInDevopsPermsMapping underTest = new RenameGithubRoleInDevopsPermsMapping(db.database()); + + @Test + void execute_renameColumn() throws SQLException { + db.assertColumnDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, NEW_COLUMN_NAME); + db.assertColumnDefinition(DEVOPS_PERMS_MAPPING_TABLE_NAME, OLD_COLUMN_NAME, VARCHAR, 100, false); + underTest.execute(); + db.assertColumnDefinition(DEVOPS_PERMS_MAPPING_TABLE_NAME, NEW_COLUMN_NAME, VARCHAR, 100, false); + db.assertColumnDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, OLD_COLUMN_NAME); + } + + @Test + void execute_whenRunTwice_isReentrant() throws SQLException { + db.assertColumnDoesNotExist(DEVOPS_PERMS_MAPPING_TABLE_NAME, NEW_COLUMN_NAME); + underTest.execute(); + underTest.execute(); + db.assertColumnDefinition(DEVOPS_PERMS_MAPPING_TABLE_NAME, NEW_COLUMN_NAME, VARCHAR, 100, false); + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/RenameTableChange.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/RenameTableChange.java new file mode 100644 index 00000000000..006ec84cf0c --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/RenameTableChange.java @@ -0,0 +1,57 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.step; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.RenameTableBuilder; + +public abstract class RenameTableChange extends DdlChange { + + protected final String tableName; + protected final String newTableName; + + protected RenameTableChange(Database db, String oldTableName, String newTableName) { + super(db); + this.tableName = oldTableName; + this.newTableName = newTableName; + } + @Override + public void execute(Context context) throws SQLException { + if (tableExists()) { + renameTable(context); + } + } + + private boolean tableExists() throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.tableExists(tableName, connection); + } + } + + private void renameTable(Context context) { + RenameTableBuilder renameTableBuilder = new RenameTableBuilder(getDialect()); + renameTableBuilder.setName(tableName); + renameTableBuilder.setNewName(newTableName); + renameTableBuilder.setAutoGeneratedId(false); + context.execute(renameTableBuilder.build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddDevopsPlatformColumnInDevopsPermsMapping.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddDevopsPlatformColumnInDevopsPermsMapping.java new file mode 100644 index 00000000000..020cf643c6d --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddDevopsPlatformColumnInDevopsPermsMapping.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import com.google.common.annotations.VisibleForTesting; +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.def.ColumnDef; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +public class AddDevopsPlatformColumnInDevopsPermsMapping extends DdlChange { + + static final String DEVOPS_PLATFORM_COLUMN_NAME = "devops_platform"; + @VisibleForTesting + static final String DEFAULT_COLUMN_VALUE = "github"; + + public AddDevopsPlatformColumnInDevopsPermsMapping(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + if (!DatabaseUtils.tableColumnExists(connection, DEVOPS_PERMS_MAPPING_TABLE_NAME, DEVOPS_PLATFORM_COLUMN_NAME)) { + ColumnDef columnDef = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName(DEVOPS_PLATFORM_COLUMN_NAME) + .setLimit(40) + .setIsNullable(false) + .setDefaultValue(DEFAULT_COLUMN_VALUE) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), DEVOPS_PERMS_MAPPING_TABLE_NAME) + .addColumn(columnDef) + .build()); + } + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyConstraintOnDevopsPermsMappingTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyConstraintOnDevopsPermsMappingTable.java new file mode 100644 index 00000000000..16326ffc0c2 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyConstraintOnDevopsPermsMappingTable.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import com.google.common.annotations.VisibleForTesting; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.sql.AddPrimaryKeyBuilder; +import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +public class CreatePrimaryKeyConstraintOnDevopsPermsMappingTable extends DdlChange { + + @VisibleForTesting + static final String UUID_COLUMN_NAME = "uuid"; + + public CreatePrimaryKeyConstraintOnDevopsPermsMappingTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + createPrimaryKey(context); + } + + private void createPrimaryKey(Context context) throws SQLException { + boolean pkExists = new DbPrimaryKeyConstraintFinder(getDatabase()).findConstraintName(DEVOPS_PERMS_MAPPING_TABLE_NAME).isPresent(); + if (!pkExists) { + context.execute(new AddPrimaryKeyBuilder(DEVOPS_PERMS_MAPPING_TABLE_NAME, UUID_COLUMN_NAME).build()); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateUniqueIndexOnDevopsPermsMappingTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateUniqueIndexOnDevopsPermsMappingTable.java new file mode 100644 index 00000000000..70d41b25530 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateUniqueIndexOnDevopsPermsMappingTable.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import com.google.common.annotations.VisibleForTesting; +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.AddDevopsPlatformColumnInDevopsPermsMapping.DEVOPS_PLATFORM_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +public class CreateUniqueIndexOnDevopsPermsMappingTable extends DdlChange { + + @VisibleForTesting + static final String DEVOPS_ROLE_COLUMN_NAME = "devops_platform_role"; + @VisibleForTesting + static final String SONARQUBE_PERMISSION_COLUMN_NAME = "sonarqube_permission"; + @VisibleForTesting + static final String INDEX_NAME = "uniq_devops_perms_mapping"; + + public CreateUniqueIndexOnDevopsPermsMappingTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + createUserUuidUniqueIndex(context, connection); + } + } + + private void createUserUuidUniqueIndex(DdlChange.Context context, Connection connection) { + if (!DatabaseUtils.indexExistsIgnoreCase(DEVOPS_PERMS_MAPPING_TABLE_NAME, INDEX_NAME, connection)) { + context.execute(new CreateIndexBuilder(getDialect()) + .setTable(DEVOPS_PERMS_MAPPING_TABLE_NAME) + .setName(INDEX_NAME) + .addColumn(DEVOPS_PLATFORM_COLUMN_NAME, false) + .addColumn(DEVOPS_ROLE_COLUMN_NAME, false) + .addColumn(SONARQUBE_PERMISSION_COLUMN_NAME, false) + .setUnique(true) + .build()); + } + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java index 2b835ed9ade..f70a46a99d4 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java @@ -42,7 +42,16 @@ public class DbVersion107 implements DbVersion { registry .add(10_7_000, "Create 'telemetry_metrics_sent' table", CreateTelemetryMetricsSentTable.class) .add(10_7_001, "sonar.auth.gitlab.userConsentForPermissionProvisioningRequired", AddUserConsentRequiredIfGitlabAutoProvisioningEnabled.class) - .add(10_7_002, "Migrate SMTP configuration into internal_properties", MigrateSmtpConfiguration.class); + .add(10_7_002, "Migrate SMTP configuration into internal_properties", MigrateSmtpConfiguration.class) + .add(10_7_003, "Drop 'github_perms_mapping' table if exists and 'devops_perms_mapping' table exists", DropGithubPermsMappingTableIfDevopsPermsMappingTableExists.class) + .add(10_7_004, "Rename 'github_perms_mapping' table to 'devops_perms_mapping'", RenameGithubPermsMappingTable.class) + .add(10_7_005, "Rename 'github_role' column to 'devops_platform_role' in devops_perms_mapping", RenameGithubRoleInDevopsPermsMapping.class) + .add(10_7_006, "Add 'devops_platform' column to 'devops_perms_mapping' table", AddDevopsPlatformColumnInDevopsPermsMapping.class) + .add(10_7_007, "Drop constraint on 'uuid' for 'devops_perms_mapping' table", DropPrimaryKeyOnDevopsPermsMappingTable.class) + .add(10_7_008, "Create primary key on 'devops_perms_mapping.uuid'", CreatePrimaryKeyConstraintOnDevopsPermsMappingTable.class) + .add(10_7_009, "Drop index 'uniq_github_perm_mappings' in the 'devops_perms_mapping' table", DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable.class) + .add(10_7_010, "Create uniq index on 'devops_perms_mapping' table for columns 'devops_platform_role', 'sonarqube_permission' and 'devops_platform'", + CreateUniqueIndexOnDevopsPermsMappingTable.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropGithubPermsMappingTableIfDevopsPermsMappingTableExists.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropGithubPermsMappingTableIfDevopsPermsMappingTableExists.java new file mode 100644 index 00000000000..322c35562da --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropGithubPermsMappingTableIfDevopsPermsMappingTableExists.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.DropTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.GITHUB_PERMS_MAPPING_TABLE_NAME; + +/** + * Migration should be reentrant. + * If migration is rerun from version 103, a new table github_perms_mapping will be created while the table devops_perms_mapping will exist, + * the rename from github_perms_mapping to devops_perms_mapping will fail and we will be in an inconstant state with the two tables. + * To avoid this state, we need to drop the table github_perms_mapping if the table devops_perms_mapping exists. + */ +public class DropGithubPermsMappingTableIfDevopsPermsMappingTableExists extends DdlChange { + + public DropGithubPermsMappingTableIfDevopsPermsMappingTableExists(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + boolean devopsPermsMappingTableExists = DatabaseUtils.tableExists(DEVOPS_PERMS_MAPPING_TABLE_NAME, connection); + boolean githubPermsMappingTableExists = DatabaseUtils.tableExists(GITHUB_PERMS_MAPPING_TABLE_NAME, connection); + if (devopsPermsMappingTableExists && githubPermsMappingTableExists) { + context.execute(new DropTableBuilder(getDialect(), GITHUB_PERMS_MAPPING_TABLE_NAME).build()); + } + } + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable.java new file mode 100644 index 00000000000..91e88209b58 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import com.google.common.annotations.VisibleForTesting; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DropIndexChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +public class DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable extends DropIndexChange { + + @VisibleForTesting + static final String UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME = "uniq_github_perm_mappings"; + + public DropIndexUniqGithubPermsMappingInDevopsPermsMappingTable(Database db) { + super(db, UNIQ_GITHUB_PERM_MAPPINGS_INDEX_NAME, DEVOPS_PERMS_MAPPING_TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropPrimaryKeyOnDevopsPermsMappingTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropPrimaryKeyOnDevopsPermsMappingTable.java new file mode 100644 index 00000000000..6eb6aa7a803 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DropPrimaryKeyOnDevopsPermsMappingTable.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import com.google.common.annotations.VisibleForTesting; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import java.util.Optional; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder; +import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +public class DropPrimaryKeyOnDevopsPermsMappingTable extends DdlChange { + + @VisibleForTesting + static final String UUID_COLUMN_NAME = "uuid"; + @VisibleForTesting + static final String CONSTRAINT_NAME = "pk_github_perms_mapping"; + + private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator; + private final DbPrimaryKeyConstraintFinder dbConstraintFinder; + + public DropPrimaryKeyOnDevopsPermsMappingTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator, DbPrimaryKeyConstraintFinder dbConstraintFinder) { + super(db); + this.dropPrimaryKeySqlGenerator = dropPrimaryKeySqlGenerator; + this.dbConstraintFinder = dbConstraintFinder; + } + + @Override + public void execute(Context context) throws SQLException { + Optional constraintName = dbConstraintFinder.findConstraintName(DEVOPS_PERMS_MAPPING_TABLE_NAME); + if (constraintName.isPresent() && constraintName.get().equalsIgnoreCase(CONSTRAINT_NAME)) { + List statements = dropPrimaryKeySqlGenerator.generate(DEVOPS_PERMS_MAPPING_TABLE_NAME, UUID_COLUMN_NAME, false); + context.execute(statements); + } + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubPermsMappingTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubPermsMappingTable.java new file mode 100644 index 00000000000..af0f6276980 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubPermsMappingTable.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.RenameTableChange; + +public class RenameGithubPermsMappingTable extends RenameTableChange { + + static final String DEVOPS_PERMS_MAPPING_TABLE_NAME = "devops_perms_mapping"; + + static final String GITHUB_PERMS_MAPPING_TABLE_NAME = "github_perms_mapping"; + + protected RenameGithubPermsMappingTable(Database db) { + super(db, GITHUB_PERMS_MAPPING_TABLE_NAME, DEVOPS_PERMS_MAPPING_TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubRoleInDevopsPermsMapping.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubRoleInDevopsPermsMapping.java new file mode 100644 index 00000000000..b5b460b3ef9 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/RenameGithubRoleInDevopsPermsMapping.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v107; + +import com.google.common.annotations.VisibleForTesting; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.RenameVarcharColumnChange; + +import static org.sonar.server.platform.db.migration.version.v107.RenameGithubPermsMappingTable.DEVOPS_PERMS_MAPPING_TABLE_NAME; + +public class RenameGithubRoleInDevopsPermsMapping extends RenameVarcharColumnChange { + + @VisibleForTesting + static final String OLD_COLUMN_NAME = "github_role"; + @VisibleForTesting + static final String NEW_COLUMN_NAME = "devops_platform_role"; + + public RenameGithubRoleInDevopsPermsMapping(Database db) { + super(db, DEVOPS_PERMS_MAPPING_TABLE_NAME, OLD_COLUMN_NAME, NEW_COLUMN_NAME); + } +}