From dca5b9d2a63c11a5a73eebeaad5e1e9a2d5445b1 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 27 Sep 2021 15:41:16 +0200 Subject: [PATCH] SONAR-15451 migrate existing team restriction from auth plugin --- .../db/migration/version/v92/DbVersion92.java | 1 + ...ketOrgPluginSettingsToBuiltInSettings.java | 41 +++++++++ .../version/v92/DbVersion92Test.java | 4 +- ...rgPluginSettingsToBuiltInSettingsTest.java | 90 +++++++++++++++++++ .../schema.sql | 12 +++ 5 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettings.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest/schema.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java index 64d0ed85f5f..32edf781106 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java @@ -27,6 +27,7 @@ public class DbVersion92 implements DbVersion { public void addSteps(MigrationStepRegistry registry) { registry .add(6101, "Change size of column 'selection_expression' in 'Portfolios'", AlterSelectionExpressionInPortfoliosTable.class) + .add(6102, "Migrate Bitbucket.org authentication plugin settings to built-in authentication settings", MigrateBibucketOrgPluginSettingsToBuiltInSettings.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettings.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettings.java new file mode 100644 index 00000000000..a828641b659 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettings.java @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.Upsert; + +public class MigrateBibucketOrgPluginSettingsToBuiltInSettings extends DataChange { + + public MigrateBibucketOrgPluginSettingsToBuiltInSettings(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + Upsert upsert = context.prepareUpsert("update properties set prop_key = ? where prop_key = ?"); + upsert.setString(1, "sonar.auth.bitbucket.workspaces"); + upsert.setString(2, "sonar.auth.bitbucket.teams"); + upsert.execute(); + upsert.commit(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java index cb68553e66a..9fafa892718 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java @@ -22,7 +22,7 @@ package org.sonar.server.platform.db.migration.version.v92; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount; +import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationNotEmpty; import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; public class DbVersion92Test { @@ -41,7 +41,7 @@ public class DbVersion92Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 1); + verifyMigrationNotEmpty(underTest); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest.java new file mode 100644 index 00000000000..2ec674f2c83 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest.java @@ -0,0 +1,90 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest { + + private final UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest.class, "schema.sql"); + + private final DataChange underTest = new MigrateBibucketOrgPluginSettingsToBuiltInSettings(db.database()); + + @Test + public void migration_populate_new_property_based_on_plugin_property() throws SQLException { + insertProperty("sonar.auth.bitbucket.teams", "restriction-value"); + insertProperty("another.property.not.impacted.by.migration", "some value"); + + underTest.execute(); + + assertPropertyMigratedCorrectly(); + } + + @Test + public void migration_dont_fail_if_plugin_property_is_missing() throws SQLException { + underTest.execute(); + + String selectSql = "select count(*) as COUNT from properties where PROP_KEY='sonar.auth.bitbucket.workspaces'"; + assertThat(db.select(selectSql).stream().map(row -> row.get("COUNT")).collect(Collectors.toList())) + .containsExactlyInAnyOrder(0L); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + insertProperty("sonar.auth.bitbucket.teams", "restriction-value"); + insertProperty("another.property.not.impacted.by.migration", "some value"); + + underTest.execute(); + // re-entrant + underTest.execute(); + + assertPropertyMigratedCorrectly(); + } + + private void assertPropertyMigratedCorrectly() { + String selectSql = "select TEXT_VALUE from properties where PROP_KEY='sonar.auth.bitbucket.workspaces'"; + assertThat(db.select(selectSql).stream().map(row -> row.get("TEXT_VALUE")).collect(Collectors.toList())) + .containsExactlyInAnyOrder("restriction-value"); + } + + private void insertProperty(String key, String value) { + Map map = new HashMap<>(); + map.put("UUID", uuidFactory.create()); + map.put("PROP_KEY", key); + map.put("IS_EMPTY", false); + map.put("TEXT_VALUE", value); + map.put("CREATED_AT", System.currentTimeMillis()); + db.executeInsert("properties", map); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest/schema.sql new file mode 100644 index 00000000000..1fd8dba4625 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/MigrateBibucketOrgPluginSettingsToBuiltInSettingsTest/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE "PROPERTIES"( + "UUID" VARCHAR(40) NOT NULL, + "PROP_KEY" VARCHAR(512) NOT NULL, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "USER_UUID" VARCHAR(255) +); +ALTER TABLE "PROPERTIES" ADD CONSTRAINT "PK_PROPERTIES" PRIMARY KEY("UUID"); +CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES"("PROP_KEY"); -- 2.39.5