}
public void delete(DbSession dbSession, PortfolioDto portfolio) {
- mapper(dbSession).deletePortfoliosByUuids(singleton(portfolio.getUuid()));
mapper(dbSession).deleteReferencesByPortfolioOrReferenceUuids(singleton(portfolio.getUuid()));
- mapper(dbSession).deleteProjectsByPortfolioUuids(singleton(portfolio.getUuid()));
+ mapper(dbSession).deletePortfolio(portfolio.getUuid());
auditPersister.deleteComponent(dbSession, toComponentNewValue(portfolio));
}
+ /**
+ * Does NOT delete related references and project/branch selections!
+ */
public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
// not audited but it's part of DefineWs
mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
mapper(dbSession).deleteAllProjects();
}
- public Set<String> selectBranches(DbSession dbSession, String portfolioProjectUuid) {
- return mapper(dbSession).selectBranches(portfolioProjectUuid);
- }
-
public void addBranch(DbSession dbSession, String portfolioProjectUuid, String branchUuid) {
mapper(dbSession).insertBranch(uuidFactory.create(), portfolioProjectUuid, branchUuid, system2.now());
}
void insert(PortfolioDto portfolio);
- void deleteByUuids(@Param("uuids") Set<String> uuids);
-
- void deletePortfoliosByUuids(@Param("uuids") Set<String> uuids);
+ void deletePortfolio(@Param("uuid") String uuid);
void deleteReferencesByPortfolioOrReferenceUuids(@Param("uuids") Set<String> uuids);
- void deleteProjectsByPortfolioUuids(@Param("uuids") Set<String> uuids);
-
void insertReference(PortfolioReferenceDto portfolioReference);
void insertProject(@Param("uuid") String uuid, @Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid, @Param("createdAt") long createdAt);
List<ReferenceDto> selectAllReferencesInHierarchy(String rootUuid);
- void deleteBranch(@Param("portfolioProjectUuid") String portfolioProjectUuid, @Param("branchUuid") String branchUuid);
-
void deleteBranch(@Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
void insertBranch(@Param("uuid") String uuid, @Param("portfolioProjectUuid") String portfolioProjectUuid, @Param("branchUuid") String branchUuid,
@Param("createdAt") long createdAt);
-
- Set<String> selectBranches(String portfolioProjectUuid);
}
profiler.stop();
}
- public void deleteProjectInPortfolios(String projectUuid) {
+ public void deleteProjectInPortfolios(String rootUuid) {
profiler.start("deleteProjectInPortfolios (portfolio_projects)");
- purgeMapper.deletePortfolioProjectsByProjectUuid(projectUuid);
+ purgeMapper.deletePortfolioProjectBranchesByBranchUuid(rootUuid);
+ purgeMapper.deletePortfolioProjectsByProjectUuid(rootUuid);
session.commit();
profiler.stop();
}
private static void purgeStaleBranches(PurgeCommands commands, PurgeConfiguration conf, PurgeMapper mapper, String rootUuid) {
Optional<Date> maxDate = conf.maxLiveDateOfInactiveBranches();
- if (!maxDate.isPresent()) {
+ if (maxDate.isEmpty()) {
// not available if branch plugin is not installed
return;
}
void deletePortfolioProjectsByProjectUuid(@Param("projectUuid") String projectUuid);
+ void deletePortfolioProjectBranchesByBranchUuid(@Param("branchUuid") String branchUuid);
+
void deleteBranchByUuid(@Param("uuid") String uuid);
void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid);
)
</insert>
- <delete id="delete" parameterType="String">
- DELETE FROM portfolios WHERE uuid = #{portfolioUuid,jdbcType=VARCHAR};
- DELETE FROM portfolio_references WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR};
- DELETE FROM portfolio_projects WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR};
- </delete>
-
<select id="selectAllReferencesToPortfolios" resultType="org.sonar.db.portfolio.ReferenceDto">
SELECT
source.uuid as sourceUuid,
AND source.root_uuid = #{rootUuid,jdbcType=VARCHAR})
</select>
- <delete id="deletePortfoliosByUuids" parameterType="String">
- DELETE FROM portfolios WHERE uuid in
- <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
+ <delete id="deletePortfolio" parameterType="String">
+ DELETE FROM portfolio_proj_branches
+ WHERE portfolio_project_uuid in (select uuid FROM portfolio_projects WHERE portfolio_uuid = #{uuid,jdbcType=VARCHAR});
+
+ DELETE FROM portfolio_projects
+ WHERE portfolio_uuid = #{uuid,jdbcType=VARCHAR};
+
+ DELETE FROM portfolios WHERE uuid = #{uuid,jdbcType=VARCHAR};
</delete>
<delete id="deleteReferencesByPortfolioOrReferenceUuids" parameterType="String">
<foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
</delete>
- <delete id="deleteProjectsByPortfolioUuids" parameterType="String">
- delete from portfolio_proj_branches
- where portfolio_project_uuid in
- (select uuid from portfolio_projects
- where portfolio_uuid in
- <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>);
-
- DELETE FROM portfolio_projects WHERE portfolio_uuid in
- <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
- </delete>
-
<delete id="deleteAllDescendantPortfolios" parameterType="String">
DELETE FROM portfolios WHERE root_uuid = #{rootUuid,jdbcType=VARCHAR} and parent_uuid is not null
</delete>
<include refid="portfolioColumns"/>
FROM portfolios p
INNER JOIN portfolio_references pr ON p.uuid = pr.portfolio_uuid
- where pr.reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
+ WHERE pr.reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
</select>
<delete id="deleteReferencesTo" parameterType="String">
- delete from portfolio_references
- where reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
+ DELETE FROM portfolio_references
+ WHERE reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteProjects" parameterType="String">
- delete from portfolio_proj_branches
- where portfolio_project_uuid =
- (select uuid from portfolio_projects
- where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR});
+ DELETE FROM portfolio_proj_branches
+ WHERE portfolio_project_uuid in
+ (SELECT uuid FROM portfolio_projects
+ WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR});
- delete from portfolio_projects
- where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+ DELETE FROM portfolio_projects
+ WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteProject" parameterType="map">
- delete from portfolio_proj_branches
- where portfolio_project_uuid =
- (select uuid from portfolio_projects
- where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
- and project_uuid = #{projectUuid,jdbcType=VARCHAR});
-
- delete from portfolio_projects
- where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
- and project_uuid = #{projectUuid,jdbcType=VARCHAR}
+ DELETE FROM portfolio_proj_branches
+ WHERE portfolio_project_uuid =
+ (SELECT uuid FROM portfolio_projects
+ WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+ AND project_uuid = #{projectUuid,jdbcType=VARCHAR});
+
+ DELETE FROM portfolio_projects
+ WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+ AND project_uuid = #{projectUuid,jdbcType=VARCHAR}
</delete>
<insert id="insertProject" parameterType="map">
</insert>
<delete id="deleteBranch" parameterType="map">
- delete from portfolio_proj_branches
- where portfolio_project_uuid =
- (select uuid from portfolio_projects
- where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
- and project_uuid = #{projectUuid,jdbcType=VARCHAR})
- and branch_uuid = #{branchUuid,jdbcType=VARCHAR}
+ DELETE FROM portfolio_proj_branches
+ WHERE portfolio_project_uuid =
+ (SELECT uuid FROM portfolio_projects
+ WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+ AND project_uuid = #{projectUuid,jdbcType=VARCHAR})
+ AND branch_uuid = #{branchUuid,jdbcType=VARCHAR}
</delete>
<update id="update" parameterType="Portfolio">
- update portfolios set
+ UPDATE portfolios SET
name = #{name,jdbcType=VARCHAR},
description = #{description,jdbcType=VARCHAR},
selection_mode = #{selectionMode,jdbcType=VARCHAR},
branch_key = #{branchKey,jdbcType=VARCHAR},
root_uuid = #{rootUuid,jdbcType=VARCHAR},
updated_at = #{updatedAt,jdbcType=BIGINT}
- where
+ WHERE
uuid = #{uuid,jdbcType=VARCHAR}
</update>
</mapper>
<delete id="deleteApplicationProjectsByApplicationUuid" parameterType="map">
DELETE
FROM app_projects
- where
+ WHERE
application_uuid=#{applicationUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteApplicationBranchProjectBranchesByApplicationUuid" parameterType="map">
DELETE
FROM app_branch_project_branch
- where
+ WHERE
application_uuid=#{applicationUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteApplicationBranchProjects" parameterType="String">
DELETE
FROM app_branch_project_branch
- where
+ WHERE
application_branch_uuid=#{branchUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteApplicationBranchProjectBranchesByProjectBranchUuid" parameterType="String">
DELETE
FROM app_branch_project_branch
- where project_branch_uuid=#{projectBranchUuid,jdbcType=VARCHAR}
+ WHERE project_branch_uuid=#{projectBranchUuid,jdbcType=VARCHAR}
</delete>
<delete id="deletePortfolioProjectsByProjectUuid" parameterType="map">
DELETE
FROM portfolio_projects
- WHERE
- project_uuid=#{projectUuid,jdbcType=VARCHAR}
+ WHERE project_uuid=#{projectUuid,jdbcType=VARCHAR}
+ </delete>
+
+ <delete id="deletePortfolioProjectBranchesByBranchUuid" parameterType="map">
+ DELETE FROM portfolio_proj_branches
+ WHERE portfolio_project_uuid IN (SELECT uuid FROM portfolio_projects WHERE project_uuid = #{branchUuid,jdbcType=VARCHAR})
+ OR branch_uuid = #{branchUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteIssueChangesFromIssueKeys" parameterType="map">
}
@Test
- public void delete_projects() {
+ public void deleteProjects_deletes_selected_projects_and_branches() {
db.components().insertPrivatePortfolioDto("portfolio1");
db.components().insertPrivatePortfolioDto("portfolio2");
db.components().insertPrivateProjectDto("project2");
assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
- portfolioDao.addProject(session, "portfolio1", "project1");
+ String uuid = portfolioDao.addProject(session, "portfolio1", "project1");
+ portfolioDao.addBranch(session, uuid, "project1Branch");
portfolioDao.addProject(session, "portfolio1", "project2");
portfolioDao.addProject(session, "portfolio2", "project2");
+
+ session.commit();
assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isNotEmpty();
+ assertThat(db.countRowsOfTable("portfolio_proj_branches")).isOne();
portfolioDao.deleteProjects(session, "portfolio1");
+ session.commit();
assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio2")).extracting(PortfolioProjectDto::getProjectUuid).containsOnly("project2");
+ assertThat(db.countRowsOfTable("portfolio_proj_branches")).isZero();
+ }
+
+ @Test
+ public void deleteProject_deletes_selected_branches() {
+ db.components().insertPrivatePortfolioDto("portfolio1");
+
+ db.components().insertPrivateProjectDto("project1");
+ db.components().insertPrivateProjectDto("project2");
+
+ String uuid1 = portfolioDao.addProject(session, "portfolio1", "project1");
+ portfolioDao.addBranch(session, uuid1, "project1Branch");
+ String uuid2 = portfolioDao.addProject(session, "portfolio1", "project2");
+ portfolioDao.addBranch(session, uuid2, "project2Branch");
+ session.commit();
+ assertThat(db.countRowsOfTable("portfolio_proj_branches")).isEqualTo(2);
+
+ portfolioDao.deleteProject(session, "portfolio1", "project2");
+ assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1"))
+ .extracting(PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
+ .containsOnly(tuple("project1", Set.of("project1Branch")));
}
@Test
.doesNotThrowAnyException();
}
- @Test
- public void delete_project() {
- db.components().insertPrivatePortfolioDto("portfolio1");
- db.components().insertPrivatePortfolioDto("portfolio2");
-
- db.components().insertPrivateProjectDto("project1");
- db.components().insertPrivateProjectDto("project2");
-
- assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
- portfolioDao.addProject(session, "portfolio1", "project1");
- portfolioDao.addProject(session, "portfolio1", "project2");
- portfolioDao.addProject(session, "portfolio2", "project2");
- assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isNotEmpty();
-
- portfolioDao.deleteProject(session, "portfolio1", "project2");
- assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).extracting(PortfolioProjectDto::getProjectUuid).containsOnly("project1");
- assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio2")).extracting(PortfolioProjectDto::getProjectUuid).containsOnly("project2");
- }
-
@Test
public void selectAllProjectsInHierarchy() {
ProjectDto p1 = db.components().insertPrivateProjectDto("p1");
import java.util.Arrays;
import java.util.List;
import java.util.Random;
+import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
}
@Test
- public void deleteProjectInPortfolios_deletes_project_from_portfolios() {
- var portfolio1 = dbTester.components().insertPrivatePortfolio();
- var portfolio2 = dbTester.components().insertPrivatePortfolio();
+ public void deleteProjectInPortfolios_deletes_project_and_branch_from_portfolios() {
+ var portfolio1 = dbTester.components().insertPrivatePortfolioDto();
+ var portfolio2 = dbTester.components().insertPrivatePortfolioDto();
dbTester.components().insertPrivatePortfolio();
-
ProjectDto project = dbTester.components().insertPrivateProjectDto();
ProjectDto anotherProject = dbTester.components().insertPrivateProjectDto();
- dbTester.components().addPortfolioProject(portfolio1, project.getUuid(), anotherProject.getUuid());
- dbTester.components().addPortfolioProject(portfolio2, project.getUuid());
+ dbTester.components().addPortfolioProject(portfolio1, project, anotherProject);
+ dbTester.components().addPortfolioProjectBranch(portfolio1, project, "projectBranch");
+ dbTester.components().addPortfolioProjectBranch(portfolio1, anotherProject, "anotherProjectBranch");
+
+ dbTester.components().addPortfolioProject(portfolio2, project);
PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler, system2);
purgeCommands.deleteProjectInPortfolios(project.getUuid());
assertThat(dbTester.getDbClient().portfolioDao().selectAllPortfolioProjects(dbTester.getSession()))
- .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid)
- .containsExactlyInAnyOrder(tuple(portfolio1.uuid(), anotherProject.getUuid()));
+ .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
+ .containsExactlyInAnyOrder(tuple(portfolio1.getUuid(), anotherProject.getUuid(), singleton("anotherProjectBranch")));
+ }
+
+ @Test
+ public void deleteProjectInPortfolios_deletes_branch_from_portfolios_if_root_is_branch() {
+ var portfolio1 = dbTester.components().insertPrivatePortfolioDto();
+ ProjectDto project = dbTester.components().insertPrivateProjectDto();
+
+ dbTester.components().addPortfolioProject(portfolio1, project);
+ dbTester.components().addPortfolioProjectBranch(portfolio1, project, "projectBranch");
+ dbTester.components().addPortfolioProjectBranch(portfolio1, project, "anotherBranch");
+
+ PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler, system2);
+
+ purgeCommands.deleteProjectInPortfolios("projectBranch");
+
+ assertThat(dbTester.getDbClient().portfolioDao().selectAllPortfolioProjects(dbTester.getSession()))
+ .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
+ .containsExactlyInAnyOrder(tuple(portfolio1.getUuid(), project.getUuid(), Set.of("anotherBranch")));
}
private void addPermissions(ComponentDto root) {