]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23250 Update active_rules table and dto
authorOrlovAlexander <alexander.orlov@sonarsource.com>
Thu, 3 Oct 2024 14:42:52 +0000 (16:42 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 16 Oct 2024 20:03:00 +0000 (20:03 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/qualityprofile/ActiveRuleDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddImpactsColumnInActiveRulesTableTest.java [new file with mode: 0644]

index ab368608d4f123bebed706c4504a0f8ee8bc1248..c7d59c7ea978586acc9967b788bee9612f4d9000 100644 (file)
@@ -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);
   }
index 96f8b1617a189bc95afafe4bd5bc0e18af1b44d8..b8cff56f10fee886704b1e0ee58145e54d01f665 100644 (file)
@@ -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;
   }
index 80c38760b5be96902e6c3d0beea8a0047b128fef..4ac4ed02af189b713883436225e730920f705803 100644 (file)
@@ -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
     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",
index 4474390011c6b3ac85c7a7fae29ca92b63739829..c18d6e54072d6139fdd64c7cfd962a12df624478 100644 (file)
@@ -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 (file)
index 0000000..e325872
--- /dev/null
@@ -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());
+      }
+    }
+  }
+}
index 2324f4c95414d8e4e0c85ab23766be258953241f..1c3340bdfce0f70378dd8cf2c8a74584981d62db 100644 (file)
@@ -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 (file)
index 0000000..8405d58
--- /dev/null
@@ -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);
+  }
+
+}