From be652549aac881f0db55ee1ca7cb4871cc4babd5 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 10 Nov 2021 11:10:31 +0100 Subject: [PATCH] SONAR-13426 add project_badge_token table --- server/sonar-db-dao/src/schema/schema-sq.ddl | 9 +++ .../v92/CreateProjectBadgeTokenTable.java | 61 +++++++++++++++++ .../db/migration/version/v92/DbVersion92.java | 1 + .../v92/CreateProjectBadgeTokenTableTest.java | 65 +++++++++++++++++++ .../schema.sql | 1 + 5 files changed, 137 insertions(+) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest/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 eb874b033a9..8958e1331f5 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -623,6 +623,15 @@ CREATE UNIQUE INDEX "UNIQ_PROJECT_ALM_SETTINGS" ON "PROJECT_ALM_SETTINGS"("PROJE CREATE INDEX "PROJECT_ALM_SETTINGS_ALM" ON "PROJECT_ALM_SETTINGS"("ALM_SETTING_UUID"); CREATE INDEX "PROJECT_ALM_SETTINGS_SLUG" ON "PROJECT_ALM_SETTINGS"("ALM_SLUG"); +CREATE TABLE "PROJECT_BADGE_TOKEN"( + "UUID" VARCHAR(40) NOT NULL, + "TOKEN" VARCHAR(255) NOT NULL, + "PROJECT_UUID" VARCHAR(40) NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "PROJECT_BADGE_TOKEN" ADD CONSTRAINT "PK_PROJECT_BADGE_TOKEN" PRIMARY KEY("UUID"); + CREATE TABLE "PROJECT_BRANCHES"( "UUID" VARCHAR(50) NOT NULL, "PROJECT_UUID" VARCHAR(50) NOT NULL, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTable.java new file mode 100644 index 00000000000..89824a7d0d7 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTable.java @@ -0,0 +1,61 @@ +/* + * 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.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateProjectBadgeTokenTable extends DdlChange { + + public static final String PROJECT_BADGE_TOKEN_TABLE = "project_badge_token"; + + public CreateProjectBadgeTokenTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + if (tableExists()) { + return; + } + + context.execute(new CreateTableBuilder(getDialect(), PROJECT_BADGE_TOKEN_TABLE) + .addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(UUID_SIZE).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName("token").setIsNullable(false).setLimit(255).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName("project_uuid").setIsNullable(false).setLimit(UUID_SIZE).build()) + .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build()) + .addColumn(newBigIntegerColumnDefBuilder().setColumnName("updated_at").setIsNullable(false).build()) + .build()); + } + + private boolean tableExists() throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.tableExists(PROJECT_BADGE_TOKEN_TABLE, connection); + } + } + +} 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 index 778bea48082..fc9a371e3f8 100644 --- 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 @@ -37,6 +37,7 @@ public class DbVersion92 implements DbVersion { .add(6109, "Create table 'portfolio_proj_branches'", CreatePortfolioProjectBranchesTable.class) .add(6110, "Add column 'branch_key' to table 'portfolios'", AddBranchToPortfolios.class) .add(6111, "Change size of column 'kee' in 'components'", AlterKeeInComponentsTable.class) + .add(6112, "Create 'project_badge_token' Table", CreateProjectBadgeTokenTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest.java new file mode 100644 index 00000000000..888cea64912 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest.java @@ -0,0 +1,65 @@ +/* + * 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 java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; + +public class CreateProjectBadgeTokenTableTest { + + private static final String TABLE_NAME = "project_badge_token"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateProjectBadgeTokenTableTest.class, "schema.sql"); + + private final DdlChange underTest = new CreateProjectBadgeTokenTable(db.database()); + + @Test + public void migration_should_create_a_table_with_index() throws SQLException { + db.assertTableDoesNotExist(TABLE_NAME); + + underTest.execute(); + + db.assertTableExists(TABLE_NAME); + db.assertPrimaryKey(TABLE_NAME, "pk_project_badge_token", "uuid"); + db.assertColumnDefinition(TABLE_NAME, "uuid", Types.VARCHAR, UUID_SIZE, false); + db.assertColumnDefinition(TABLE_NAME, "token", Types.VARCHAR, 255, false); + db.assertColumnDefinition(TABLE_NAME, "project_uuid", Types.VARCHAR, UUID_SIZE, false); + db.assertColumnDefinition(TABLE_NAME, "updated_at", Types.BIGINT, 20, false); + db.assertColumnDefinition(TABLE_NAME, "created_at", Types.BIGINT, 20, false); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + db.assertTableDoesNotExist(TABLE_NAME); + + underTest.execute(); + //re-entrant + underTest.execute(); + + db.assertTableExists(TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest/schema.sql new file mode 100644 index 00000000000..f90dd9beb54 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest/schema.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS project_badge_token; -- 2.39.5