]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20021 Create rules and issues impact tables
authorJacek <jacek.poreda@sonarsource.com>
Wed, 2 Aug 2023 11:59:23 +0000 (13:59 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 18 Aug 2023 20:02:47 +0000 (20:02 +0000)
14 files changed:
server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java
server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIssueImpactsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateRulesDefaultImpactsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpacts.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpacts.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIssueImpactsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateRulesDefaultImpactsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpactsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpactsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpactsTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpactsTest/schema.sql [new file with mode: 0644]

index 923955f5147708e7220fdaca70ee0e68f46406a2..476ee26358934e935d41ec3e1b8cbc0def85f489 100644 (file)
@@ -565,7 +565,29 @@ public class PurgeCommandsIT {
     underTest.deleteIssues(projectOrView.uuid());
 
     assertThat(dbTester.countSql("select count(uuid) from new_code_reference_issues where issue_key in (" +
-                                 String.join(", ", issueKeys) + ")")).isZero();
+      String.join(", ", issueKeys) + ")")).isZero();
+  }
+
+  @Test
+  @UseDataProvider("projectsAndViews")
+  public void deleteIssues_shouldDeleteIssuesImpacts(ComponentDto projectOrView) {
+    RuleDto rule = dbTester.rules().insert();
+    dbTester.components().insertComponent(projectOrView);
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(projectOrView));
+    List<String> issueKeys = new ArrayList<>();
+    int count = 5;
+    IntStream.range(0, count).forEach(i -> {
+      IssueDto issue = dbTester.issues().insertIssue(t -> t.setRule(rule).setProject(projectOrView).setComponent(projectOrView)
+        .addImpact(new ImpactDto().setUuid(UuidFactoryFast.getInstance().create())
+          .setSoftwareQuality(SoftwareQuality.MAINTAINABILITY)
+          .setSeverity(Severity.HIGH)));
+      issueKeys.add("'" + issue.getKey() + "'");
+    });
+
+    underTest.deleteIssues(projectOrView.uuid());
+
+    assertThat(dbTester.countSql("select count(uuid) from issues_impacts where issue_key in (" +
+      String.join(", ", issueKeys) + ")")).isZero();
   }
 
   @Test
index 61b2b81abfbcdee9e2cfbf7030e0e4c5380fe32e..c24e040a0a0427df5922ad2099aeadc036d3e2c3 100644 (file)
     #{createdAt,jdbcType=BIGINT})
   </insert>
 
