From 67febf83a0bbf8c987748586093ce695e2a4cd14 Mon Sep 17 00:00:00 2001 From: Serhat Yenican Date: Wed, 6 Nov 2024 07:44:15 +0100 Subject: [PATCH] CODEFIX-177 Add 'ai_code_fix_enabled' column to the 'projects' table --- server/sonar-db-dao/src/schema/schema-sq.ddl | 3 +- ...AICodeFixEnabledColumnToProjectsTable.java | 53 +++++++++++++++++ .../migration/version/v108/DbVersion108.java | 3 +- ...deFixEnabledColumnToProjectsTableTest.java | 57 +++++++++++++++++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTableTest.java diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index b6a46b9bc40..34cfb933d22 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -799,7 +799,8 @@ CREATE TABLE "PROJECTS"( "UPDATED_AT" BIGINT NOT NULL, "NCLOC" BIGINT, "CREATION_METHOD" CHARACTER VARYING(50) NOT NULL, - "AI_CODE_ASSURANCE" BOOLEAN DEFAULT FALSE NOT NULL + "AI_CODE_ASSURANCE" BOOLEAN DEFAULT FALSE NOT NULL, + "AI_CODE_FIX_ENABLED" BOOLEAN DEFAULT FALSE NOT NULL ); ALTER TABLE "PROJECTS" ADD CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID"); CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_PROJECTS_KEE" ON "PROJECTS"("KEE" NULLS FIRST); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTable.java new file mode 100644 index 00000000000..f5d0e6e2d9a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTable.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 AddAICodeFixEnabledColumnToProjectsTable extends DdlChange { + static final String PROJECTS_TABLE_NAME = "projects"; + static final String AI_CODE_FIX_ENABLED_COLUMN_NAME = "ai_code_fix_enabled"; + + public AddAICodeFixEnabledColumnToProjectsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + if (!tableColumnExists(connection, PROJECTS_TABLE_NAME, AI_CODE_FIX_ENABLED_COLUMN_NAME)) { + var columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() + .setColumnName(AI_CODE_FIX_ENABLED_COLUMN_NAME) + .setIsNullable(false) + .setDefaultValue(false) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), PROJECTS_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 2c493a67fc7..169a59e5fc2 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 @@ -62,7 +62,8 @@ public class DbVersion108 implements DbVersion { .add(10_8_019, "Delete Software Quality ratings from project_measures", DeleteSoftwareQualityRatingFromProjectMeasures.class) .add(10_8_020, "Create new software quality metrics", CreateNewSoftwareQualityMetrics.class) .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_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); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTableTest.java new file mode 100644 index 00000000000..7fd5c364e5f --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v108/AddAICodeFixEnabledColumnToProjectsTableTest.java @@ -0,0 +1,57 @@ +/* + * 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; +import static org.sonar.server.platform.db.migration.version.v108.AddAICodeFixEnabledColumnToProjectsTable.AI_CODE_FIX_ENABLED_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v108.AddAICodeFixEnabledColumnToProjectsTable.PROJECTS_TABLE_NAME; + +class AddAICodeFixEnabledColumnToProjectsTableTest { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddImpactsColumnInActiveRulesTable.class); + + private final AddAICodeFixEnabledColumnToProjectsTable underTest = new AddAICodeFixEnabledColumnToProjectsTable(db.database()); + + @Test + void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { + db.assertColumnDoesNotExist(PROJECTS_TABLE_NAME, AI_CODE_FIX_ENABLED_COLUMN_NAME); + underTest.execute(); + assertColumnExists(); + } + + @Test + void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { + underTest.execute(); + assertColumnExists(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } + + private void assertColumnExists() { + db.assertColumnDefinition(PROJECTS_TABLE_NAME, AI_CODE_FIX_ENABLED_COLUMN_NAME, BOOLEAN, null, false); + } + +} -- 2.39.5