From bee7ee5ec9f07f9f54804eeb22c1e2e547afc2e8 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 21 Sep 2021 11:10:29 -0500 Subject: [PATCH] SONAR-15422 Portfolios 'selection_expression' column is too short --- server/sonar-db-dao/src/schema/schema-sq.ddl | 2 +- .../MigrationConfigurationModule.java | 2 + ...rSelectionExpressionInPortfoliosTable.java | 49 +++++++++++++++++ .../db/migration/version/v92/DbVersion92.java | 32 +++++++++++ ...ectionExpressionInPortfoliosTableTest.java | 53 +++++++++++++++++++ .../version/v92/DbVersion92Test.java | 47 ++++++++++++++++ .../schema.sql | 15 ++++++ 7 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest/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 869c173f7e0..a5d8d5fec77 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -590,7 +590,7 @@ CREATE TABLE "PORTFOLIOS"( "PARENT_UUID" VARCHAR(40), "PRIVATE" BOOLEAN NOT NULL, "SELECTION_MODE" VARCHAR(50) NOT NULL, - "SELECTION_EXPRESSION" VARCHAR(50), + "SELECTION_EXPRESSION" VARCHAR(4000), "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL ); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java index aefa1a5d9ad..beb3929e55f 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java @@ -29,6 +29,7 @@ import org.sonar.server.platform.db.migration.step.MigrationStepsProvider; import org.sonar.server.platform.db.migration.version.v00.DbVersion00; import org.sonar.server.platform.db.migration.version.v90.DbVersion90; import org.sonar.server.platform.db.migration.version.v91.DbVersion91; +import org.sonar.server.platform.db.migration.version.v92.DbVersion92; public class MigrationConfigurationModule extends Module { @Override @@ -38,6 +39,7 @@ public class MigrationConfigurationModule extends Module { DbVersion00.class, DbVersion90.class, DbVersion91.class, + DbVersion92.class, // migration steps MigrationStepRegistryImpl.class, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTable.java new file mode 100644 index 00000000000..441b05ee398 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTable.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +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.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AlterSelectionExpressionInPortfoliosTable extends DdlChange { + private static final String TABLE = "portfolios"; + + private static final VarcharColumnDef columnDefinition = newVarcharColumnDefBuilder() + .setColumnName("selection_expression") + .setIsNullable(true) + .setLimit(VarcharColumnDef.MAX_SIZE) + .build(); + + public AlterSelectionExpressionInPortfoliosTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(columnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java new file mode 100644 index 00000000000..64d0ed85f5f --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; +import org.sonar.server.platform.db.migration.version.DbVersion; + +public class DbVersion92 implements DbVersion { + @Override + public void addSteps(MigrationStepRegistry registry) { + registry + .add(6101, "Change size of column 'selection_expression' in 'Portfolios'", AlterSelectionExpressionInPortfoliosTable.class) + ; + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest.java new file mode 100644 index 00000000000..3ea431b6ca4 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.VARCHAR; + +public class AlterSelectionExpressionInPortfoliosTableTest { + private final String TABLE = "portfolios"; + private final String COLUMN = "selection_expression"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AlterSelectionExpressionInPortfoliosTableTest.class, "schema.sql"); + + private final DdlChange underTest = new AlterSelectionExpressionInPortfoliosTable(db.database()); + + @Test + public void selection_expression_column_is_altered() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition(TABLE, COLUMN, VARCHAR, 4000, true); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + db.assertColumnDefinition(TABLE, COLUMN, VARCHAR, 4000, true); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java new file mode 100644 index 00000000000..cb68553e66a --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92Test.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.v92; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount; +import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; + +public class DbVersion92Test { + + private final DbVersion92 underTest = new DbVersion92(); + + @Test + public void verify_no_support_component() { + assertThat(underTest.getSupportComponents()).isEmpty(); + } + + @Test + public void migrationNumber_starts_at_6101() { + verifyMinimumMigrationNumber(underTest, 6101); + } + + @Test + public void verify_migration_count() { + verifyMigrationCount(underTest, 1); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest/schema.sql new file mode 100644 index 00000000000..3b5fef0154b --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/AlterSelectionExpressionInPortfoliosTableTest/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE "PORTFOLIOS"( + "UUID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(400) NOT NULL, + "NAME" VARCHAR(2000) NOT NULL, + "DESCRIPTION" VARCHAR(2000), + "ROOT_UUID" VARCHAR(40) NOT NULL, + "PARENT_UUID" VARCHAR(40), + "PRIVATE" BOOLEAN NOT NULL, + "SELECTION_MODE" VARCHAR(50) NOT NULL, + "SELECTION_EXPRESSION" VARCHAR(50), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "PORTFOLIOS" ADD CONSTRAINT "PK_PORTFOLIOS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PORTFOLIOS_KEE" ON "PORTFOLIOS"("KEE"); -- 2.39.5