From 051c30417beed3f793766946f82496e381130ab5 Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Tue, 5 Jul 2022 09:29:23 +0200 Subject: [PATCH] SONAR-16614 add rule_description_context_key column in issues table --- server/sonar-db-dao/src/schema/schema-sq.ddl | 3 +- ...ContextColumnsToRuleDescSectionsTable.java | 3 +- ...uleDescriptionContextKeyInIssuesTable.java | 62 +++++++++++++++++++ .../db/migration/version/v96/DbConstants.java | 28 +++++++++ .../db/migration/version/v96/DbVersion96.java | 1 + ...escriptionContextKeyInIssuesTableTest.java | 55 ++++++++++++++++ .../schema.sql | 35 +++++++++++ 7 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbConstants.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest/schema.sql diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 281bafab250..37204e4e154 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -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); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddContextColumnsToRuleDescSectionsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddContextColumnsToRuleDescSectionsTable.java index cad6e3c698c..08ec0d4851c 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddContextColumnsToRuleDescSectionsTable.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddContextColumnsToRuleDescSectionsTable.java @@ -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 index 00000000000..0881155c23a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTable.java @@ -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 index 00000000000..7079a7a82f8 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbConstants.java @@ -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"); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbVersion96.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbVersion96.java index e8651bfc078..b618dc29d46 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbVersion96.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/DbVersion96.java @@ -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 index 00000000000..5ecebb88e2e --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest.java @@ -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 index 00000000000..df04ccae04e --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddRuleDescriptionContextKeyInIssuesTableTest/schema.sql @@ -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); -- 2.39.5