From 108a542c3248cb24a1c7417dfad693f51e7d4b50 Mon Sep 17 00:00:00 2001 From: OrlovAlexander Date: Thu, 3 Oct 2024 16:42:52 +0200 Subject: [PATCH] SONAR-23250 Update active_rules table and dto --- .../db/qualityprofile/ActiveRuleDaoIT.java | 6 ++ .../db/qualityprofile/ActiveRuleDto.java | 17 +++++- .../db/qualityprofile/ActiveRuleMapper.xml | 6 ++ server/sonar-db-dao/src/schema/schema-sq.ddl | 3 +- .../AddImpactsColumnInActiveRulesTable.java | 53 +++++++++++++++++ .../migration/version/v108/DbVersion108.java | 1 + ...ddImpactsColumnInActiveRulesTableTest.java | 57 +++++++++++++++++++ 7 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTableTest.java diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/qualityprofile/ActiveRuleDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/qualityprofile/ActiveRuleDaoIT.java index ab368608d4f..c7d59c7ea97 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/qualityprofile/ActiveRuleDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/qualityprofile/ActiveRuleDaoIT.java @@ -59,6 +59,7 @@ import static org.sonar.db.qualityprofile.ActiveRuleDto.createFor; class ActiveRuleDaoIT { private static final long NOW = 10_000_000L; + public static final String IMPACTS = "{\\“SECURITY\\”:\\”BLOCKER\\”,\\”MAINTAINABILITY\\”:\\”INFO\\”}"; private QProfileDto profile1; private QProfileDto profile2; @@ -272,6 +273,7 @@ class ActiveRuleDaoIT { ActiveRuleDto activeRule = createFor(profile1, rule1) .setSeverity(BLOCKER) .setInheritance(INHERITED) + .setImpacts(IMPACTS) .setIsExternal(false) .setCreatedAt(1000L) .setUpdatedAt(2000L); @@ -285,6 +287,7 @@ class ActiveRuleDaoIT { assertThat(result.getProfileUuid()).isEqualTo(profile1.getRulesProfileUuid()); assertThat(result.getSeverityString()).isEqualTo(BLOCKER); assertThat(result.getInheritance()).isEqualTo(INHERITED); + assertThat(result.getImpacts()).isEqualTo(IMPACTS); assertThat(result.isExternal()).isFalse(); assertThat(result.getCreatedAt()).isEqualTo(1000L); assertThat(result.getUpdatedAt()).isEqualTo(2000L); @@ -316,6 +319,7 @@ class ActiveRuleDaoIT { ActiveRuleDto activeRule = createFor(profile1, rule1) .setSeverity(BLOCKER) .setInheritance(INHERITED) + .setImpacts("{\"RELIABILITY\":\"INFO\"}") .setCreatedAt(1000L) .setUpdatedAt(2000L); underTest.insert(dbSession, activeRule); @@ -324,6 +328,7 @@ class ActiveRuleDaoIT { ActiveRuleDto activeRuleUpdated = activeRule .setSeverity(MAJOR) .setInheritance(OVERRIDES) + .setImpacts(IMPACTS) // created at should not be updated .setCreatedAt(3000L) .setUpdatedAt(4000L); @@ -337,6 +342,7 @@ class ActiveRuleDaoIT { assertThat(result.getProfileUuid()).isEqualTo(profile1.getRulesProfileUuid()); assertThat(result.getSeverityString()).isEqualTo(MAJOR); assertThat(result.getInheritance()).isEqualTo(OVERRIDES); + assertThat(result.getImpacts()).isEqualTo(IMPACTS); assertThat(result.getCreatedAt()).isEqualTo(1000L); assertThat(result.getUpdatedAt()).isEqualTo(4000L); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java index 96f8b1617a1..b8cff56f10f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java @@ -41,11 +41,12 @@ public class ActiveRuleDto { private String ruleUuid; private Integer severity; private String inheritance; + private String impacts; private long createdAt; private long updatedAt; private boolean prioritizedRule; - // These fields do not exists in db, it's only retrieve by joins + // These fields do not exist in db, it's only retrieve by joins private String repository; private String ruleField; private String ruleProfileUuid; @@ -161,6 +162,16 @@ public class ActiveRuleDto { return this; } + @CheckForNull + public String getImpacts() { + return impacts; + } + + public ActiveRuleDto setImpacts(@Nullable String impacts) { + this.impacts = impacts; + return this; + } + public boolean isExternal() { return this.isExternal; } @@ -170,11 +181,11 @@ public class ActiveRuleDto { return this; } - public boolean isPrioritizedRule(){ + public boolean isPrioritizedRule() { return prioritizedRule; } - public ActiveRuleDto setPrioritizedRule(boolean prioritizedRule){ + public ActiveRuleDto setPrioritizedRule(boolean prioritizedRule) { this.prioritizedRule = prioritizedRule; return this; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 80c38760b5b..4ac4ed02af1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -9,6 +9,7 @@ a.rule_uuid as "ruleUuid", a.failure_level as "severity", a.inheritance as "inheritance", + a.impacts as "impacts", r.plugin_rule_key as "rulefield", r.plugin_name as "repository", rp.uuid as "ruleProfileUuid", @@ -23,6 +24,7 @@ a.rule_uuid as "ruleUuid", a.failure_level as "severity", a.inheritance as "inheritance", + a.impacts as "impacts", a.prioritized_rule as "prioritizedRule", r.plugin_rule_key as "rulefield", r.plugin_name as "repository", @@ -46,6 +48,7 @@ rule_uuid, failure_level, inheritance, + impacts, prioritized_rule, created_at, updated_at @@ -55,6 +58,7 @@ #{ruleUuid, jdbcType=VARCHAR}, #{severity, jdbcType=INTEGER}, #{inheritance, jdbcType=VARCHAR}, + #{impacts, jdbcType=VARCHAR}, #{prioritizedRule, jdbcType=BOOLEAN}, #{createdAt, jdbcType=BIGINT}, #{updatedAt, jdbcType=BIGINT} @@ -66,6 +70,7 @@ set failure_level = #{severity, jdbcType=INTEGER}, inheritance = #{inheritance, jdbcType=VARCHAR}, + impacts = #{impacts, jdbcType=VARCHAR}, prioritized_rule = #{prioritizedRule, jdbcType=BOOLEAN}, updated_at = #{updatedAt, jdbcType=BIGINT} where @@ -337,6 +342,7 @@ ar.uuid as "uuid", ar.failure_level as "severity", ar.inheritance as "inheritance", + ar.impacts as "impacts", ar.prioritized_rule as "prioritizedRule", r.uuid as "ruleUuid", r.plugin_name as "repository", diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 4474390011c..c18d6e54072 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -31,7 +31,8 @@ CREATE TABLE "ACTIVE_RULES"( "UPDATED_AT" BIGINT, "PROFILE_UUID" CHARACTER VARYING(40) NOT NULL, "RULE_UUID" CHARACTER VARYING(40) NOT NULL, - "PRIORITIZED_RULE" BOOLEAN + "PRIORITIZED_RULE" BOOLEAN, + "IMPACTS" CHARACTER VARYING(500) ); ALTER TABLE "ACTIVE_RULES" ADD CONSTRAINT "PK_ACTIVE_RULES" PRIMARY KEY("UUID"); CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_PROFILE_RULE_UUIDS" ON "ACTIVE_RULES"("PROFILE_UUID" NULLS FIRST, "RULE_UUID" NULLS FIRST); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTable.java new file mode 100644 index 00000000000..e3258724e3e --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTable.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v108; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.db.DatabaseUtils.tableColumnExists; + +public class AddImpactsColumnInActiveRulesTable extends DdlChange { + static final String ACTIVE_RULES_TABLE_NAME = "active_rules"; + static final String IMPACTS = "impacts"; + + public AddImpactsColumnInActiveRulesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + if (!tableColumnExists(connection, ACTIVE_RULES_TABLE_NAME, IMPACTS)) { + var columnDef = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName(IMPACTS) + .setIsNullable(true) + .setLimit(500) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), ACTIVE_RULES_TABLE_NAME) + .addColumn(columnDef) + .build()); + } + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java index 2324f4c9541..1c3340bdfce 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java @@ -55,6 +55,7 @@ public class DbVersion108 implements DbVersion { .add(10_8_012, "Drop 'measures_migrated' column on 'portfolios' table", DropMeasuresMigratedColumnInPortfoliosTable.class) .add(10_8_013, "Drop index on 'project_branches.measures_migrated'", DropIndexOnProjectBranchesMeasuresMigrated.class) .add(10_8_014, "Drop 'measures_migrated' column on 'project_branches' table", DropMeasuresMigratedColumnInProjectBranchesTable.class) + .add(10_8_015, "Add column 'impacts' in 'active_rules' table", AddImpactsColumnInActiveRulesTable.class) ; } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTableTest.java new file mode 100644 index 00000000000..8405d5889ae --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTableTest.java @@ -0,0 +1,57 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.v108; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; + +import static java.sql.Types.VARCHAR; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; +import static org.sonar.server.platform.db.migration.version.v108.AddImpactsColumnInActiveRulesTable.ACTIVE_RULES_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v108.AddImpactsColumnInActiveRulesTable.IMPACTS; + +class AddImpactsColumnInActiveRulesTableTest { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddImpactsColumnInActiveRulesTable.class); + + private final AddImpactsColumnInActiveRulesTable underTest = new AddImpactsColumnInActiveRulesTable(db.database()); + + @Test + void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { + db.assertColumnDoesNotExist(ACTIVE_RULES_TABLE_NAME, IMPACTS); + underTest.execute(); + assertColumnExists(); + } + + @Test + void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { + underTest.execute(); + assertColumnExists(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } + + private void assertColumnExists() { + db.assertColumnDefinition(ACTIVE_RULES_TABLE_NAME, IMPACTS, VARCHAR, 500, true); + } + +} -- 2.39.5