diff options
Diffstat (limited to 'server/sonar-db-dao/src')
9 files changed, 115 insertions, 99 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java index 6c008b9186a..c7e2fc968bf 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java @@ -91,12 +91,14 @@ public class PortfolioDao implements Dao { } 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); @@ -198,10 +200,6 @@ public class PortfolioDao implements Dao { 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()); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java index 0e848ccb50a..6abdcca9acb 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java @@ -36,14 +36,10 @@ public interface PortfolioMapper { 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); @@ -94,12 +90,8 @@ public interface PortfolioMapper { 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); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java index 6f71d4584c4..feba85ea9e3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -453,9 +453,10 @@ class PurgeCommands { 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(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java index 49dc462904c..11a0ee7e67c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -78,7 +78,7 @@ public class PurgeDao implements Dao { 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; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java index 5569cea2192..5270cec0721 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -149,6 +149,8 @@ public interface PurgeMapper { void deletePortfolioProjectsByProjectUuid(@Param("projectUuid") String projectUuid); + void deletePortfolioProjectBranchesByBranchUuid(@Param("branchUuid") String branchUuid); + void deleteBranchByUuid(@Param("uuid") String uuid); void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml index d25f5a4b6a8..85db3d297fa 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml @@ -249,12 +249,6 @@ ) </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, @@ -300,9 +294,14 @@ 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"> @@ -312,17 +311,6 @@ <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> @@ -356,34 +344,34 @@ <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"> @@ -417,16 +405,16 @@ </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}, @@ -435,7 +423,7 @@ branch_key = #{branchKey,jdbcType=VARCHAR}, root_uuid = #{rootUuid,jdbcType=VARCHAR}, updated_at = #{updatedAt,jdbcType=BIGINT} - where + WHERE uuid = #{uuid,jdbcType=VARCHAR} </update> </mapper> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index 997914ba086..32267058e04 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -325,35 +325,40 @@ <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"> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java index c8c086f392b..3ded65b6e54 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java @@ -398,7 +398,7 @@ public class PortfolioDaoTest { } @Test - public void delete_projects() { + public void deleteProjects_deletes_selected_projects_and_branches() { db.components().insertPrivatePortfolioDto("portfolio1"); db.components().insertPrivatePortfolioDto("portfolio2"); @@ -406,14 +406,40 @@ public class PortfolioDaoTest { 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 @@ -443,25 +469,6 @@ public class PortfolioDaoTest { } @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"); ProjectDto p2 = db.components().insertPrivateProjectDto("p2"); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java index 794e3a90721..33c0475e5e5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java @@ -25,6 +25,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider; 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; @@ -55,6 +56,8 @@ import org.sonar.db.user.UserDto; 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; @@ -651,24 +654,44 @@ public class PurgeCommandsTest { } @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) { |