From 71f554e82182d06350a94c8970baa2ee4268056d Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Wed, 22 Nov 2017 11:46:09 +0100 Subject: [PATCH] SONAR-10089 Add a migration removing loaded_templates for quality gates --- .../db/migration/version/v70/DbVersion70.java | 1 + .../v70/RemoveQualityGateLoadedTemplates.java | 47 ++++++++++ .../version/v70/DbVersion70Test.java | 4 +- .../RemoveQualityGateLoadedTemplatesTest.java | 92 +++++++++++++++++++ .../loaded_templates.sql | 6 ++ 5 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplates.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest/loaded_templates.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 b8bae431e05..bde8d70a4b2 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 @@ -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 index 00000000000..85fb015b7a4 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplates.java @@ -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; + }); + } +} 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 4daa8d55926..2c1870ebe96 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 @@ -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 index 00000000000..dadb9e6b2b2 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest.java @@ -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 index 00000000000..d3c72d43b4d --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest/loaded_templates.sql @@ -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"); -- 2.39.5