diff options
author | Jacek Poreda <jacek.poreda@sonarsource.com> | 2024-11-21 10:59:38 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-11-29 20:03:06 +0000 |
commit | 7aeeaa3fe7b3a491385d467f523a6273a06053fd (patch) | |
tree | 124b362a30a7af89d1cc622ca73f8bc1cc06756c /server/sonar-db-migration | |
parent | 2e581ba13369a28401e3ea359579f977b2c8a3c0 (diff) | |
download | sonarqube-7aeeaa3fe7b3a491385d467f523a6273a06053fd.tar.gz sonarqube-7aeeaa3fe7b3a491385d467f523a6273a06053fd.zip |
SONAR-23619 Add DB migration for 'quality_gates' table to add 'ai_code_supported' boolean flag
Diffstat (limited to 'server/sonar-db-migration')
3 files changed, 110 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeSupportedColumnToQualityGatesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeSupportedColumnToQualityGatesTable.java new file mode 100644 index 00000000000..b9e67816e14 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeSupportedColumnToQualityGatesTable.java @@ -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.BooleanColumnDef; +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 AddAICodeSupportedColumnToQualityGatesTable extends DdlChange { + private static final String QUALITY_GATE_TABLE_NAME = "quality_gates"; + private static final String SUPPORTS_AI_CODE_COLUMN_NAME = "ai_code_supported"; + + public AddAICodeSupportedColumnToQualityGatesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + if (!tableColumnExists(connection, QUALITY_GATE_TABLE_NAME, SUPPORTS_AI_CODE_COLUMN_NAME)) { + var columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() + .setColumnName(SUPPORTS_AI_CODE_COLUMN_NAME) + .setIsNullable(false) + .setDefaultValue(false) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), QUALITY_GATE_TABLE_NAME) + .addColumn(columnDef) + .build()); + } + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java index e69eae20426..1b2b94be3cc 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java @@ -64,7 +64,9 @@ public class DbVersion108 implements DbVersion { .add(10_8_021, "Migrate deprecated project_measures to replacement metrics", MigrateProjectMeasuresDeprecatedMetrics.class) .add(10_8_022, "Add 'manual_severity' column in 'issues_impacts' table", AddManualSeverityColumnInIssuesImpactsTable.class) .add(10_8_023, "Add 'ai_code_fix_enabled' column to 'projects' table", AddAICodeFixEnabledColumnToProjectsTable.class) - .add(10_8_024, "Migrate boolean values of 'sonar.ai.suggestions.enabled' property to new enum values", MigrateAiSuggestionEnabledValues.class); + .add(10_8_024, "Migrate boolean values of 'sonar.ai.suggestions.enabled' property to new enum values", MigrateAiSuggestionEnabledValues.class) + .add(10_8_025, "Add 'ai_code_supported' column in 'quality_gates' table", AddAICodeSupportedColumnToQualityGatesTable.class) + ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeSupportedColumnToQualityGatesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeSupportedColumnToQualityGatesTableTest.java new file mode 100644 index 00000000000..110f1725f52 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeSupportedColumnToQualityGatesTableTest.java @@ -0,0 +1,54 @@ +/* + * 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.BOOLEAN; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; + +class AddAICodeSupportedColumnToQualityGatesTableTest { + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddAICodeSupportedColumnToQualityGatesTable.class); + + private final AddAICodeSupportedColumnToQualityGatesTable underTest = new AddAICodeSupportedColumnToQualityGatesTable(db.database()); + + @Test + void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { + db.assertColumnDoesNotExist("quality_gates", "ai_code_supported"); + underTest.execute(); + assertColumnExists(); + } + + @Test + void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { + underTest.execute(); + assertColumnExists(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } + + private void assertColumnExists() { + db.assertColumnDefinition("quality_gates", "ai_code_supported", BOOLEAN, null, false); + } + +} |