]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10147 Delete 'sonar.qualitygate' setting at global level
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 12 Dec 2017 08:59:24 +0000 (09:59 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSetting.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/DeleteGlobalSonarQualityGateSettingTest/properties.sql [new file with mode: 0644]

index 1f5217b79d6d380a901d0188929924ad5fc779d3..1da53519c939b13e09afebaf2d475ec46e3c447f 100644 (file)
@@ -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 (file)
index 0000000..84a4355
--- /dev/null
@@ -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;
+    });
+  }
+
+}
index 36bc0171a7f782b0e759ea94e69d2aa54e679d70..628ce06a0dbc77fd6d068803288777973d52aae1 100644 (file)
@@ -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 (file)
index 0000000..a1c27d3
--- /dev/null
@@ -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 (file)
index 0000000..d84c238
--- /dev/null
@@ -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");