From 2d2b4dcc3718bf994c2b5b72cbb6531da051ec4e Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 12 Dec 2017 09:59:24 +0100 Subject: [PATCH] SONAR-10147 Delete 'sonar.qualitygate' setting at global level --- .../db/migration/version/v70/DbVersion70.java | 1 + .../DeleteGlobalSonarQualityGateSetting.java | 45 ++++++++ .../version/v70/DbVersion70Test.java | 2 +- ...leteGlobalSonarQualityGateSettingTest.java | 101 ++++++++++++++++++ .../properties.sql | 11 ++ 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSetting.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest/properties.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java index 1f5217b79d6..1da53519c93 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java @@ -47,6 +47,7 @@ public class DbVersion70 implements DbVersion { .add(1917, "Populate ORG_QUALITY_GATES table", PopulateOrgQualityGates.class) .add(1918, "Populate default quality gate on organization", PopulateDefaultQualityGate.class) .add(1919, "Associate existing quality gates to default organization", AssociateQualityGatesToDefaultOrganization.class) + .add(1920, "Delete 'sonar.qualitygate' setting at global level", DeleteGlobalSonarQualityGateSetting.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSetting.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSetting.java new file mode 100644 index 00000000000..84a43556d53 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSetting.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +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.MassUpdate; + +public class DeleteGlobalSonarQualityGateSetting extends DataChange { + + public DeleteGlobalSonarQualityGateSetting(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from properties where prop_key=? and resource_id is null") + .setString(1, "sonar.qualitygate"); + massUpdate.update("delete from properties where id=?"); + massUpdate.execute((row, update) -> { + update.setLong(1, row.getLong(1)); + return true; + }); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java index 36bc0171a7f..628ce06a0db 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java @@ -35,7 +35,7 @@ public class DbVersion70Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 20); + verifyMigrationCount(underTest, 21); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest.java new file mode 100644 index 00000000000..a1c27d37ce4 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest.java @@ -0,0 +1,101 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +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.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class DeleteGlobalSonarQualityGateSettingTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DeleteGlobalSonarQualityGateSettingTest.class, "properties.sql"); + + private DataChange underTest = new DeleteGlobalSonarQualityGateSetting(db.database()); + + @Test + public void delete_sonar_quality_gate_setting() throws SQLException { + insertSetting("sonar.qualitygate", null); + insertSetting("sonar.qualitygate", 1L); + insertSetting("other", null); + insertSetting("other", 2L); + + underTest.execute(); + + assertSettings( + tuple("sonar.qualitygate", 1L), + tuple("other", null), + tuple("other", 2L)); + } + + @Test + public void migration_is_reentrant() throws SQLException { + insertSetting("sonar.qualitygate", null); + insertSetting("sonar.qualitygate", 1L); + + underTest.execute(); + assertSettings(tuple("sonar.qualitygate", 1L)); + + underTest.execute(); + assertSettings(tuple("sonar.qualitygate", 1L)); + } + + @Test + public void does_nothing_when_no_sonar_quality_gate_setting() throws SQLException { + insertSetting("other", null); + + underTest.execute(); + + assertSettings(tuple("other", null)); + } + + @Test + public void does_nothing_on_empty_table() throws SQLException { + underTest.execute(); + + assertSettings(); + } + + private void assertSettings(Tuple... expectedTuples) { + assertThat(db.select("SELECT PROP_KEY, RESOURCE_ID FROM PROPERTIES") + .stream() + .map(map -> new Tuple(map.get("PROP_KEY"), map.get("RESOURCE_ID"))) + .collect(Collectors.toList())) + .containsExactlyInAnyOrder(expectedTuples); + } + + private void insertSetting(String key, @Nullable Long componentId) { + db.executeInsert( + "properties", + "PROP_KEY", key, + "RESOURCE_ID", componentId, + "IS_EMPTY", false, + "CREATED_AT", 1000); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest/properties.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest/properties.sql new file mode 100644 index 00000000000..d84c238cd48 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest/properties.sql @@ -0,0 +1,11 @@ +CREATE TABLE "PROPERTIES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "PROP_KEY" VARCHAR(512) NOT NULL, + "RESOURCE_ID" INTEGER, + "USER_ID" INTEGER, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT +); +CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY"); -- 2.39.5