]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10089 Add a migration removing loaded_templates for quality gates
authorEric Hartmann <hartmann.eric@gmail.com>
Wed, 22 Nov 2017 10:46:09 +0000 (11:46 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 4 Dec 2017 12:44:55 +0000 (13:44 +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/RemoveQualityGateLoadedTemplates.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/RemoveQualityGateLoadedTemplatesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest/loaded_templates.sql [new file with mode: 0644]

index b8bae431e0575080631cb02a1291ad838e48b8e2..bde8d70a4b2e2a6872651841dc77910fe99c9bb0 100644 (file)
@@ -30,6 +30,7 @@ public class DbVersion70 implements DbVersion {
       .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)
+      .add(1903, "Remove quality gates loaded templates", RemoveQualityGateLoadedTemplates.class)
     ;
   }
 
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplates.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplates.java
new file mode 100644 (file)
index 0000000..85fb015
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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 RemoveQualityGateLoadedTemplates extends DataChange {
+
+  public RemoveQualityGateLoadedTemplates(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("SELECT id " +
+      "FROM loaded_templates " +
+      "WHERE template_type = ?")
+      .setString(1, "QUALITY_GATE");
+    massUpdate.update("DELETE from loaded_templates WHERE id=?");
+    massUpdate.rowPluralName("delete loaded templates for quality gate");
+    massUpdate.execute((row, update) -> {
+      update.setLong(1, row.getLong(1));
+      return true;
+    });
+  }
+}
index 4daa8d55926a47652d80bf5fab321e3482a76072..2c1870ebe961f00898fa2154bf9abc0690125ebc 100644 (file)
@@ -29,13 +29,13 @@ public class DbVersion70Test {
   private DbVersion70 underTest = new DbVersion70();
 
   @Test
-  public void migrationNumber_starts_at_1830() {
+  public void migrationNumber_starts_at_1900() {
     verifyMinimumMigrationNumber(underTest, 1900);
   }
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 3);
+    verifyMigrationCount(underTest, 4);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest.java
new file mode 100644 (file)
index 0000000..dadb9e6
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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.IntStream;
+import org.apache.commons.lang.RandomStringUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RemoveQualityGateLoadedTemplatesTest {
+  public static final String QUALITY_GATE_TYPE = "QUALITY_GATE";
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(RemoveQualityGateLoadedTemplatesTest.class, "loaded_templates.sql");
+
+  private RemoveQualityGateLoadedTemplates underTest = new RemoveQualityGateLoadedTemplates(db.database());
+
+  @Test
+  public void has_no_effect_if_table_is_empty() throws SQLException {
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(0);
+  }
+
+  @Test
+  public void migration_should_remove_all_quality_gate_loaded_templates() throws SQLException {
+    db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", QUALITY_GATE_TYPE);
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(0);
+  }
+
+  @Test
+  public void migration_should_NOT_remove_other_gate_loaded_templates() throws SQLException {
+    db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", "WHATEVER1");
+    db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", "WHATEVER2");
+    db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", "WHATEVER3");
+    db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", "WHATEVER4");
+    db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", "WHATEVER5");
+
+    underTest.execute();
+
+    assertThat(db.countSql("SELECT count(*) FROM loaded_templates WHERE template_type = '" + QUALITY_GATE_TYPE + "'")).isEqualTo(0);
+    assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(5);
+  }
+
+  @Test
+  public void migration_is_reentrant() throws SQLException {
+    db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", "WHATEVER1");
+    db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", "WHATEVER2");
+    db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", "WHATEVER3");
+    db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", "WHATEVER4");
+    db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", "WHATEVER5");
+
+    db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", QUALITY_GATE_TYPE);
+    db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", QUALITY_GATE_TYPE);
+
+    underTest.execute();
+    underTest.execute();
+
+    assertThat(db.countSql("SELECT count(*) FROM loaded_templates WHERE template_type = '" + QUALITY_GATE_TYPE + "'")).isEqualTo(0);
+    assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(5);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest/loaded_templates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest/loaded_templates.sql
new file mode 100644 (file)
index 0000000..d3c72d4
--- /dev/null
@@ -0,0 +1,6 @@
+CREATE TABLE "LOADED_TEMPLATES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "KEE" VARCHAR(200),
+  "TEMPLATE_TYPE" VARCHAR(64) NOT NULL
+);
+CREATE INDEX "IX_LOADED_TEMPLATES_TYPE" ON "LOADED_TEMPLATES" ("TEMPLATE_TYPE");