]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14929 Create 'new_code_reference_issues' table
authorJacek <jacek.poreda@sonarsource.com>
Tue, 11 Jan 2022 08:25:36 +0000 (09:25 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 21 Jan 2022 20:03:21 +0000 (20:03 +0000)
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssues.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssues.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssuesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest/schema.sql [new file with mode: 0644]

index cbeabcbe8bc5fdb0edc14ed0c27159fd634b5b44..d2c2746bd79c2b45e036b13a0248f800370d6656 100644 (file)
@@ -490,6 +490,14 @@ CREATE UNIQUE INDEX "UNIQ_NEW_CODE_PERIODS" ON "NEW_CODE_PERIODS"("PROJECT_UUID"
 CREATE INDEX "IDX_NCP_TYPE" ON "NEW_CODE_PERIODS"("TYPE");
 CREATE INDEX "IDX_NCP_VALUE" ON "NEW_CODE_PERIODS"("VALUE");
 
+CREATE TABLE "NEW_CODE_REFERENCE_ISSUES"(
+    "UUID" VARCHAR(40) NOT NULL,
+    "ISSUE_KEY" VARCHAR(50) NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "NEW_CODE_REFERENCE_ISSUES" ADD CONSTRAINT "PK_NEW_CODE_REFERENCE_ISSUES" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_NEW_CODE_REFERENCE_ISSUES" ON "NEW_CODE_REFERENCE_ISSUES"("ISSUE_KEY");
+
 CREATE TABLE "NOTIFICATIONS"(
     "UUID" VARCHAR(40) NOT NULL,
     "DATA" BLOB,
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssues.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssues.java
new file mode 100644 (file)
index 0000000..5be4f6c
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * 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.v93;
+
+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.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class CreateIndexForNewCodeReferenceBranchIssues extends DdlChange {
+
+  private static final String TABLE_NAME = "new_code_reference_issues";
+  private static final String INDEX_NAME = "uniq_new_code_reference_issues";
+
+  public CreateIndexForNewCodeReferenceBranchIssues(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection c = getDatabase().getDataSource().getConnection()) {
+      if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, c)) {
+        context.execute(new CreateIndexBuilder()
+          .setTable(TABLE_NAME)
+          .setName(INDEX_NAME)
+          .addColumn("issue_key")
+          .setUnique(true)
+          .build());
+      }
+    }
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssues.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssues.java
new file mode 100644 (file)
index 0000000..8b74795
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.v93;
+
+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 CreateNewCodeReferenceBranchIssues extends DdlChange {
+
+  private static final String TABLE_NAME = "new_code_reference_issues";
+
+  public CreateNewCodeReferenceBranchIssues(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    if (tableExists()) {
+      return;
+    }
+
+    context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME)
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(UUID_SIZE).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName("issue_key").setIsNullable(false).setLimit(50).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build())
+      .build());
+  }
+
+  private boolean tableExists() throws SQLException {
+    try (var connection = getDatabase().getDataSource().getConnection()) {
+      return DatabaseUtils.tableExists(TABLE_NAME, connection);
+    }
+  }
+}
index db2ce3563e4834d82c65f1613a424deb1ead4ef4..3df3325c50d8f9df944a55325913fa911bd1ecd4 100644 (file)
@@ -30,6 +30,8 @@ public class DbVersion93 implements DbVersion {
       .add(6202, "Drop index 'uniq_portfolio_references'", DropUniqPortfolioReferencesIndex.class)
       .add(6203, "Add column 'branch_uuid' to 'portfolio_references'", AddBranchToPortfolioReferences.class)
       .add(6204, "Create index 'uniq_portfolio_references'", CreateIndexForPortfolioReferences.class)
+      .add(6205, "Create table 'new_code_reference_branch_issues'", CreateNewCodeReferenceBranchIssues.class)
+      .add(6206, "Create index 'uniq_new_code_reference_issues'", CreateIndexForNewCodeReferenceBranchIssues.class)
 
     ;
   }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest.java
new file mode 100644 (file)
index 0000000..e6bb6d0
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.v93;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+
+public class CreateIndexForNewCodeReferenceBranchIssuesTest {
+  private static final String TABLE_NAME = "new_code_reference_issues";
+  private static final String INDEX_NAME = "uniq_new_code_reference_issues";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForNewCodeReferenceBranchIssuesTest.class, "schema.sql");
+
+  private final CreateIndexForNewCodeReferenceBranchIssues underTest = new CreateIndexForNewCodeReferenceBranchIssues(db.database());
+
+  @Test
+  public void should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME);
+    underTest.execute();
+    db.assertUniqueIndex(TABLE_NAME, INDEX_NAME, "issue_key");
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    db.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME);
+
+    underTest.execute();
+    //re-entrant
+    underTest.execute();
+
+    db.assertUniqueIndex(TABLE_NAME, INDEX_NAME, "issue_key");
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssuesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssuesTest.java
new file mode 100644 (file)
index 0000000..a690e72
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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.v93;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+public class CreateNewCodeReferenceBranchIssuesTest {
+  private static final String TABLE_NAME = "new_code_reference_issues";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createEmpty();
+
+  private final CreateNewCodeReferenceBranchIssues underTest = new CreateNewCodeReferenceBranchIssues(db.database());
+
+  @Test
+  public void migration_should_create_table() throws SQLException {
+    db.assertTableDoesNotExist(TABLE_NAME);
+
+    underTest.execute();
+
+    db.assertTableExists(TABLE_NAME);
+  }
+
+  @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/v93/CreateIndexForNewCodeReferenceBranchIssuesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest/schema.sql
new file mode 100644 (file)
index 0000000..dd78fca
--- /dev/null
@@ -0,0 +1,6 @@
+CREATE TABLE "NEW_CODE_REFERENCE_ISSUES"(
+    "UUID" VARCHAR(40) NOT NULL,
+    "ISSUE_KEY" VARCHAR(50) NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "NEW_CODE_REFERENCE_ISSUES" ADD CONSTRAINT "PK_NEW_CODE_REFERENCE_ISSUES" PRIMARY KEY("UUID");