From 0ffac1f829327e18521c69c09a6ced3fb5639af2 Mon Sep 17 00:00:00 2001 From: Alain Kermis Date: Wed, 19 Jul 2023 16:31:32 +0200 Subject: [PATCH] SONAR-19611 Fix WS description for sqale_index metric --- .../migration/version/v102/DbVersion102.java | 6 +- .../v102/FixSqaleIndexMetricDescription.java | 42 ++++++++++++ .../FixSqaleIndexMetricDescriptionTest.java | 66 +++++++++++++++++++ .../schema.sql | 19 ++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescription.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest/schema.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java index 3d6958146a9..9bcbd45fb20 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java @@ -81,6 +81,7 @@ public class DbVersion102 implements DbVersion { .add(10_2_027, "Populate column 'created_at_temp' in 'components' table", PopulateCreatedAtTempInComponents.class) .add(10_2_028, "Drop column 'created_at' in 'components' table", DropCreatedAtInComponents.class) .add(10_2_029, "Rename column 'created_at_temp' to 'created_at' in 'components' table", RenameCreatedAtTempInComponents.class) + .add(10_2_030, "Create table 'anticipated_transitions'", CreateAnticipatedTransitionsTable.class) .add(10_2_031, "Increase size of 'ce_queue.is_last_key' from 55 to 80 characters", IncreaseIsLastKeyInCeActivity.class) @@ -89,11 +90,12 @@ public class DbVersion102 implements DbVersion { .add(10_2_034, "Populate 'clean_code_attribute' column in 'rules' table", PopulateCleanCodeAttributeColumnInRules.class) //TODO SONAR-20073 //.add(10_2_035, "Make 'clean_code_attribute' column not nullable in 'rules' table", MakeCleanCodeAttributeColumnNotNullableInRules.class); - + .add(10_2_036, "Create 'rules_default_impacts' table", CreateRulesDefaultImpactsTable.class) .add(10_2_037, "Create unique constraint index on 'rules_default_impacts' table", CreateUniqueConstraintOnRulesDefaultImpacts.class) .add(10_2_038, "Create 'issues_impacts' table", CreateIssueImpactsTable.class) .add(10_2_039, "Create unique constraint index on 'issues_impacts' table", CreateUniqueConstraintOnIssuesImpacts.class) - .add(10_2_040, "Populate default impacts for existing rules", PopulateDefaultImpactsInRules.class); + .add(10_2_040, "Populate default impacts for existing rules", PopulateDefaultImpactsInRules.class) + .add(10_2_041, "Fix sqale_index metric description in 'metrics' table", FixSqaleIndexMetricDescription.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescription.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescription.java new file mode 100644 index 00000000000..a8a359b4b50 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescription.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.v102; + +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.Upsert; + +public class FixSqaleIndexMetricDescription extends DataChange { + + public FixSqaleIndexMetricDescription(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + try (Upsert upsert = context.prepareUpsert("update metrics set description = ? where name = ?")) { + upsert.setString(1, "Total effort (in minutes) to fix all the issues on the component and therefore to comply to all the requirements."); + upsert.setString(2, "sqale_index"); + upsert.execute(); + upsert.commit(); + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest.java new file mode 100644 index 00000000000..b0034b37044 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.v102; + +import java.sql.SQLException; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FixSqaleIndexMetricDescriptionTest { + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(FixSqaleIndexMetricDescriptionTest.class, "schema.sql"); + + private final FixSqaleIndexMetricDescription underTest = new FixSqaleIndexMetricDescription(db.database()); + private final String OLD_DESCRIPTION = "Total effort (in hours) to fix all the issues on the component and therefore to comply to all the requirements."; + private final String NEW_DESCRIPTION = "Total effort (in minutes) to fix all the issues on the component and therefore to comply to all the requirements."; + + @Before + public void setUp() { + db.executeInsert("metrics", + "uuid", "uuid", + "name", "sqale_index", + "description", OLD_DESCRIPTION); + } + + @Test + public void execute_whenExecuted_shouldUpdateSqaleIndexDescription() throws SQLException { + assertThat(select()).isEqualTo(OLD_DESCRIPTION); + underTest.execute(); + assertThat(select()).isEqualTo(NEW_DESCRIPTION); + } + + @Test + public void execute_WhenExecutedTwice_shouldBeReentrant() throws SQLException { + assertThat(select()).isEqualTo(OLD_DESCRIPTION); + underTest.execute(); + underTest.execute(); + assertThat(select()).isEqualTo(NEW_DESCRIPTION); + } + + private String select() { + return (String) db.selectFirst("SELECT DESCRIPTION FROM metrics WHERE NAME = 'sqale_index'").get("DESCRIPTION"); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest/schema.sql new file mode 100644 index 00000000000..973448595df --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/FixSqaleIndexMetricDescriptionTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "METRICS"( + "UUID" CHARACTER VARYING(40) NOT NULL, + "NAME" CHARACTER VARYING(64) NOT NULL, + "DESCRIPTION" CHARACTER VARYING(255), + "DIRECTION" INTEGER DEFAULT 0 NOT NULL, + "DOMAIN" CHARACTER VARYING(64), + "SHORT_NAME" CHARACTER VARYING(64), + "QUALITATIVE" BOOLEAN DEFAULT FALSE NOT NULL, + "VAL_TYPE" CHARACTER VARYING(8), + "ENABLED" BOOLEAN DEFAULT TRUE, + "WORST_VALUE" DOUBLE PRECISION, + "BEST_VALUE" DOUBLE PRECISION, + "OPTIMIZED_BEST_VALUE" BOOLEAN, + "HIDDEN" BOOLEAN, + "DELETE_HISTORICAL_DATA" BOOLEAN, + "DECIMAL_SCALE" INTEGER +); +ALTER TABLE "METRICS" ADD CONSTRAINT "PK_METRICS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "METRICS_UNIQUE_NAME" ON "METRICS"("NAME" NULLS FIRST); -- 2.39.5