]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13426 add project_badge_token table
authorPierre <pierre.guillot@sonarsource.com>
Wed, 10 Nov 2021 10:10:31 +0000 (11:10 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 15 Nov 2021 20:04:34 +0000 (20:04 +0000)
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v92/DbVersion92.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v92/CreateProjectBadgeTokenTableTest/schema.sql [new file with mode: 0644]

index eb874b033a933c86b27408812f3cd2dda7472df6..8958e1331f531084fcbe6d6e573404e4ce64491f 100644 (file)
@@ -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 (file)
index 0000000..89824a7
--- /dev/null
@@ -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);
+    }
+  }
+
+}
index 778bea48082d7aab7ad260f6ee44e522de6a960c..fc9a371e3f858b2db59bd0dc098ec39c60ea434b 100644 (file)
@@ -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 (file)
index 0000000..888cea6
--- /dev/null
@@ -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 (file)
index 0000000..f90dd9b
--- /dev/null
@@ -0,0 +1 @@
+DROP TABLE IF EXISTS project_badge_token;