From 5396d999bfe805baa4ff7671422df88931af7a8d Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 11 Nov 2021 14:35:10 -0600 Subject: [PATCH] SONAR-15360 Deprecate old built-in JS/TS quality profile --- .../db/migration/version/v92/DbVersion92.java | 1 + .../v92/DeprecateSWRecommendedQProfile.java | 72 ++++++++++++ .../DeprecateSWRecommendedQProfileTest.java | 104 ++++++++++++++++++ .../schema.sql | 10 ++ 4 files changed, 187 insertions(+) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfile.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest/schema.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java index fc9a371e3f8..3f1e6bf6ccc 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java @@ -38,6 +38,7 @@ public class DbVersion92 implements DbVersion { .add(6110, "Add column 'branch_key' to table 'portfolios'", AddBranchToPortfolios.class) .add(6111, "Change size of column 'kee' in 'components'", AlterKeeInComponentsTable.class) .add(6112, "Create 'project_badge_token' Table", CreateProjectBadgeTokenTable.class) + .add(6113, "Deprecate quality profile 'Sonar way Recommended' for js and ts", DeprecateSWRecommendedQProfile.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfile.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfile.java new file mode 100644 index 00000000000..c50aa1ade0d --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfile.java @@ -0,0 +1,72 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; + +public class DeprecateSWRecommendedQProfile extends DataChange { + private final static String NEW_NAME = "Sonar way Recommended (deprecated)"; + + public DeprecateSWRecommendedQProfile(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + migrate(context, "Sonar way Recommended", "js"); + migrate(context, "Sonar way recommended", "ts"); + } + + private static void migrate(Context context, String oldName, String language) throws SQLException { + Integer count = context.prepareSelect("select count(*) from rules_profiles " + + "where name = ? and language = ?") + .setString(1, NEW_NAME) + .setString(2, language) + .get(t -> t.getInt(1)); + + if (count != null && count > 0) { + // keep the same name to not create a duplicated entry + context + .prepareUpsert("update rules_profiles " + + "set is_built_in = ? " + + "where name = ? and is_built_in = ? and language = ?") + .setBoolean(1, false) + .setString(2, oldName) + .setBoolean(3, true) + .setString(4, language) + .execute() + .commit(); + } else { + context + .prepareUpsert("update rules_profiles " + + "set is_built_in = ?, name = ? " + + "where name = ? and is_built_in = ? and language = ?") + .setBoolean(1, false) + .setString(2, NEW_NAME) + .setString(3, oldName) + .setBoolean(4, true) + .setString(5, language) + .execute() + .commit(); + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest.java new file mode 100644 index 00000000000..2dd4fbe30a7 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest.java @@ -0,0 +1,104 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactoryFast; +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 DeprecateSWRecommendedQProfileTest { + private final UuidFactoryFast uuids = UuidFactoryFast.getInstance(); + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DeprecateSWRecommendedQProfileTest.class, "schema.sql"); + + private final DataChange underTest = new DeprecateSWRecommendedQProfile(db.database()); + + @Test + public void deprecate_qps() throws SQLException { + insertQps("Sonar way Recommended", "js", true); + insertQps("Sonar way recommended", "ts", true); + underTest.execute(); + + assertThat(db.select("select name as \"NAME\", language as \"LANG\", is_built_in as \"BUILTIN\" from rules_profiles")) + .extracting(m -> m.get("NAME"), m -> m.get("LANG"), m -> m.get("BUILTIN")) + .containsOnly( + tuple("Sonar way Recommended (deprecated)", "js", false), + tuple("Sonar way Recommended (deprecated)", "ts", false)); + } + + @Test + public void deprecate_qps_no_op_if_no_match() throws SQLException { + insertQps("Sonar way Recommended", "js", false); + insertQps("Sonar way Recommended", "java", true); + underTest.execute(); + + assertThat(db.select("select name as \"NAME\", language as \"LANG\", is_built_in as \"BUILTIN\" from rules_profiles")) + .extracting(m -> m.get("NAME"), m -> m.get("LANG"), m -> m.get("BUILTIN")) + .containsOnly( + tuple("Sonar way Recommended", "js", false), + tuple("Sonar way Recommended", "java", true)); + } + + @Test + public void deprecate_qps_is_reentrant() throws SQLException { + insertQps("Sonar way Recommended", "js", true); + insertQps("Sonar way recommended", "ts", true); + underTest.execute(); + + assertThat(db.select("select name as \"NAME\", language as \"LANG\", is_built_in as \"BUILTIN\" from rules_profiles")) + .extracting(m -> m.get("NAME"), m -> m.get("LANG"), m -> m.get("BUILTIN")) + .containsOnly( + tuple("Sonar way Recommended (deprecated)", "js", false), + tuple("Sonar way Recommended (deprecated)", "ts", false)); + } + + @Test + public void deprecate_qps_does_not_create_duplicate_names() throws SQLException { + insertQps("Sonar way Recommended", "js", true); + insertQps("Sonar way Recommended (deprecated)", "js", false); + + underTest.execute(); + + assertThat(db.select("select name as \"NAME\", language as \"LANG\", is_built_in as \"BUILTIN\" from rules_profiles")) + .extracting(m -> m.get("NAME"), m -> m.get("LANG"), m -> m.get("BUILTIN")) + .containsOnly( + tuple("Sonar way Recommended (deprecated)", "js", false), + tuple("Sonar way Recommended", "js", false)); + } + + private void insertQps(String name, String lang, boolean builtIn) { + db.executeInsert("rules_profiles", + "NAME", name, + "LANGUAGE", lang, + "IS_BUILT_IN", builtIn, + "RULES_UPDATED_AT", "2021-10-01", + "CREATED_AT", "2021-10-01", + "UPDATED_AT", "2021-10-01", + "UUID", uuids.create()); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest/schema.sql new file mode 100644 index 00000000000..b0ff7f47a7c --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/DeprecateSWRecommendedQProfileTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "RULES_PROFILES"( + "UUID" VARCHAR(40) NOT NULL, + "NAME" VARCHAR(100) NOT NULL, + "LANGUAGE" VARCHAR(20), + "IS_BUILT_IN" BOOLEAN NOT NULL, + "RULES_UPDATED_AT" VARCHAR(100), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); +ALTER TABLE "RULES_PROFILES" ADD CONSTRAINT "PK_RULES_PROFILES" PRIMARY KEY("UUID"); -- 2.39.5