From f4945a109a811cdbd292e0ad88fb6e6fb7c0ee13 Mon Sep 17 00:00:00 2001 From: Benjamin Campomenosi Date: Mon, 5 Sep 2022 17:32:48 +0200 Subject: SONAR-17266 add webhook_secret column to alm_settings table --- .../v96/AddWebhookSecretToAlmSettingsTable.java | 66 ++++++++++++++++++++++ .../db/migration/version/v96/DbConstants.java | 1 + .../db/migration/version/v96/DbVersion96.java | 1 + .../AddLanguageColumnToPushEventsTableTest.java | 2 +- .../AddWebhookSecretToAlmSettingsTableTest.java | 58 +++++++++++++++++++ .../schema.sql | 15 +++++ 6 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest/schema.sql (limited to 'server/sonar-db-migration') diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTable.java new file mode 100644 index 00000000000..708abb1d8b1 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTable.java @@ -0,0 +1,66 @@ +/* + * 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.WEBHOOK_SECRET_COLUMN_SIZE; + +public class AddWebhookSecretToAlmSettingsTable extends DdlChange { + + @VisibleForTesting + static final String WEBHOOK_SECRET_COLUMN_NAME = "webhook_secret"; + @VisibleForTesting + static final String ALM_SETTINGS_TABLE_NAME = "alm_settings"; + + private static final VarcharColumnDef COLUMN_DEFINITION = newVarcharColumnDefBuilder() + .setColumnName(WEBHOOK_SECRET_COLUMN_NAME) + .setIsNullable(true) + .setLimit(WEBHOOK_SECRET_COLUMN_SIZE).build(); + + + public AddWebhookSecretToAlmSettingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + createWebhookSecretColumn(context, connection); + } + } + + private void createWebhookSecretColumn(Context context, Connection connection) { + if (!DatabaseUtils.tableColumnExists(connection, ALM_SETTINGS_TABLE_NAME, WEBHOOK_SECRET_COLUMN_NAME)) { + context.execute(new AddColumnsBuilder(getDialect(), ALM_SETTINGS_TABLE_NAME) + .addColumn(COLUMN_DEFINITION) + .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 index 7079a7a82f8..082bf083043 100644 --- 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 @@ -21,6 +21,7 @@ package org.sonar.server.platform.db.migration.version.v96; class DbConstants { static final int CONTEXT_KEY_COLUMNS_SIZE = 50; + static final int WEBHOOK_SECRET_COLUMN_SIZE = 160; 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 57d133f3fd3..0efdb9cb1e0 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 @@ -43,6 +43,7 @@ public class DbVersion96 implements DbVersion { .add(6512, "Add column 'language' to 'push_events'", AddLanguageColumnToPushEventsTable.class) .add(6513, "Delete duplicated rows in 'project_badge_token'", DeleteDuplicatedProjectBadgeTokens.class) .add(6514, "Add unique index on 'project_uuid' in 'project_badge_token'", CreateIndexForProjectBadgeTokens.class) + .add(6515,"Add column 'webhook_secret' to 'alm_settings'", AddWebhookSecretToAlmSettingsTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddLanguageColumnToPushEventsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddLanguageColumnToPushEventsTableTest.java index 24ff79c7cc8..f5c19444c95 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddLanguageColumnToPushEventsTableTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddLanguageColumnToPushEventsTableTest.java @@ -36,7 +36,7 @@ public class AddLanguageColumnToPushEventsTableTest { private final AddLanguageColumnToPushEventsTable underTest = new AddLanguageColumnToPushEventsTable(db.database()); @Test - public void column_education_principles_should_be_added() throws SQLException { + public void column_language_should_be_added() throws SQLException { db.assertColumnDoesNotExist(PUSH_EVENTS_TABLE_NAME, COLUMN_LANGUAGE); underTest.execute(); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest.java new file mode 100644 index 00000000000..536ec817ee6 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest.java @@ -0,0 +1,58 @@ +/* + * 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.AddWebhookSecretToAlmSettingsTable.ALM_SETTINGS_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v96.AddWebhookSecretToAlmSettingsTable.WEBHOOK_SECRET_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v96.DbConstants.WEBHOOK_SECRET_COLUMN_SIZE; + +public class AddWebhookSecretToAlmSettingsTableTest { + + @Rule + public final CoreDbTester db = createForSchema(AddWebhookSecretToAlmSettingsTableTest.class, "schema.sql"); + + private final AddWebhookSecretToAlmSettingsTable underTest = new AddWebhookSecretToAlmSettingsTable(db.database()); + + @Test + public void column_language_should_be_added() throws SQLException { + db.assertColumnDoesNotExist(ALM_SETTINGS_TABLE_NAME, WEBHOOK_SECRET_COLUMN_NAME); + + underTest.execute(); + + db.assertColumnDefinition(ALM_SETTINGS_TABLE_NAME, WEBHOOK_SECRET_COLUMN_NAME, Types.VARCHAR, WEBHOOK_SECRET_COLUMN_SIZE, true); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + db.assertColumnDoesNotExist(ALM_SETTINGS_TABLE_NAME, WEBHOOK_SECRET_COLUMN_NAME); + + underTest.execute(); + underTest.execute(); + + db.assertColumnDefinition(ALM_SETTINGS_TABLE_NAME, WEBHOOK_SECRET_COLUMN_NAME, Types.VARCHAR, WEBHOOK_SECRET_COLUMN_SIZE, true); + } +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest/schema.sql new file mode 100644 index 00000000000..ac2de3480f7 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v96/AddWebhookSecretToAlmSettingsTableTest/schema.sql @@ -0,0 +1,15 @@ + CREATE TABLE "ALM_SETTINGS"( + "UUID" VARCHAR(40) NOT NULL, + "ALM_ID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(200) NOT NULL, + "URL" VARCHAR(2000), + "APP_ID" VARCHAR(80), + "PRIVATE_KEY" VARCHAR(2000), + "PAT" VARCHAR(2000), + "UPDATED_AT" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "CLIENT_ID" VARCHAR(80), + "CLIENT_SECRET" VARCHAR(80) + ); + ALTER TABLE "ALM_SETTINGS" ADD CONSTRAINT "PK_ALM_SETTINGS" PRIMARY KEY("UUID"); + CREATE UNIQUE INDEX "UNIQ_ALM_SETTINGS" ON "ALM_SETTINGS"("KEE"); \ No newline at end of file -- cgit v1.2.3