From 77f20a5c300a8f1576c20185de265119ef2fc8a9 Mon Sep 17 00:00:00 2001 From: Jacek Date: Tue, 11 Jan 2022 09:25:36 +0100 Subject: [PATCH] SONAR-14929 Create 'new_code_reference_issues' table --- server/sonar-db-dao/src/schema/schema-sq.ddl | 8 +++ ...eIndexForNewCodeReferenceBranchIssues.java | 51 ++++++++++++++++ .../CreateNewCodeReferenceBranchIssues.java | 58 +++++++++++++++++++ .../db/migration/version/v93/DbVersion93.java | 2 + ...exForNewCodeReferenceBranchIssuesTest.java | 55 ++++++++++++++++++ ...reateNewCodeReferenceBranchIssuesTest.java | 54 +++++++++++++++++ .../schema.sql | 6 ++ 7 files changed, 234 insertions(+) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssues.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssues.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssuesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest/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 cbeabcbe8bc..d2c2746bd79 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -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 index 00000000000..5be4f6c33cf --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssues.java @@ -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 index 00000000000..8b7479548b7 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssues.java @@ -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); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java index db2ce3563e4..3df3325c50d 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java @@ -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 index 00000000000..e6bb6d0959e --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest.java @@ -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 index 00000000000..a690e7259de --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/CreateNewCodeReferenceBranchIssuesTest.java @@ -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 index 00000000000..dd78fcab532 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v93/CreateIndexForNewCodeReferenceBranchIssuesTest/schema.sql @@ -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"); -- 2.39.5