From cd6e5cbb8e203abeb8fa3e2670bc70a5368c52ba Mon Sep 17 00:00:00 2001 From: Jacek Poreda Date: Thu, 2 Jan 2025 15:54:58 +0100 Subject: [PATCH] SONAR-14367 Add inline_annotations_enabled column to project_alm_settings table --- server/sonar-db-dao/src/schema/schema-sq.ddl | 3 +- ...nabledColumnToProjectAlmSettingsTable.java | 53 ++++++++++++++++++ .../version/v202501/DbVersion202501.java | 1 + ...edColumnToProjectAlmSettingsTableTest.java | 55 +++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTableTest.java diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index a993947c7332..a115dead30f4 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -708,7 +708,8 @@ CREATE TABLE "PROJECT_ALM_SETTINGS"( "UPDATED_AT" BIGINT NOT NULL, "CREATED_AT" BIGINT NOT NULL, "SUMMARY_COMMENT_ENABLED" BOOLEAN, - "MONOREPO" BOOLEAN NOT NULL + "MONOREPO" BOOLEAN NOT NULL, + "INLINE_ANNOTATIONS_ENABLED" BOOLEAN ); ALTER TABLE "PROJECT_ALM_SETTINGS" ADD CONSTRAINT "PK_PROJECT_ALM_SETTINGS" PRIMARY KEY("UUID"); CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_PROJECT_ALM_SETTINGS" ON "PROJECT_ALM_SETTINGS"("PROJECT_UUID" NULLS FIRST); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable.java new file mode 100644 index 000000000000..fa6e3ed684f0 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable.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.v202501; + +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 AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable extends DdlChange { + private static final String PROJECT_ALM_SETTINGS_TABLE_NAME = "project_alm_settings"; + private static final String INLINE_ANNOTATIONS_ENABLED_COLUMN_NAME = "inline_annotations_enabled"; + + public AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + if (!tableColumnExists(connection, PROJECT_ALM_SETTINGS_TABLE_NAME, INLINE_ANNOTATIONS_ENABLED_COLUMN_NAME)) { + var columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() + .setColumnName(INLINE_ANNOTATIONS_ENABLED_COLUMN_NAME) + .setIsNullable(true) + .setDefaultValue(null) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), PROJECT_ALM_SETTINGS_TABLE_NAME) + .addColumn(columnDef) + .build()); + } + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/DbVersion202501.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/DbVersion202501.java index 3c969f7e0ac9..3b20c9bf69b5 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/DbVersion202501.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202501/DbVersion202501.java @@ -46,6 +46,7 @@ public void addSteps(MigrationStepRegistry registry) { .add(2025_01_006, "Add 'detected_ai_code' column to 'projects' table", AddDetectedAICodeColumnToProjectsTable.class) .add(2025_01_007, "Create table 'migration_logs'", CreateMigrationLogsTable.class) .add(2025_01_008, "Log message if SAML configuration is not valid", LogMessageIfInvalidSamlSetup.class) + .add(2025_01_009, "Add 'inline_annotations_enabled' column to 'project_alm_settings' table", AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTableTest.java new file mode 100644 index 000000000000..962b387d0f16 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v202501/AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTableTest.java @@ -0,0 +1,55 @@ +/* + * 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.v202501; + +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 AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTableTest { + + @RegisterExtension + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable.class); + + private final AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable underTest = new AddInlineAnnotationsEnabledColumnToProjectAlmSettingsTable(db.database()); + + @Test + void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { + db.assertColumnDoesNotExist("project_alm_settings", "inline_annotations_enabled"); + underTest.execute(); + assertColumnExists(); + } + + @Test + void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { + underTest.execute(); + assertColumnExists(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } + + private void assertColumnExists() { + db.assertColumnDefinition("project_alm_settings", "inline_annotations_enabled", BOOLEAN, null, true); + } + +}