]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16614 add rule_description_context_key column in issues table
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Tue, 5 Jul 2022 07:29:23 +0000 (09:29 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 8 Jul 2022 20:02:47 +0000 (20:02 +0000)
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddContextColumnsToRuleDescSectionsTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbConstants.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbVersion96.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest/schema.sql [new file with mode: 0644]

index 281bafab250dd330174337bb812c3a27bd5c6108..37204e4e154c16bf9694e805935df86d9beaa6e8 100644 (file)
@@ -426,7 +426,8 @@ CREATE TABLE "ISSUES"(
     "LOCATIONS" BINARY LARGE OBJECT,
     "ISSUE_TYPE" TINYINT,
     "FROM_HOTSPOT" BOOLEAN,
-    "QUICK_FIX_AVAILABLE" BOOLEAN
+    "QUICK_FIX_AVAILABLE" BOOLEAN,
+    "RULE_DESCRIPTION_CONTEXT_KEY" CHARACTER VARYING(50)
 );
 ALTER TABLE "ISSUES" ADD CONSTRAINT "PK_ISSUES" PRIMARY KEY("KEE");
 CREATE INDEX "ISSUES_ASSIGNEE" ON "ISSUES"("ASSIGNEE" NULLS FIRST);
index cad6e3c698cc1525afade06fe5703e80df00817a..08ec0d4851c1d1b4067219c9796e117ec6e33ff8 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.server.platform.db.migration.step.DdlChange;
 
 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
 import static org.sonar.server.platform.db.migration.version.v95.CreateRuleDescSectionsTable.RULE_DESCRIPTION_SECTIONS_TABLE;
+import static org.sonar.server.platform.db.migration.version.v96.DbConstants.CONTEXT_KEY_COLUMNS_SIZE;
 
 public class AddContextColumnsToRuleDescSectionsTable extends DdlChange {
 
@@ -49,7 +50,7 @@ public class AddContextColumnsToRuleDescSectionsTable extends DdlChange {
   }
 
   private void createContextKeyColumn(Context context, Connection connection) {
-    VarcharColumnDef contextKeyColumn = newVarcharColumnDefBuilder().setColumnName(COLUMN_CONTEXT_KEY).setIsNullable(true).setLimit(50).build();
+    VarcharColumnDef contextKeyColumn = newVarcharColumnDefBuilder().setColumnName(COLUMN_CONTEXT_KEY).setIsNullable(true).setLimit(CONTEXT_KEY_COLUMNS_SIZE).build();
     createColumnIfNotExists(context, connection, contextKeyColumn);
   }
 
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTable.java
new file mode 100644 (file)
index 0000000..0881155
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.v96;
+
+import com.google.common.annotations.VisibleForTesting;
+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.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+import static org.sonar.server.platform.db.migration.version.v96.DbConstants.CONTEXT_KEY_COLUMNS_SIZE;
+
+public class AddRuleDescriptionContextKeyInIssuesTable extends DdlChange {
+
+  @VisibleForTesting
+  static final String ISSUES_TABLE_NAME = "issues";
+  @VisibleForTesting
+  static final String RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME = "rule_description_context_key";
+
+  private static final VarcharColumnDef columnDefinition = newVarcharColumnDefBuilder()
+    .setColumnName(RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME)
+    .setLimit(CONTEXT_KEY_COLUMNS_SIZE)
+    .setIsNullable(true)
+    .build();
+
+  public AddRuleDescriptionContextKeyInIssuesTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection c = getDatabase().getDataSource().getConnection()) {
+      if (!DatabaseUtils.tableColumnExists(c, ISSUES_TABLE_NAME, RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME)) {
+        context.execute(new AddColumnsBuilder(getDialect(), ISSUES_TABLE_NAME)
+          .addColumn(columnDefinition)
+          .build());
+      }
+    }
+  }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbConstants.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbConstants.java
new file mode 100644 (file)
index 0000000..7079a7a
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.v96;
+
+class DbConstants {
+  static final int CONTEXT_KEY_COLUMNS_SIZE = 50;
+
+  private DbConstants() {
+    throw new IllegalStateException("This class must not be instantiated");
+  }
+}
index e8651bfc078b9dedc6558d61bb87b4f899110518..b618dc29d469f2d78ceca76195d6b251368a70ff 100644 (file)
@@ -32,6 +32,7 @@ public class DbVersion96 implements DbVersion {
       .add(6502, "Drop unique index uniq_rule_desc_sections_kee", DropIndexForRuleDescSection.class)
       .add(6503, "Create unique uniq_rule_desc_sections", CreateIndexForRuleDescSections.class)
       .add(6504, "Add column 'expiration_date' to 'user_tokens'", AddExpirationDateColumnToUserTokens.class)
+      .add(6505, "Add column 'rule_description_context_key' to 'issues'", AddRuleDescriptionContextKeyInIssuesTable.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest.java
new file mode 100644 (file)
index 0000000..5ecebb8
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.v96;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.sonar.db.CoreDbTester.createForSchema;
+import static org.sonar.server.platform.db.migration.version.v96.AddRuleDescriptionContextKeyInIssuesTable.ISSUES_TABLE_NAME;
+import static org.sonar.server.platform.db.migration.version.v96.AddRuleDescriptionContextKeyInIssuesTable.RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME;
+
+public class AddRuleDescriptionContextKeyInIssuesTableTest {
+
+  @Rule
+  public final CoreDbTester db = createForSchema(AddRuleDescriptionContextKeyInIssuesTableTest.class, "schema.sql");
+
+  private final AddRuleDescriptionContextKeyInIssuesTable addRuleDescriptionContextKeyInIssuesTable = new AddRuleDescriptionContextKeyInIssuesTable(db.database());
+
+  @Test
+  public void column_rule_description_context_key_should_be_added() throws SQLException {
+    db.assertColumnDoesNotExist(ISSUES_TABLE_NAME, RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME);
+
+    addRuleDescriptionContextKeyInIssuesTable.execute();
+
+    db.assertColumnDefinition(ISSUES_TABLE_NAME, RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME, Types.VARCHAR, 50, true);
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    addRuleDescriptionContextKeyInIssuesTable.execute();
+    addRuleDescriptionContextKeyInIssuesTable.execute();
+
+    db.assertColumnDefinition(ISSUES_TABLE_NAME, RULE_DESCRIPTION_CONTEXT_KEY_COLUMN_NAME, Types.VARCHAR, DbConstants.CONTEXT_KEY_COLUMNS_SIZE, true);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest/schema.sql
new file mode 100644 (file)
index 0000000..df04cca
--- /dev/null
@@ -0,0 +1,35 @@
+CREATE TABLE "ISSUES"(
+    "KEE" CHARACTER VARYING(50) NOT NULL,
+    "RULE_UUID" CHARACTER VARYING(40),
+    "SEVERITY" CHARACTER VARYING(10),
+    "MANUAL_SEVERITY" BOOLEAN NOT NULL,
+    "MESSAGE" CHARACTER VARYING(4000),
+    "LINE" INTEGER,
+    "GAP" DOUBLE PRECISION,
+    "STATUS" CHARACTER VARYING(20),
+    "RESOLUTION" CHARACTER VARYING(20),
+    "CHECKSUM" CHARACTER VARYING(1000),
+    "ASSIGNEE" CHARACTER VARYING(255),
+    "AUTHOR_LOGIN" CHARACTER VARYING(255),
+    "EFFORT" INTEGER,
+    "CREATED_AT" BIGINT,
+    "UPDATED_AT" BIGINT,
+    "ISSUE_CREATION_DATE" BIGINT,
+    "ISSUE_UPDATE_DATE" BIGINT,
+    "ISSUE_CLOSE_DATE" BIGINT,
+    "TAGS" CHARACTER VARYING(4000),
+    "COMPONENT_UUID" CHARACTER VARYING(50),
+    "PROJECT_UUID" CHARACTER VARYING(50),
+    "LOCATIONS" BINARY LARGE OBJECT,
+    "ISSUE_TYPE" TINYINT,
+    "FROM_HOTSPOT" BOOLEAN,
+    "QUICK_FIX_AVAILABLE" BOOLEAN
+);
+ALTER TABLE "ISSUES" ADD CONSTRAINT "PK_ISSUES" PRIMARY KEY("KEE");
+CREATE INDEX "ISSUES_ASSIGNEE" ON "ISSUES"("ASSIGNEE" NULLS FIRST);
+CREATE INDEX "ISSUES_COMPONENT_UUID" ON "ISSUES"("COMPONENT_UUID" NULLS FIRST);
+CREATE INDEX "ISSUES_CREATION_DATE" ON "ISSUES"("ISSUE_CREATION_DATE" NULLS FIRST);
+CREATE INDEX "ISSUES_PROJECT_UUID" ON "ISSUES"("PROJECT_UUID" NULLS FIRST);
+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);