Bläddra i källkod

SONAR-13861 Remove column 'key_type" from the table 'project_branches'

tags/8.5.0.37579
Duarte Meneses 3 år sedan
förälder
incheckning
9cf5918d47
28 ändrade filer med 393 tillägg och 63 borttagningar
  1. 2
    2
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssuesLoader.java
  2. 4
    14
      server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java
  3. 0
    12
      server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java
  4. 1
    1
      server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java
  5. 6
    6
      server/sonar-db-dao/src/main/java/org/sonar/db/issue/PrIssueDto.java
  6. 1
    2
      server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
  7. 0
    2
      server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
  8. 1
    2
      server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java
  9. 0
    2
      server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java
  10. 1
    4
      server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml
  11. 0
    1
      server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
  12. 1
    1
      server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
  13. 0
    1
      server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
  14. 0
    1
      server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml
  15. 2
    3
      server/sonar-db-dao/src/schema/schema-sq.ddl
  16. 1
    1
      server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java
  17. 0
    2
      server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java
  18. 6
    2
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DbVersion85.java
  19. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyType.java
  20. 46
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchType.java
  21. 21
    4
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullable.java
  22. 66
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest.java
  23. 70
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest.java
  24. 65
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest.java
  25. 16
    0
      server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest/schema.sql
  26. 16
    0
      server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest/schema.sql
  27. 16
    0
      server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest/schema.sql
  28. 2
    0
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java

+ 2
- 2
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssuesLoader.java Visa fil

@@ -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()));
}

+ 4
- 14
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java Visa fil

@@ -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) {

+ 0
- 12
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java Visa fil

@@ -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 +

+ 1
- 1
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java Visa fil

@@ -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);


+ 6
- 6
server/sonar-db-dao/src/main/java/org/sonar/db/issue/PrIssueDto.java Visa fil

@@ -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;
}


+ 1
- 2
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java Visa fil

@@ -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;
}


+ 0
- 2
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java Visa fil

@@ -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,

+ 1
- 2
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java Visa fil

@@ -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;
}

+ 0
- 2
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java Visa fil

@@ -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);

}

+ 1
- 4
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml Visa fil

@@ -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">

+ 0
- 1
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml Visa fil

@@ -728,7 +728,6 @@
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}

+ 1
- 1
server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml Visa fil

@@ -296,7 +296,7 @@
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

+ 0
- 1
server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml Visa fil

@@ -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>

+ 0
- 1
server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml Visa fil

@@ -203,7 +203,6 @@
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

+ 2
- 3
server/sonar-db-dao/src/schema/schema-sq.ddl Visa fil

@@ -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,

+ 1
- 1
server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDtoTest.java Visa fil

@@ -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

+ 0
- 2
server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java Visa fil

@@ -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();
}


+ 6
- 2
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DbVersion85.java Visa fil

@@ -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)

;
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyType.java Visa fil

@@ -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());
}
}

+ 46
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchType.java Visa fil

@@ -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;
});
}
}

server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyType.java → server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullable.java Visa fil

@@ -17,10 +17,27 @@
* 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;
package org.sonar.server.platform.db.migration.version.v85;

public enum KeyType {
BRANCH,
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;

PULL_REQUEST
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());
}
}

+ 66
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest.java Visa fil

@@ -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
);
}
}

+ 70
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest.java Visa fil

@@ -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
);
}
}

+ 65
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest.java Visa fil

@@ -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
);
}
}

+ 16
- 0
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/DropProjectBranchesKeyTypeTest/schema.sql Visa fil

@@ -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");

+ 16
- 0
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/FillProjectBranchesBranchTypeTest/schema.sql Visa fil

@@ -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");

+ 16
- 0
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v85/MakeProjectBranchesBranchTypeNotNullableTest/schema.sql Visa fil

@@ -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");

+ 2
- 0
server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java Visa fil

@@ -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());

Laddar…
Avbryt
Spara