-  <insert id="deleteAsNewCodeOnReferenceBranch" parameterType="String" useGeneratedKeys="false">
+  <insert id="insertIssueImpact" parameterType="map" useGeneratedKeys="false">
+    INSERT INTO issues_impacts (uuid, issue_key, software_quality, severity)
+    VALUES (
+    #{dto.uuid,jdbcType=VARCHAR},
+    #{issueKey,jdbcType=VARCHAR},
+    #{dto.softwareQuality,jdbcType=VARCHAR},
+    #{dto.severity,jdbcType=VARCHAR})
+  </insert>
+
+  <delete id="deleteAsNewCodeOnReferenceBranch" parameterType="String" >
     DELETE FROM new_code_reference_issues
     where issue_key = #{issueKey, jdbcType=VARCHAR}
-  </insert>
+  </delete>
+
+  <delete id="deleteIssueImpacts" parameterType="String" >
+    DELETE FROM issues_impacts
+    where issue_key = #{issueUuid, jdbcType=VARCHAR}
+  </delete>
 
   <!--
     IMPORTANT - invariant columns can't be updated. See IssueDto#toDtoForUpdate()
     inner join components root on root.uuid=i.project_uuid
     left join users u on i.assignee = u.uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
     where i.kee=#{kee,jdbcType=VARCHAR}
   </select>
 
     inner join components p on p.uuid=i.component_uuid
     inner join components root on root.uuid=i.project_uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
     where
     i.component_uuid = #{componentUuid,jdbcType=VARCHAR} and
     i.status &lt;&gt; 'CLOSED'
       and ic.change_data like '%status=%|CLOSED%'
     left join new_code_reference_issues n on
       i.kee = n.issue_key
+    left outer join issues_impacts ii on
+      i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on
+      r.uuid = rdi.rule_uuid
     where
       i.component_uuid = #{componentUuid,jdbcType=VARCHAR}
       and i.status = 'CLOSED'
     inner join components p on p.uuid=i.component_uuid
     inner join components root on root.uuid=i.project_uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
     where i.kee in
     <foreach collection="list" open="(" close=")" item="key" separator=",">
       #{key,jdbcType=VARCHAR}
     inner join components p on p.uuid=i.component_uuid
     inner join components root on root.uuid=i.project_uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
     where
       i.kee in
         <foreach collection="keys" open="(" close=")" item="key" separator=",">
     inner join components root on root.uuid=i.project_uuid
     left join users u on i.assignee = u.uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
     where i.project_uuid=#{componentUuid,jdbcType=VARCHAR}
     order by i.issue_creation_date ASC
     limit #{pagination.pageSize,jdbcType=INTEGER} offset #{pagination.offset,jdbcType=INTEGER}
     inner join components root on root.uuid=i.project_uuid
     left join users u on i.assignee = u.uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
   </select>
 
    <select id="selectByComponentUuidPaginated" parameterType="map" resultType="Issue" databaseId="oracle">
     inner join components root on root.uuid=i.project_uuid
     left join users u on i.assignee = u.uuid
     left join new_code_reference_issues n on i.kee = n.issue_key
+    left outer join issues_impacts ii on i.kee = ii.issue_key
+    left outer join rules_default_impacts rdi on r.uuid = rdi.rule_uuid
   </select>
 
   <sql id="selectByBranchColumns">
index 7f0bef150d38467c5cedb9b3dba3d1e962a094f0..4646bf41591704f46b0877956c9d05d1672d11cb 100644 (file)
@@ -456,6 +456,15 @@ CREATE INDEX "ISSUES_RESOLUTION" ON "ISSUES"("RESOLUTION" NULLS FIRST);
 CREATE INDEX "ISSUES_UPDATED_AT" ON "ISSUES"("UPDATED_AT" NULLS FIRST);
 CREATE INDEX "ISSUES_RULE_UUID" ON "ISSUES"("RULE_UUID" NULLS FIRST);
 
+CREATE TABLE "ISSUES_IMPACTS"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "ISSUE_KEY" CHARACTER VARYING(40) NOT NULL,
+    "SOFTWARE_QUALITY" CHARACTER VARYING(40) NOT NULL,
+    "SEVERITY" CHARACTER VARYING(40) NOT NULL
+);
+ALTER TABLE "ISSUES_IMPACTS" ADD CONSTRAINT "PK_ISSUES_IMPACTS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_ISS_KEY_SOF_QUAL" ON "ISSUES_IMPACTS"("ISSUE_KEY" NULLS FIRST, "SOFTWARE_QUALITY" NULLS FIRST);
+
 CREATE TABLE "LIVE_MEASURES"(
     "UUID" CHARACTER VARYING(40) NOT NULL,
     "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL,
@@ -908,6 +917,15 @@ CREATE TABLE "RULES"(
 ALTER TABLE "RULES" ADD CONSTRAINT "PK_RULES" PRIMARY KEY("UUID");
 CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES"("PLUGIN_RULE_KEY" NULLS FIRST, "PLUGIN_NAME" NULLS FIRST);
 
+CREATE TABLE "RULES_DEFAULT_IMPACTS"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "RULE_UUID" CHARACTER VARYING(40) NOT NULL,
+    "SOFTWARE_QUALITY" CHARACTER VARYING(40) NOT NULL,
+    "SEVERITY" CHARACTER VARYING(40) NOT NULL
+);
+ALTER TABLE "RULES_DEFAULT_IMPACTS" ADD CONSTRAINT "PK_RULES_DEFAULT_IMPACTS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_RUL_UUID_SOF_QUAL" ON "RULES_DEFAULT_IMPACTS"("RULE_UUID" NULLS FIRST, "SOFTWARE_QUALITY" NULLS FIRST);
+
 CREATE TABLE "RULES_PARAMETERS"(
     "UUID" CHARACTER VARYING(40) NOT NULL,
     "NAME" CHARACTER VARYING(128) NOT NULL,
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIssueImpactsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIssueImpactsTable.java
new file mode 100644 (file)
index 0000000..52461a6
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.CreateTableChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateIssueImpactsTable extends CreateTableChange {
+  static final String TABLE_NAME = "issues_impacts";
+
+  public CreateIssueImpactsTable(Database db) {
+    super(db, TABLE_NAME);
+  }
+
+  @Override
+  public void execute(Context context, String tableName) throws SQLException {
+    context.execute(new CreateTableBuilder(getDialect(), tableName)
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(VarcharColumnDef.UUID_SIZE).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("issue_key").setIsNullable(false).setLimit(VarcharColumnDef.UUID_SIZE).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("software_quality").setIsNullable(false).setLimit(40).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("severity").setIsNullable(false).setLimit(40).build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateRulesDefaultImpactsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateRulesDefaultImpactsTable.java
new file mode 100644 (file)
index 0000000..83def88
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.CreateTableChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateRulesDefaultImpactsTable extends CreateTableChange {
+  static final String TABLE_NAME = "rules_default_impacts";
+
+  public CreateRulesDefaultImpactsTable(Database db) {
+    super(db, TABLE_NAME);
+  }
+
+  @Override
+  public void execute(Context context, String tableName) throws SQLException {
+    context.execute(new CreateTableBuilder(getDialect(), tableName)
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(VarcharColumnDef.UUID_SIZE).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_uuid").setIsNullable(false).setLimit(VarcharColumnDef.UUID_SIZE).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("software_quality").setIsNullable(false).setLimit(40).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("severity").setIsNullable(false).setLimit(40).build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpacts.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpacts.java
new file mode 100644 (file)
index 0000000..1fd6252
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class CreateUniqueConstraintOnIssuesImpacts extends DdlChange {
+  private static final String TABLE_NAME = "issues_impacts";
+  private static final String INDEX_NAME = "uniq_iss_key_sof_qual";
+
+  public CreateUniqueConstraintOnIssuesImpacts(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection connection = getDatabase().getDataSource().getConnection()) {
+      createIndex(context, connection);
+    }
+  }
+
+  private static void createIndex(Context context, Connection connection) {
+    if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, connection)) {
+      context.execute(new CreateIndexBuilder()
+        .setTable(TABLE_NAME)
+        .setName(INDEX_NAME)
+        .addColumn("issue_key")
+        .addColumn("software_quality")
+        .setUnique(true)
+        .build());
+    }
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpacts.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpacts.java
new file mode 100644 (file)
index 0000000..368960e
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class CreateUniqueConstraintOnRulesDefaultImpacts extends DdlChange {
+  private static final String TABLE_NAME = "rules_default_impacts";
+  private static final String INDEX_NAME = "uniq_rul_uuid_sof_qual";
+
+  public CreateUniqueConstraintOnRulesDefaultImpacts(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection connection = getDatabase().getDataSource().getConnection()) {
+      createIndex(context, connection);
+    }
+  }
+
+  private static void createIndex(Context context, Connection connection) {
+    if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, connection)) {
+      context.execute(new CreateIndexBuilder()
+        .setTable(TABLE_NAME)
+        .setName(INDEX_NAME)
+        .addColumn("rule_uuid")
+        .addColumn("software_quality")
+        .setUnique(true)
+        .build());
+    }
+  }
+}
index 2d516a92b83e0a4ec1293267b5243f65951a7c72..c2d8ddc24c996584e879e4c0f458c47306ae7af7 100644 (file)
@@ -89,5 +89,10 @@ 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);
   }
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIssueImpactsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIssueImpactsTableTest.java
new file mode 100644 (file)
index 0000000..ce093c9
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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 java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class CreateIssueImpactsTableTest {
+  private static final String EXPECTED_TABLE_NAME = "issues_impacts";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createEmpty();
+
+  private final DdlChange underTest = new CreateIssueImpactsTable(db.database());
+
+  @Test
+  public void migration_should_create_a_table() throws SQLException {
+    db.assertTableDoesNotExist(EXPECTED_TABLE_NAME);
+
+    underTest.execute();
+
+    db.assertTableExists(EXPECTED_TABLE_NAME);
+    db.assertColumnDefinition(EXPECTED_TABLE_NAME, "issue_key", Types.VARCHAR, 40, false);
+    db.assertColumnDefinition(EXPECTED_TABLE_NAME, "software_quality", Types.VARCHAR, 40, false);
+    db.assertColumnDefinition(EXPECTED_TABLE_NAME, "severity", Types.VARCHAR, 40, false);
+    db.assertPrimaryKey(EXPECTED_TABLE_NAME, null, "uuid");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    db.assertTableDoesNotExist(EXPECTED_TABLE_NAME);
+
+    underTest.execute();
+    // re-entrant
+    underTest.execute();
+
+    db.assertTableExists(EXPECTED_TABLE_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateRulesDefaultImpactsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateRulesDefaultImpactsTableTest.java
new file mode 100644 (file)
index 0000000..3b7f93c
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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 java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+
+public class CreateRulesDefaultImpactsTableTest {
+  private static final String EXPECTED_TABLE_NAME = "rules_default_impacts";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createEmpty();
+
+  private final DdlChange underTest = new CreateRulesDefaultImpactsTable(db.database());
+
+  @Test
+  public void migration_should_create_a_table() throws SQLException {
+    db.assertTableDoesNotExist(EXPECTED_TABLE_NAME);
+
+    underTest.execute();
+
+    db.assertTableExists(EXPECTED_TABLE_NAME);
+    db.assertColumnDefinition(EXPECTED_TABLE_NAME, "rule_uuid", Types.VARCHAR, 40, false);
+    db.assertColumnDefinition(EXPECTED_TABLE_NAME, "software_quality", Types.VARCHAR, 40, false);
+    db.assertColumnDefinition(EXPECTED_TABLE_NAME, "severity", Types.VARCHAR, 40, false);
+    db.assertPrimaryKey(EXPECTED_TABLE_NAME, null, "uuid");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    db.assertTableDoesNotExist(EXPECTED_TABLE_NAME);
+
+    underTest.execute();
+    // re-entrant
+    underTest.execute();
+
+    db.assertTableExists(EXPECTED_TABLE_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpactsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpactsTest.java
new file mode 100644 (file)
index 0000000..2e14357
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class CreateUniqueConstraintOnIssuesImpactsTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateUniqueConstraintOnIssuesImpactsTest.class, "schema.sql");
+
+  private final CreateUniqueConstraintOnIssuesImpacts underTest = new CreateUniqueConstraintOnIssuesImpacts(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist("issues_impacts", "uniq_iss_key_sof_qual");
+
+    underTest.execute();
+
+    db.assertUniqueIndex("issues_impacts", "uniq_iss_key_sof_qual", "issue_key", "software_quality");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    underTest.execute();
+    underTest.execute();
+
+    db.assertUniqueIndex("issues_impacts", "uniq_iss_key_sof_qual", "issue_key", "software_quality");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpactsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpactsTest.java
new file mode 100644 (file)
index 0000000..aed10eb
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * 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.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+
+public class CreateUniqueConstraintOnRulesDefaultImpactsTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateUniqueConstraintOnRulesDefaultImpactsTest.class, "schema.sql");
+
+  private final CreateUniqueConstraintOnRulesDefaultImpacts underTest = new CreateUniqueConstraintOnRulesDefaultImpacts(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist("rules_default_impacts", "uniq_rul_uuid_sof_qual");
+
+    underTest.execute();
+
+    db.assertUniqueIndex("rules_default_impacts", "uniq_rul_uuid_sof_qual", "rule_uuid", "software_quality");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    underTest.execute();
+    underTest.execute();
+
+    db.assertUniqueIndex("rules_default_impacts", "uniq_rul_uuid_sof_qual", "rule_uuid", "software_quality");
+  }
+}
+
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpactsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnIssuesImpactsTest/schema.sql
new file mode 100644 (file)
index 0000000..b334698
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE "ISSUES_IMPACTS"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "ISSUE_KEY" CHARACTER VARYING(40) NOT NULL,
+    "SOFTWARE_QUALITY" CHARACTER VARYING(40) NOT NULL,
+    "SEVERITY" CHARACTER VARYING(40) NOT NULL
+);
+ALTER TABLE "ISSUES_IMPACTS" ADD CONSTRAINT "PK_ISSUES_IMPACTS" PRIMARY KEY("UUID");
+CREATE INDEX "ISSUES_IMPACTS_ISSUE_KEY" ON "ISSUES_IMPACTS"("ISSUE_KEY" NULLS FIRST);
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpactsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateUniqueConstraintOnRulesDefaultImpactsTest/schema.sql
new file mode 100644 (file)
index 0000000..3e12b77
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE "RULES_DEFAULT_IMPACTS"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "RULE_UUID" CHARACTER VARYING(40) NOT NULL,
+    "SOFTWARE_QUALITY" CHARACTER VARYING(40) NOT NULL,
+    "SEVERITY" CHARACTER VARYING(40) NOT NULL
+);
+ALTER TABLE "RULES_DEFAULT_IMPACTS" ADD CONSTRAINT "PK_RULES_DEFAULT_IMPACTS" PRIMARY KEY("UUID");
+CREATE INDEX "RUL_DEF_IMP_RULE_UUID" ON "RULES_DEFAULT_IMPACTS"("RULE_UUID" NULLS FIRST);