]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10087 Populate built-in flag on existing quality gates
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 21 Nov 2017 14:33:39 +0000 (15:33 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 4 Dec 2017 12:44:55 +0000 (13:44 +0100)
15 files changed:
server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
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/MakeQualityGatesIsBuiltInNotNullable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltIn.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest.java
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/MakeQualityGatesIsBuiltInNotNullableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltInTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest/quality_gates.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest/quality_gates_7_0.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullableTest/quality_gates.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltInTest/quality_gates.sql [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateUpdaterTest.java

index 733e8d5f09d64f4d6c5f9585b95b74f5ad448cc5..9ad599b91fda96b4fa3c8c4d1598d5dcc7c678b6 100644 (file)
@@ -229,7 +229,7 @@ CREATE UNIQUE INDEX "EVENTS_UUID" ON "EVENTS" ("UUID");
 CREATE TABLE "QUALITY_GATES" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
   "NAME" VARCHAR(100) NOT NULL,
-  "IS_BUILT_IN" BOOLEAN NULL,
+  "IS_BUILT_IN" BOOLEAN NOT NULL,
   "CREATED_AT" TIMESTAMP,
   "UPDATED_AT" TIMESTAMP,
 );
index 48c677f007e30300eb225bbef7ff3ae0ce4fb953..b8bae431e0575080631cb02a1291ad838e48b8e2 100644 (file)
@@ -27,7 +27,10 @@ public class DbVersion70 implements DbVersion {
   @Override
   public void addSteps(MigrationStepRegistry registry) {
     registry
-      .add(1900, "Add QUALITY_GATES.IS_BUILT_IN", AddIsBuiltInToQualityGates.class);
+      .add(1900, "Add QUALITY_GATES.IS_BUILT_IN", AddIsBuiltInToQualityGates.class)
+      .add(1901, "Populate QUALITY_GATES.IS_BUILT_IN", PopulateQualityGatesIsBuiltIn.class)
+      .add(1902, "Make QUALITY_GATES.IS_BUILT_IN not null", MakeQualityGatesIsBuiltInNotNullable.class)
+    ;
   }
 
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullable.java
new file mode 100644 (file)
index 0000000..723a93e
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * 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.def.BooleanColumnDef;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder;
+
+public class MakeQualityGatesIsBuiltInNotNullable extends DdlChange {
+
+  public MakeQualityGatesIsBuiltInNotNullable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    BooleanColumnDef column = newBooleanColumnDefBuilder()
+      .setColumnName("is_built_in")
+      .setIsNullable(false)
+      .build();
+
+    context.execute(new AlterColumnsBuilder(getDialect(), "quality_gates")
+      .updateColumn(column)
+      .build());
+  }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltIn.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltIn.java
new file mode 100644 (file)
index 0000000..b2a4480
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.Date;
+import org.sonar.api.utils.System2;
+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 PopulateQualityGatesIsBuiltIn extends DataChange {
+
+  private static final String SONARQUBE_WAY_QUALITY_GATE = "SonarQube way";
+
+  private final System2 system2;
+
+  public PopulateQualityGatesIsBuiltIn(Database db, System2 system2) {
+    super(db);
+    this.system2 = system2;
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("select id, name from quality_gates where is_built_in is null");
+    massUpdate.rowPluralName("quality_gates");
+    massUpdate.update("update quality_gates set is_built_in=?, updated_at=? where id=?");
+    massUpdate.execute((row, update) -> {
+      String name = row.getString(2);
+      update.setBoolean(1, SONARQUBE_WAY_QUALITY_GATE.equals(name));
+      update.setDate(2, new Date(system2.now()));
+      update.setLong(3, row.getLong(1));
+      return true;
+    });
+  }
+
+}
index 3cdfb04724b965fe691fed506479674131490cd1..f8d66df8e4c032c0c0dde8c8cad7b8a4f5d056e9 100644 (file)
@@ -30,7 +30,7 @@ import static java.sql.Types.BOOLEAN;
 public class AddIsBuiltInToQualityGatesTest {
 
   @Rule
-  public final CoreDbTester dbTester = CoreDbTester.createForSchema(AddIsBuiltInToQualityGatesTest.class, "quality_gates_7_0.sql");
+  public final CoreDbTester dbTester = CoreDbTester.createForSchema(AddIsBuiltInToQualityGatesTest.class, "quality_gates.sql");
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
index f28d675304d9116bb03778bd8674d30f75a3eff8..4daa8d55926a47652d80bf5fab321e3482a76072 100644 (file)
@@ -35,7 +35,7 @@ public class DbVersion70Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 1);
+    verifyMigrationCount(underTest, 3);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullableTest.java
new file mode 100644 (file)
index 0000000..72faa61
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+public class MakeQualityGatesIsBuiltInNotNullableTest {
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(MakeQualityGatesIsBuiltInNotNullableTest.class, "quality_gates.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private MakeQualityGatesIsBuiltInNotNullable underTest = new MakeQualityGatesIsBuiltInNotNullable(db.database());
+
+  @Test
+  public void execute_makes_column_not_null() throws SQLException {
+    db.assertColumnDefinition("quality_gates", "is_built_in", Types.BOOLEAN, null, true);
+    insertRow(1);
+    insertRow(2);
+
+    underTest.execute();
+
+    db.assertColumnDefinition("quality_gates", "is_built_in", Types.BOOLEAN, null, false);
+  }
+
+  private void insertRow(int id) {
+    db.executeInsert(
+      "QUALITY_GATES",
+      "NAME", "name_" + id,
+      "IS_BUILT_IN", false
+      );
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltInTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltInTest.java
new file mode 100644 (file)
index 0000000..af090f0
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * 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.Date;
+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.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class PopulateQualityGatesIsBuiltInTest {
+
+  private final static long PAST = 10_000_000_000L;
+  private final static long NOW = 50_000_000_000L;
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(PopulateQualityGatesIsBuiltInTest.class, "quality_gates.sql");
+
+  private System2 system2 = new TestSystem2().setNow(NOW);
+
+  private PopulateQualityGatesIsBuiltIn underTest = new PopulateQualityGatesIsBuiltIn(db.database(), system2);
+
+  @Test
+  public void has_no_effect_if_table_is_empty() throws SQLException {
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable("quality_gates")).isEqualTo(0);
+  }
+
+  @Test
+  public void updates_sonarqube_way_is_build_in_column_to_true() throws SQLException {
+    insertQualityGate("SonarQube way", null);
+
+    underTest.execute();
+
+    assertQualityGates(tuple("SonarQube way", true, new Date(PAST), new Date(NOW)));
+  }
+
+  @Test
+  public void updates_none_sonarqube_way_is_build_in_column_to_false() throws SQLException {
+    insertQualityGate("Other 1", null);
+    insertQualityGate("Other 2", null);
+
+    underTest.execute();
+
+    assertQualityGates(
+      tuple("Other 1", false, new Date(PAST), new Date(NOW)),
+      tuple("Other 2", false, new Date(PAST), new Date(NOW)));
+  }
+
+  @Test
+  public void does_nothing_when_built_in_column_is_set() throws SQLException {
+    insertQualityGate("SonarQube way", true);
+    insertQualityGate("Other way", false);
+
+    underTest.execute();
+
+    assertQualityGates(
+      tuple("SonarQube way", true, new Date(PAST), new Date(PAST)),
+      tuple("Other way", false, new Date(PAST), new Date(PAST)));
+  }
+
+  @Test
+  public void execute_is_reentreant() throws SQLException {
+    insertQualityGate("SonarQube way", null);
+    insertQualityGate("Other way", null);
+
+    underTest.execute();
+
+    underTest.execute();
+
+    assertQualityGates(
+      tuple("SonarQube way", true, new Date(PAST), new Date(NOW)),
+      tuple("Other way", false, new Date(PAST), new Date(NOW)));
+  }
+
+  private void assertQualityGates(Tuple... expectedTuples) {
+    assertThat(db.select("SELECT NAME, IS_BUILT_IN, CREATED_AT, UPDATED_AT FROM QUALITY_GATES")
+      .stream()
+      .map(map -> new Tuple(map.get("NAME"), map.get("IS_BUILT_IN"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
+      .collect(Collectors.toList()))
+        .containsExactlyInAnyOrder(expectedTuples);
+  }
+
+  private void insertQualityGate(String name, @Nullable Boolean builtIn) {
+    db.executeInsert(
+      "QUALITY_GATES",
+      "NAME", name,
+      "IS_BUILT_IN", builtIn == null ? null : builtIn.toString(),
+      "CREATED_AT", new Date(PAST),
+      "UPDATED_AT", new Date(PAST));
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest/quality_gates.sql
new file mode 100644 (file)
index 0000000..811a5d4
--- /dev/null
@@ -0,0 +1,7 @@
+CREATE TABLE "QUALITY_GATES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "NAME" VARCHAR(100) NOT NULL,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP,
+);
+CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest/quality_gates_7_0.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddIsBuiltInToQualityGatesTest/quality_gates_7_0.sql
deleted file mode 100644 (file)
index 811a5d4..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-CREATE TABLE "QUALITY_GATES" (
-  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
-  "NAME" VARCHAR(100) NOT NULL,
-  "CREATED_AT" TIMESTAMP,
-  "UPDATED_AT" TIMESTAMP,
-);
-CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullableTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/MakeQualityGatesIsBuiltInNotNullableTest/quality_gates.sql
new file mode 100644 (file)
index 0000000..52b9423
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE "QUALITY_GATES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "NAME" VARCHAR(100) NOT NULL,
+  "IS_BUILT_IN" BOOLEAN NULL,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP,
+);
+CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltInTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/PopulateQualityGatesIsBuiltInTest/quality_gates.sql
new file mode 100644 (file)
index 0000000..52b9423
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE "QUALITY_GATES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "NAME" VARCHAR(100) NOT NULL,
+  "IS_BUILT_IN" BOOLEAN NULL,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP,
+);
+CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME");
index 6b96732a6bfd74f020a0094102eb0c7bf3506f00..722e593c58668237087a33823b8f9b23557241a9 100644 (file)
@@ -41,7 +41,7 @@ public class QualityGateUpdater {
 
   public QualityGateDto create(DbSession dbSession, String name) {
     validateQualityGate(dbSession, null, name);
-    QualityGateDto newQualityGate = new QualityGateDto().setName(name);
+    QualityGateDto newQualityGate = new QualityGateDto().setName(name).setBuiltIn(false);
     dbClient.qualityGateDao().insert(dbSession, newQualityGate);
     return newQualityGate;
   }
index fce67bf44a42047367a3cdc642d592190d5f4c3c..add13b7b5c4e6bf41bad2bd08e66d09141df9b54 100644 (file)
@@ -101,7 +101,7 @@ public class QualityGates {
     getNonNullQgate(sourceId);
     try (DbSession dbSession = dbClient.openSession(false)) {
       validateQualityGate(dbSession, null, destinationName);
-      QualityGateDto destinationGate = new QualityGateDto().setName(destinationName);
+      QualityGateDto destinationGate = new QualityGateDto().setName(destinationName).setBuiltIn(false);
       dao.insert(dbSession, destinationGate);
       for (QualityGateConditionDto sourceCondition : conditionDao.selectForQualityGate(dbSession, sourceId)) {
         conditionDao.insert(new QualityGateConditionDto().setQualityGateId(destinationGate.getId())
index 4b9e66207f7630fb62e196c33d9f1df0cafa931f..accccda8eafb56c5468a735592b9bcdcfc54725d 100644 (file)
@@ -52,6 +52,7 @@ public class QualityGateUpdaterTest {
     assertThat(result).isNotNull();
     assertThat(result.getName()).isEqualTo(QGATE_NAME);
     assertThat(result.getCreatedAt()).isNotNull();
+    assertThat(result.isBuiltIn()).isFalse();
     QualityGateDto reloaded = dbClient.qualityGateDao().selectByName(dbSession, QGATE_NAME);
     assertThat(reloaded).isNotNull();
   }