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;
}
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()));
}
}
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);
}
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) {
}
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) {
*/
private String kee;
- /**
- * Key type, as provided by {@link KeyType}.
- * Not null.
- */
- private KeyType keyType;
-
/**
* Branch type, as provided by {@link BranchType}.
* Not null.
return this;
}
- BranchDto setKeyType(KeyType keyType) {
- this.keyType = keyType;
- return this;
- }
-
public BranchType getBranchType() {
return branchType;
}
"uuid='" + uuid + '\'' +
", projectUuid='" + projectUuid + '\'' +
", kee='" + kee + '\'' +
- ", keyType=" + keyType +
", branchType=" + branchType +
", mergeBranchUuid='" + mergeBranchUuid + '\'' +
", excludeFromPurge=" + excludeFromPurge +
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);
+++ /dev/null
-/*
- * 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
-}
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 {
private String ruleKey;
private String ruleRepo;
private String branchKey;
- private KeyType keyType;
+ private BranchType branchType;
public String getKey() {
return kee;
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;
}
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;
*/
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;
}
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 {
Long sumNclocOfBiggestBranch(
@Param("ncloc") String nclocKey,
- @Param("branch") KeyType branchOrPullRequest,
@Param("branchType") BranchType branchType,
@Param("organizationUuid") String organizationUuid,
@Param("private") Boolean privateProject,
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;
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;
}
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);
List<OrganizationWithNclocDto> selectOrganizationsWithNcloc(
@Param("ncloc") String ncloc,
@Param("organizationUuids") List<String> organizationUuids,
- @Param("branch") KeyType branchOrPullRequest,
@Param("branchType") BranchType branchType);
}
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,
uuid,
project_uuid,
kee,
- key_type,
branch_type,
merge_branch_uuid,
pull_request_binary,
#{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},
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">
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}
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
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 <> #{projectUuidToExclude,jdbcType=VARCHAR}
</if>
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
"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,
"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,
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
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;
assertThat(fp.getRuleKey()).isNotNull();
assertThat(fp.getStatus()).isNotNull();
assertThat(fp.getBranchKey()).isEqualTo("feature/foo");
- assertThat(fp.getKeyType()).isEqualTo(KeyType.BRANCH);
assertThat(fp.getIssueUpdateDate()).isNotNull();
}
.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)
+
+ ;
}
}
--- /dev/null
+/*
+ * 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());
+ }
+}
--- /dev/null
+/*
+ * 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;
+ });
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+}
--- /dev/null
+/*
+ * 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
+ );
+ }
+}
--- /dev/null
+/*
+ * 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
+ );
+ }
+}
--- /dev/null
+/*
+ * 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
+ );
+ }
+}
--- /dev/null
+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");
--- /dev/null
+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");
--- /dev/null
+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");
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;
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());