]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13861 Remove column 'key_type" from the table 'project_branches'
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 22 Sep 2020 21:08:24 +0000 (16:08 -0500)
committersonartech <sonartech@sonarsource.com>
Wed, 30 Sep 2020 20:07:46 +0000 (20:07 +0000)
29 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssuesLoader.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyType.java [deleted file]
server/sonar-db-dao/src/main/java/org/sonar/db/issue/PrIssueDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DbVersion85.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyType.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchType.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullable.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest/schema.sql [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java

index 68bd4404074bd3735cfdd68ed5ec40a13c5c434f..21aaed78c357e3bdb0c9958751f8bf911f36bacb 100644 (file)
@@ -31,8 +31,8 @@ import org.sonar.ce.task.projectanalysis.component.SiblingComponentsWithOpenIssu
 import org.sonar.core.issue.DefaultIssue;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.KeyType;
 import org.sonar.db.issue.IssueDto;
 import org.sonar.db.issue.PrIssueDto;
 
@@ -69,7 +69,7 @@ public class SiblingsIssuesLoader {
   }
 
   private static SiblingIssue toSiblingIssue(PrIssueDto dto) {
-    Preconditions.checkState(dto.getKeyType().equals(KeyType.PULL_REQUEST), "Expected all issues to belong to P/Rs");
+    Preconditions.checkState(dto.getBranchType().equals(BranchType.PULL_REQUEST), "Expected all issues to belong to P/Rs");
     return new SiblingIssue(dto.getKey(), dto.getLine(), dto.getMessage(), dto.getChecksum(), dto.getRuleKey(), dto.getStatus(), dto.getBranchKey(),
       longToDate(dto.getIssueUpdateDate()));
   }
index 0898737cec8bc55c60b7203097ceeaae035ded87..eea97147e498d43df9965174f6da88d60ecfe614 100644 (file)
@@ -41,27 +41,17 @@ public class BranchDao implements Dao {
   }
 
   public void insert(DbSession dbSession, BranchDto dto) {
-    setKeyType(dto);
     mapper(dbSession).insert(dto, system2.now());
   }
 
   public void upsert(DbSession dbSession, BranchDto dto) {
     BranchMapper mapper = mapper(dbSession);
     long now = system2.now();
-    setKeyType(dto);
     if (mapper.update(dto, now) == 0) {
       mapper.insert(dto, now);
     }
   }
 
-  private static void setKeyType(BranchDto dto) {
-    if (dto.getBranchType() == BranchType.PULL_REQUEST) {
-      dto.setKeyType(KeyType.PULL_REQUEST);
-    } else {
-      dto.setKeyType(KeyType.BRANCH);
-    }
-  }
-
   public int updateMainBranchName(DbSession dbSession, String projectUuid, String newBranchKey) {
     long now = system2.now();
     return mapper(dbSession).updateMainBranchName(projectUuid, newBranchKey, now);
@@ -73,7 +63,7 @@ public class BranchDao implements Dao {
   }
 
   public Optional<BranchDto> selectByBranchKey(DbSession dbSession, String projectUuid, String key) {
-    return selectByKey(dbSession, projectUuid, key, KeyType.BRANCH);
+    return selectByKey(dbSession, projectUuid, key, BranchType.BRANCH);
   }
 
   public List<BranchDto> selectByBranchKeys(DbSession dbSession, Map<String, String> branchKeyByProjectUuid) {
@@ -84,11 +74,11 @@ public class BranchDao implements Dao {
   }
 
   public Optional<BranchDto> selectByPullRequestKey(DbSession dbSession, String projectUuid, String key) {
-    return selectByKey(dbSession, projectUuid, key, KeyType.PULL_REQUEST);
+    return selectByKey(dbSession, projectUuid, key, BranchType.PULL_REQUEST);
   }
 
-  private static Optional<BranchDto> selectByKey(DbSession dbSession, String projectUuid, String key, KeyType keyType) {
-    return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, key, keyType));
+  private static Optional<BranchDto> selectByKey(DbSession dbSession, String projectUuid, String key, BranchType branchType) {
+    return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, key, branchType));
   }
 
   public Collection<BranchDto> selectByComponent(DbSession dbSession, ComponentDto component) {
index cfdb4dfbdacc081f679e9ce342f0dea957cad290..72a6952333d1da2d838058b31c4e1998257dc7cc 100644 (file)
@@ -60,12 +60,6 @@ public class BranchDto {
    */
   private String kee;
 
-  /**
-   * Key type, as provided by {@link KeyType}.
-   * Not null.
-   */
-  private KeyType keyType;
-
   /**
    * Branch type, as provided by {@link BranchType}.
    * Not null.
@@ -138,11 +132,6 @@ public class BranchDto {
     return this;
   }
 
-  BranchDto setKeyType(KeyType keyType) {
-    this.keyType = keyType;
-    return this;
-  }
-
   public BranchType getBranchType() {
     return branchType;
   }
@@ -239,7 +228,6 @@ public class BranchDto {
       "uuid='" + uuid + '\'' +
       ", projectUuid='" + projectUuid + '\'' +
       ", kee='" + kee + '\'' +
-      ", keyType=" + keyType +
       ", branchType=" + branchType +
       ", mergeBranchUuid='" + mergeBranchUuid + '\'' +
       ", excludeFromPurge=" + excludeFromPurge +
index 60a017fcac3067e18184e77af5c2ee27c9151d6b..c44507015fd1fc43fe2c7a104e6c82f69848d1e3 100644 (file)
@@ -38,7 +38,7 @@ public interface BranchMapper {
   int updateExcludeFromPurge(@Param("uuid") String uuid, @Param("excludeFromPurge") boolean excludeFromPurge,
     @Param("now") long now);
 
-  BranchDto selectByKey(@Param("projectUuid") String projectUuid, @Param("key") String key, @Param("keyType") KeyType keyType);
+  BranchDto selectByKey(@Param("projectUuid") String projectUuid, @Param("key") String key, @Param("branchType") BranchType branchType);
 
   BranchDto selectByUuid(@Param("uuid") String uuid);
 
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyType.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyType.java
deleted file mode 100644 (file)
index 1ee617a..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2020 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.db.component;
-
-public enum KeyType {
-  BRANCH,
-
-  PULL_REQUEST
-}
index ccf39fba9fce2334b674a94b6567e72ec533d307..94a2653fa8c799dc1dd586f9bbde757c3361abcc 100644 (file)
@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 import org.sonar.api.rule.RuleKey;
-import org.sonar.db.component.KeyType;
+import org.sonar.db.component.BranchType;
 
 public final class PrIssueDto implements Serializable {
 
@@ -40,7 +40,7 @@ public final class PrIssueDto implements Serializable {
   private String ruleKey;
   private String ruleRepo;
   private String branchKey;
-  private KeyType keyType;
+  private BranchType branchType;
 
   public String getKey() {
     return kee;
@@ -83,12 +83,12 @@ public final class PrIssueDto implements Serializable {
     return this;
   }
 
-  public KeyType getKeyType() {
-    return keyType;
+  public BranchType getBranchType() {
+    return branchType;
   }
 
-  public PrIssueDto setKeyType(KeyType s) {
-    this.keyType = s;
+  public PrIssueDto setBranchType(BranchType s) {
+    this.branchType = s;
     return this;
   }
 
index 4b778ff52d05a71131e1f93851220aef1b5dcbcb..d4abbce6721b47bea15edaee30ffd907efb2ff80 100644 (file)
@@ -30,7 +30,6 @@ import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.KeyType;
 import org.sonar.db.dialect.Dialect;
 
 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
@@ -101,7 +100,7 @@ public class LiveMeasureDao implements Dao {
    */
   public long sumNclocOfBiggestBranch(DbSession dbSession, SumNclocDbQuery dbQuery) {
     Long ncloc = mapper(dbSession).sumNclocOfBiggestBranch(
-      NCLOC_KEY, KeyType.BRANCH, BranchType.BRANCH, dbQuery.getOrganizationUuid(), dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude());
+      NCLOC_KEY, BranchType.BRANCH, dbQuery.getOrganizationUuid(), dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude());
     return ncloc == null ? 0L : ncloc;
   }
 
index 5f805a369797c3cdc7e835d1bbabb90eb3ee9446..22e5ee92cc10e79b27c3bae9d284ec726889f8a2 100644 (file)
@@ -25,7 +25,6 @@ import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.session.ResultHandler;
 import org.sonar.db.component.BranchType;
-import org.sonar.db.component.KeyType;
 
 public interface LiveMeasureMapper {
 
@@ -58,7 +57,6 @@ public interface LiveMeasureMapper {
 
   Long sumNclocOfBiggestBranch(
     @Param("ncloc") String nclocKey,
-    @Param("branch") KeyType branchOrPullRequest,
     @Param("branchType") BranchType branchType,
     @Param("organizationUuid") String organizationUuid,
     @Param("private") Boolean privateProject,
index b81990c3ea2193eddd49bca19def63fcff7cc42b..f120445e3bfb2852d2fecc28daa0b151db2aa4ea 100644 (file)
@@ -29,7 +29,6 @@ import org.sonar.db.DbSession;
 import org.sonar.db.Pagination;
 import org.sonar.db.alm.ALM;
 import org.sonar.db.component.BranchType;
-import org.sonar.db.component.KeyType;
 import org.sonar.db.qualitygate.QGateWithOrgDto;
 import org.sonar.db.user.GroupDto;
 
@@ -145,7 +144,7 @@ public class OrganizationDao implements Dao {
   public List<OrganizationWithNclocDto> selectOrganizationsWithNcloc(DbSession dbSession, List<String> organizationUuids) {
     List<OrganizationWithNclocDto> result = new ArrayList<>();
     executeLargeUpdates(organizationUuids, chunk ->
-      result.addAll(getMapper(dbSession).selectOrganizationsWithNcloc(NCLOC_KEY, chunk, KeyType.BRANCH, BranchType.BRANCH))
+      result.addAll(getMapper(dbSession).selectOrganizationsWithNcloc(NCLOC_KEY, chunk, BranchType.BRANCH))
     );
     return result;
   }
index fdf43b88c0bbf66d45103a422b9ff21a4b433f57..8e806bda37bea75dab80205b2ad5584ee83eb61c 100644 (file)
@@ -24,7 +24,6 @@ import javax.annotation.CheckForNull;
 import org.apache.ibatis.annotations.Param;
 import org.sonar.db.Pagination;
 import org.sonar.db.component.BranchType;
-import org.sonar.db.component.KeyType;
 
 public interface OrganizationMapper {
   void insert(@Param("organization") OrganizationDto organization, @Param("newProjectPrivate") boolean newProjectPrivate);
@@ -80,7 +79,6 @@ public interface OrganizationMapper {
   List<OrganizationWithNclocDto> selectOrganizationsWithNcloc(
     @Param("ncloc") String ncloc,
     @Param("organizationUuids") List<String> organizationUuids,
-    @Param("branch") KeyType branchOrPullRequest,
     @Param("branchType") BranchType branchType);
 
 }
index af4785fd8d97c0ea0a0b61e3d0d5dc9e24e0d2e4..dccb832be366e1eb01d4f3f5a47eb7b67123da5b 100644 (file)
@@ -6,7 +6,6 @@
     pb.uuid as uuid,
     pb.project_uuid as projectUuid,
     pb.kee as kee,
-    pb.key_type as keyType,
     pb.branch_type as branchType,
     pb.merge_branch_uuid as mergeBranchUuid,
     pb.pull_request_binary as pullRequestBinary,
@@ -19,7 +18,6 @@
     uuid,
     project_uuid,
     kee,
-    key_type,
     branch_type,
     merge_branch_uuid,
     pull_request_binary,
@@ -31,7 +29,6 @@
     #{dto.uuid, jdbcType=VARCHAR},
     #{dto.projectUuid, jdbcType=VARCHAR},
     #{dto.kee, jdbcType=VARCHAR},
-    #{dto.keyType, jdbcType=VARCHAR},
     #{dto.branchType, jdbcType=VARCHAR},
     #{dto.mergeBranchUuid, jdbcType=VARCHAR},
     #{dto.pullRequestBinary, jdbcType=BINARY},
@@ -76,7 +73,7 @@
     where
       pb.project_uuid = #{projectUuid, jdbcType=VARCHAR} and
       pb.kee = #{key, jdbcType=VARCHAR} and
-      pb.key_type = #{keyType, jdbcType=VARCHAR}
+      pb.branch_type = #{branchType, jdbcType=VARCHAR}
   </select>
 
   <select id="selectByBranchKeys" resultType="org.sonar.db.component.BranchDto">
index 89de35765863728b66c11e49d51b4cb9010a8855..d3a1d7ae509dc86a941a05baaf082c7b57707bf4 100644 (file)
     inner join components p on b.project_uuid = p.uuid
     where
       m.name = 'ncloc'
-      and b.key_type = 'BRANCH'
     and b.branch_type = 'BRANCH'
     and p.enabled = ${_true}
     and p.private = ${_true}
index 5dc62303552e488d718617780d76ed5dc7b45eb1..2056461e95a65cda781bccdbeb6c8e3be18ece7d 100644 (file)
       r.plugin_rule_key as ruleKey,
       r.plugin_name as ruleRepo,
       b.kee as branchKey,
-      b.key_type as keyType
+      b.branch_type as branchType
     from issues i
     inner join rules r on r.uuid = i.rule_uuid
     inner join project_branches b on i.project_uuid = b.uuid
index b04fccfefca32fd71355aecd845d4f653996454a..499640bead1bd8a56343193f83ee587dfa024a89 100644 (file)
@@ -68,7 +68,6 @@
         and p.copy_component_uuid is null
         and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
         and b.branch_type = #{branchType, jdbcType=VARCHAR}
-        and b.key_type = #{branch, jdbcType=VARCHAR}
         <if test="projectUuidToExclude != null">
           and b.project_uuid &lt;&gt; #{projectUuidToExclude,jdbcType=VARCHAR}
         </if>
index b7d9744bf949e69d0cf78577c2fea6b2cea275d4..449b2b73bc5f2acae41aea7905939e95c84c396f 100644 (file)
     and p.copy_component_uuid is null
     and p.organization_uuid in <foreach collection="organizationUuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
     and b.branch_type = #{branchType, jdbcType=VARCHAR}
-    and b.key_type = #{branch, jdbcType=VARCHAR}
     group by b.project_uuid, p.organization_uuid
     ) sumncloc
     group by orgUuid
index e4f53071172b1f8daf75015266a489e9b4c0b35c..ae5bb2b7ca8c82fa61a1461b4d3039c8cf008cb0 100644 (file)
@@ -629,9 +629,8 @@ CREATE TABLE "PROJECT_BRANCHES"(
     "UUID" VARCHAR(50) NOT NULL,
     "PROJECT_UUID" VARCHAR(50) NOT NULL,
     "KEE" VARCHAR(255) NOT NULL,
-    "BRANCH_TYPE" VARCHAR(12),
+    "BRANCH_TYPE" VARCHAR(12) NOT NULL,
     "MERGE_BRANCH_UUID" VARCHAR(50),
-    "KEY_TYPE" VARCHAR(12) NOT NULL,
     "PULL_REQUEST_BINARY" BLOB,
     "MANUAL_BASELINE_ANALYSIS_UUID" VARCHAR(40),
     "CREATED_AT" BIGINT NOT NULL,
@@ -640,7 +639,7 @@ CREATE TABLE "PROJECT_BRANCHES"(
     "NEED_ISSUE_SYNC" BOOLEAN NOT NULL
 );
 ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID");
-CREATE UNIQUE INDEX "PROJECT_BRANCHES_KEE_KEY_TYPE" ON "PROJECT_BRANCHES"("PROJECT_UUID", "KEE", "KEY_TYPE");
+CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE", "PROJECT_UUID", "KEE");
 
 CREATE TABLE "PROJECT_LINKS"(
     "UUID" VARCHAR(40) NOT NULL,
index 48a786bde79b8d5c43e826283faa01c7c94832a9..feda89a2963a41628a8777e0356796a745ff3d8e 100644 (file)
@@ -59,7 +59,7 @@ public class BranchDtoTest {
     underTest.setExcludeFromPurge(true);
 
     assertThat(underTest.toString()).isEqualTo("BranchDto{uuid='U1', " +
-      "projectUuid='U2', kee='K1', keyType=null, branchType=BRANCH, mergeBranchUuid='U3', excludeFromPurge=true, needIssueSync=false}");
+      "projectUuid='U2', kee='K1', branchType=BRANCH, mergeBranchUuid='U3', excludeFromPurge=true, needIssueSync=false}");
   }
 
   @Test
index 1621814ababd5bdf9a0891260d2523647d6f4de9..6813a473440d3d03b365ea929052cfb008149dc1 100644 (file)
@@ -37,7 +37,6 @@ import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.ComponentUpdateDto;
-import org.sonar.db.component.KeyType;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.rule.RuleDefinitionDto;
 import org.sonar.db.rule.RuleDto;
@@ -237,7 +236,6 @@ public class IssueDaoTest {
     assertThat(fp.getRuleKey()).isNotNull();
     assertThat(fp.getStatus()).isNotNull();
     assertThat(fp.getBranchKey()).isEqualTo("feature/foo");
-    assertThat(fp.getKeyType()).isEqualTo(KeyType.BRANCH);
     assertThat(fp.getIssueUpdateDate()).isNotNull();
   }
 
index 0396ecac844e14614a26ba09847faa799ce4db4f..daeb281ea0015a5c80f74f663cd0a1a479b5c393 100644 (file)
@@ -51,7 +51,11 @@ public class DbVersion85 implements DbVersion {
       .add(4020, "Populate 'message_type' column of 'ce_task_message' table", PopulateMessageTypeColumnOfCeTaskMessageTable.class)
       .add(4021, "Make 'message_type' column not nullable for `ce_task_message` table", MakeMessageTypeColumnNotNullableOnCeTaskMessageTable.class)
       .add(4022, "Add index on 'message_type' column of `ce_task_message` table", AddIndexOnMessageTypeColumnOfCeTaskMessageTable.class)
-      .add(4023, "Create 'user_dismissed_messages' table", CreateUserDismissedMessagesTable.class
-      );
+      .add(4023, "Create 'user_dismissed_messages' table", CreateUserDismissedMessagesTable.class)
+      .add(4024, "Populate 'branch_type' in 'project_branches'", FillProjectBranchesBranchType.class)
+      .add(4025, "Make 'branch_type' in 'project_branches' not nullable", MakeProjectBranchesBranchTypeNotNullable.class)
+      .add(4026, "Drop column 'key_type' in table 'project_branches'", DropProjectBranchesKeyType.class)
+
+    ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyType.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyType.java
new file mode 100644 (file)
index 0000000..2965fb2
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v85;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
+import org.sonar.server.platform.db.migration.sql.DropIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class DropProjectBranchesKeyType extends DdlChange {
+  private static final String TABLE_NAME = "project_branches";
+
+  public DropProjectBranchesKeyType(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new DropIndexBuilder(getDialect()).setTable(TABLE_NAME).setName("project_branches_kee_key_type").build());
+    context.execute(new DropColumnsBuilder(getDialect(), TABLE_NAME, "key_type").build());
+    context.execute(new CreateIndexBuilder()
+      .setTable(TABLE_NAME)
+      .setUnique(true)
+      .setName("uniq_project_branches")
+      .addColumn("branch_type")
+      .addColumn("project_uuid")
+      .addColumn("kee")
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchType.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchType.java
new file mode 100644 (file)
index 0000000..38796ac
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v85;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+
+public class FillProjectBranchesBranchType extends DataChange {
+  public FillProjectBranchesBranchType(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("select uuid, key_type from project_branches where branch_type is null");
+    massUpdate.update("update project_branches set branch_type = ? where uuid = ?");
+    massUpdate.execute((row, update) -> {
+      String uuid = row.getString(1);
+      String type = row.getString(2);
+
+      update.setString(1, type);
+      update.setString(2, uuid);
+      return true;
+    });
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullable.java
new file mode 100644 (file)
index 0000000..838dd8f
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v85;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class MakeProjectBranchesBranchTypeNotNullable extends DdlChange {
+  public MakeProjectBranchesBranchTypeNotNullable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AlterColumnsBuilder(getDialect(), "project_branches")
+      .updateColumn(newVarcharColumnDefBuilder()
+        .setLimit(12)
+        .setIsNullable(false)
+        .setColumnName("branch_type")
+        .build()).build());
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest.java
new file mode 100644 (file)
index 0000000..c8a3dae
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v85;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.MigrationStep;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DropProjectBranchesKeyTypeTest {
+  private static final String TABLE_NAME = "project_branches";
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(DropProjectBranchesKeyTypeTest.class, "schema.sql");
+
+  private MigrationStep underTest = new DropProjectBranchesKeyType(db.database());
+
+  @Test
+  public void drops_table() throws SQLException {
+    insertData(1, "PULL_REQUEST", "PULL_REQUEST");
+    insertData(2, "BRANCH", "BRANCH");
+
+    db.assertColumnDefinition(TABLE_NAME, "key_type", Types.VARCHAR, 12, false);
+
+    underTest.execute();
+    db.assertIndexDoesNotExist(TABLE_NAME, "project_branches_kee_key_type");
+    db.assertUniqueIndex(TABLE_NAME, "uniq_project_branches", "branch_type", "project_uuid", "kee");
+    db.assertColumnDoesNotExist(TABLE_NAME, "key_type");
+    assertThat(db.countRowsOfTable(TABLE_NAME)).isEqualTo(2);
+  }
+
+  private void insertData(int id, String keyType, @Nullable String branchType) {
+    db.executeInsert(TABLE_NAME,
+      "uuid", "uuid" + id,
+      "project_uuid", "project" + id,
+      "kee", "key" + id,
+      "key_type", keyType,
+      "created_at", id,
+      "updated_at", id + 1,
+      "need_issue_sync", true,
+      "branch_type", branchType
+    );
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest.java
new file mode 100644 (file)
index 0000000..258730a
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v85;
+
+import java.sql.SQLException;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.MigrationStep;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class FillProjectBranchesBranchTypeTest {
+  private static final String TABLE_NAME = "project_branches";
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(FillProjectBranchesBranchTypeTest.class, "schema.sql");
+
+  private MigrationStep underTest = new FillProjectBranchesBranchType(db.database());
+
+  @Test
+  public void drops_table() throws SQLException {
+    insertData(1, "PULL_REQUEST", "PULL_REQUEST");
+    insertData(2, "PULL_REQUEST", null);
+    insertData(3, "BRANCH", null);
+    insertData(4, "BRANCH", "BRANCH");
+
+    underTest.execute();
+    assertThat(db.select("select uuid, key_type, branch_type from project_branches"))
+      .extracting(m -> m.get("UUID"), m -> m.get("KEY_TYPE"), m -> m.get("BRANCH_TYPE"))
+      .containsOnly(
+        tuple("uuid1", "PULL_REQUEST", "PULL_REQUEST"),
+        tuple("uuid2", "PULL_REQUEST", "PULL_REQUEST"),
+        tuple("uuid3", "BRANCH", "BRANCH"),
+        tuple("uuid4", "BRANCH", "BRANCH"));
+
+  }
+
+  private void insertData(int id, String keyType, @Nullable String branchType) {
+    db.executeInsert(TABLE_NAME,
+      "uuid", "uuid" + id,
+      "project_uuid", "project" + id,
+      "kee", "key" + id,
+      "key_type", keyType,
+      "created_at", id,
+      "updated_at", id + 1,
+      "need_issue_sync", true,
+      "branch_type", branchType
+    );
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest.java
new file mode 100644 (file)
index 0000000..1c8d163
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v85;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.MigrationStep;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MakeProjectBranchesBranchTypeNotNullableTest {
+  private static final String TABLE_NAME = "project_branches";
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(MakeProjectBranchesBranchTypeNotNullableTest.class, "schema.sql");
+
+  private MigrationStep underTest = new MakeProjectBranchesBranchTypeNotNullable(db.database());
+
+  @Test
+  public void drops_table() throws SQLException {
+    insertData(1, "PULL_REQUEST", "PULL_REQUEST");
+    insertData(2, "BRANCH", "BRANCH");
+
+    db.assertColumnDefinition(TABLE_NAME, "branch_type", Types.VARCHAR, 12, true);
+
+    underTest.execute();
+
+    db.assertColumnDefinition(TABLE_NAME, "branch_type", Types.VARCHAR, 12, false);
+    assertThat(db.countRowsOfTable(TABLE_NAME)).isEqualTo(2);
+  }
+
+  private void insertData(int id, String keyType, @Nullable String branchType) {
+    db.executeInsert(TABLE_NAME,
+      "uuid", "uuid" + id,
+      "project_uuid", "project" + id,
+      "kee", "key" + id,
+      "key_type", keyType,
+      "created_at", id,
+      "updated_at", id + 1,
+      "need_issue_sync", true,
+      "branch_type", branchType
+    );
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest/schema.sql
new file mode 100644 (file)
index 0000000..93927b5
--- /dev/null
@@ -0,0 +1,16 @@
+CREATE TABLE "PROJECT_BRANCHES"(
+    "UUID" VARCHAR(50) NOT NULL,
+    "PROJECT_UUID" VARCHAR(50) NOT NULL,
+    "KEE" VARCHAR(255) NOT NULL,
+    "BRANCH_TYPE" VARCHAR(12) NOT NULL,
+    "MERGE_BRANCH_UUID" VARCHAR(50),
+    "KEY_TYPE" VARCHAR(12) NOT NULL,
+    "PULL_REQUEST_BINARY" BLOB,
+    "MANUAL_BASELINE_ANALYSIS_UUID" VARCHAR(40),
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL,
+    "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL,
+    "NEED_ISSUE_SYNC" BOOLEAN NOT NULL
+);
+ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "PROJECT_BRANCHES_KEE_KEY_TYPE" ON "PROJECT_BRANCHES"("PROJECT_UUID", "KEE", "KEY_TYPE");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest/schema.sql
new file mode 100644 (file)
index 0000000..094c328
--- /dev/null
@@ -0,0 +1,16 @@
+CREATE TABLE "PROJECT_BRANCHES"(
+    "UUID" VARCHAR(50) NOT NULL,
+    "PROJECT_UUID" VARCHAR(50) NOT NULL,
+    "KEE" VARCHAR(255) NOT NULL,
+    "BRANCH_TYPE" VARCHAR(12),
+    "MERGE_BRANCH_UUID" VARCHAR(50),
+    "KEY_TYPE" VARCHAR(12) NOT NULL,
+    "PULL_REQUEST_BINARY" BLOB,
+    "MANUAL_BASELINE_ANALYSIS_UUID" VARCHAR(40),
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL,
+    "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL,
+    "NEED_ISSUE_SYNC" BOOLEAN NOT NULL
+);
+ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "PROJECT_BRANCHES_KEE_KEY_TYPE" ON "PROJECT_BRANCHES"("PROJECT_UUID", "KEE", "KEY_TYPE");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest/schema.sql
new file mode 100644 (file)
index 0000000..094c328
--- /dev/null
@@ -0,0 +1,16 @@
+CREATE TABLE "PROJECT_BRANCHES"(
+    "UUID" VARCHAR(50) NOT NULL,
+    "PROJECT_UUID" VARCHAR(50) NOT NULL,
+    "KEE" VARCHAR(255) NOT NULL,
+    "BRANCH_TYPE" VARCHAR(12),
+    "MERGE_BRANCH_UUID" VARCHAR(50),
+    "KEY_TYPE" VARCHAR(12) NOT NULL,
+    "PULL_REQUEST_BINARY" BLOB,
+    "MANUAL_BASELINE_ANALYSIS_UUID" VARCHAR(40),
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL,
+    "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL,
+    "NEED_ISSUE_SYNC" BOOLEAN NOT NULL
+);
+ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "PROJECT_BRANCHES_KEE_KEY_TYPE" ON "PROJECT_BRANCHES"("PROJECT_UUID", "KEE", "KEY_TYPE");
index 13326b680478eec53eb928d3c5d20d18c3cfcd87..17292c537e5f0c5f0405a17ff6c4fdb5c8c2e305 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.newcodeperiod.ws;
 
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -86,6 +87,7 @@ public class ListAction implements NewCodePeriodsWsAction {
       userSession.checkProjectPermission(UserRole.ADMIN, project);
       Collection<BranchDto> branches = dbClient.branchDao().selectByProject(dbSession, project).stream()
         .filter(b -> b.getBranchType() == BranchType.BRANCH)
+        .sorted(Comparator.comparing(BranchDto::getKey))
         .collect(toList());
 
       List<NewCodePeriodDto> newCodePeriods = newCodePeriodDao.selectAllByProject(dbSession, project.getUuid());