diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-05-27 02:05:34 +0200 |
---|---|---|
committer | Eric Hartmann <hartmann.eric@gmail.com> | 2017-06-14 15:43:12 +0200 |
commit | d5bdf821a39621dbf4bd60dbb3412ea5c36b6ddc (patch) | |
tree | 6c482cb7d4b9ffade9f9e5e3fa4d47573a032715 /server/sonar-db-migration | |
parent | b6dd52cf7cd8c47988cfdb9a9887ac4e66c61999 (diff) | |
download | sonarqube-d5bdf821a39621dbf4bd60dbb3412ea5c36b6ddc.tar.gz sonarqube-d5bdf821a39621dbf4bd60dbb3412ea5c36b6ddc.zip |
SONAR-9302 built-in profiles must always be present
The profiles that have been deleted in the past by
administrators should be re-created.
Diffstat (limited to 'server/sonar-db-migration')
5 files changed, 127 insertions, 3 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java index 1b6ddd78046..52bd522c806 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java @@ -17,7 +17,6 @@ * 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.v65; import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; @@ -44,6 +43,7 @@ public class DbVersion65 implements DbVersion { .add(1714, "Purge developer data", PurgeDeveloperData.class) .add(1715, "Add rules_profiles.is_built_in", AddBuiltInFlagToRulesProfiles.class) .add(1716, "Set rules_profiles.is_built_in to false", SetRulesProfilesIsBuiltInToFalse.class) - .add(1717, "Make rules_profiles.is_built_in not null", MakeRulesProfilesIsBuiltInNotNullable.class); + .add(1717, "Make rules_profiles.is_built_in not null", MakeRulesProfilesIsBuiltInNotNullable.class) + .add(1718, "Delete unused loaded_templates on quality profiles", DeleteLoadedTemplatesOnQProfiles.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfiles.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfiles.java new file mode 100644 index 00000000000..0e7067aa0db --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfiles.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.v65; + +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 DeleteLoadedTemplatesOnQProfiles extends DataChange { + + public DeleteLoadedTemplatesOnQProfiles(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from loaded_templates where template_type like 'QUALITY_PROFILE.%'"); + massUpdate.rowPluralName("loaded_templates"); + massUpdate.update("delete from loaded_templates 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/v65/DbVersion65Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java index a8dad8dd5b5..8cd51244d00 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java @@ -35,6 +35,6 @@ public class DbVersion65Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 18); + verifyMigrationCount(underTest, 19); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfilesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfilesTest.java new file mode 100644 index 00000000000..79cb3d39d95 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfilesTest.java @@ -0,0 +1,73 @@ +/* + * 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.v65; + +import java.sql.SQLException; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.core.util.stream.MoreCollectors; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DeleteLoadedTemplatesOnQProfilesTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DeleteLoadedTemplatesOnQProfilesTest.class, "initial.sql"); + + private DeleteLoadedTemplatesOnQProfiles underTest = new DeleteLoadedTemplatesOnQProfiles(db.database()); + + @Test + public void does_nothing_if_table_is_empty() throws SQLException { + underTest.execute(); + + assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(0); + } + + @Test + public void deletes_rows_with_qprofile_type() throws SQLException { + insertRow("ORG_UUID_1", "QUALITY_PROFILE.HASH_OF_ORG_UUID_1"); + insertRow("ORG_UUID_2", "QUALITY_PROFILE.HASH_OF_ORG_UUID_2"); + insertRow("foo", "QUALITY_GATE"); + + underTest.execute(); + + assertThat(selectAllKeys()).containsExactly("foo"); + } + + private void insertRow(String key, String type) { + db.executeInsert( + "LOADED_TEMPLATES", + "KEE", key, + "TEMPLATE_TYPE", type); + } + + private List<String> selectAllKeys() { + return db.select("select kee as TEMPLATE_KEY from loaded_templates") + .stream() + .map(e -> (String)e.get("TEMPLATE_KEY")) + .collect(MoreCollectors.toList()); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfilesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DeleteLoadedTemplatesOnQProfilesTest/initial.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/v65/DeleteLoadedTemplatesOnQProfilesTest/initial.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"); |