From 420af6c92dfb3d4c11f5ef83c71e78c8f6d72195 Mon Sep 17 00:00:00 2001 From: Michal Duda Date: Thu, 15 Apr 2021 17:27:37 +0200 Subject: [PATCH] SONAR-12323 clear GitHub endpoint setting at project level --- .../db/migration/version/v89/DbVersion89.java | 3 +- ...opGithubEndpointOnProjectLevelSetting.java | 38 ++++++ ...thubEndpointOnProjectLevelSettingTest.java | 108 ++++++++++++++++++ .../schema.sql | 12 ++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSetting.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest/schema.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java index 9637872efee..c9685b683b2 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java @@ -29,6 +29,7 @@ public class DbVersion89 implements DbVersion { registry .add(4400, "Add indices on columns 'type' and 'value' to 'new_code_periods' table", AddIndicesToNewCodePeriodTable.class) .add(4401, "Drop local webhooks", DropLocalWebhooks.class) - .add(4402, "Add Index on column 'main_branch_project_uuid' to 'components' table", AddMainBranchProjectUuidIndexToComponentTable.class); + .add(4402, "Add Index on column 'main_branch_project_uuid' to 'components' table", AddMainBranchProjectUuidIndexToComponentTable.class) + .add(4403, "Drop Github endpoint on project level setting", DropGithubEndpointOnProjectLevelSetting.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSetting.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSetting.java new file mode 100644 index 00000000000..a0310d7205c --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSetting.java @@ -0,0 +1,38 @@ +/* + * 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.v89; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; + +public class DropGithubEndpointOnProjectLevelSetting extends DataChange { + + public DropGithubEndpointOnProjectLevelSetting(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + context.prepareUpsert("delete from properties where prop_key='sonar.pullrequest.github.endpoint' and component_uuid is not null") + .execute() + .commit(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest.java new file mode 100644 index 00000000000..379b5e0dd75 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest.java @@ -0,0 +1,108 @@ +/* + * 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.v89; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.assertj.core.groups.Tuple; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.core.util.Uuids; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class DropGithubEndpointOnProjectLevelSettingTest { + private static final String DROPPED_PROPERTY = "sonar.pullrequest.github.endpoint"; + private static final String PROP_1_UUID = Uuids.createFast(); + private static final String PROP_2_UUID = Uuids.createFast(); + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropGithubEndpointOnProjectLevelSettingTest.class, "schema.sql"); + + private final MigrationStep underTest = new DropGithubEndpointOnProjectLevelSetting(db.database()); + + @Test + public void execute() throws SQLException { + setupProperties(); + + underTest.execute(); + + verifyResult(); + } + + @Test + public void is_reentrant() throws SQLException { + setupProperties(); + + underTest.execute(); + underTest.execute(); + + verifyResult(); + } + + @Test + public void is_successful_when_no_entries_to_drop() throws SQLException { + underTest.execute(); + + assertThat(db.select("SELECT UUID FROM PROPERTIES")).isEmpty(); + } + + private void setupProperties() { + insertProperty(Uuids.createFast(), "some.prop", "1"); + insertProperty(Uuids.createFast(), "some.other.prop", null); + insertProperty(PROP_1_UUID, DROPPED_PROPERTY, "1"); + insertProperty(PROP_2_UUID, DROPPED_PROPERTY, "2"); + insertProperty(Uuids.createFast(), DROPPED_PROPERTY, null); + } + + private void verifyResult() { + List> result = db.select("SELECT UUID, PROP_KEY, COMPONENT_UUID FROM PROPERTIES"); + assertThat(result + .stream() + .map(e -> new Tuple(e.get("PROP_KEY"), e.get("COMPONENT_UUID"))) + .collect(Collectors.toList())) + .containsExactlyInAnyOrder( + tuple("some.prop", "1"), + tuple("some.other.prop", null), + tuple(DROPPED_PROPERTY, null)); + assertThat(result) + .extracting(e -> e.get("UUID")) + .doesNotContain(PROP_1_UUID, PROP_2_UUID); + } + + private void insertProperty(String uuid, String key, @Nullable String projectUuid) { + db.executeInsert("PROPERTIES", + "UUID", uuid, + "PROP_KEY", key, + "COMPONENT_UUID", projectUuid, + "USER_UUID", null, + "IS_EMPTY", false, + "TEXT_VALUE", "AnyValue", + "CLOB_VALUE", null, + "CREATED_AT", System2.INSTANCE.now()); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest/schema.sql new file mode 100644 index 00000000000..dfe931f54d1 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/DropGithubEndpointOnProjectLevelSettingTest/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE "PROPERTIES"( + "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), + "UUID" VARCHAR(40) NOT NULL, + "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