diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2021-09-03 14:56:15 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-09-08 20:03:35 +0000 |
commit | a5cdfb5877fb07448302bf655c4c2834df9d384b (patch) | |
tree | c7008be21713223e8e1f1faea93c0bff84738867 | |
parent | 44e331c87ff60c386455f60476958577f512152c (diff) | |
download | sonarqube-a5cdfb5877fb07448302bf655c4c2834df9d384b.tar.gz sonarqube-a5cdfb5877fb07448302bf655c4c2834df9d384b.zip |
SONAR-15259 Purge project from portfolios when removed
6 files changed, 68 insertions, 13 deletions
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 8eec8a70106..6f71d4584c4 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,4 +453,10 @@ class PurgeCommands { profiler.stop(); } + public void deleteProjectInPortfolios(String projectUuid) { + profiler.start("deleteProjectInPortfolios (portfolio_projects)"); + purgeMapper.deletePortfolioProjectsByProjectUuid(projectUuid); + 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 46b3d523ac4..49dc462904c 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 @@ -232,6 +232,7 @@ public class PurgeDao implements Dao { commands.deleteBranch(rootUuid); commands.deleteApplicationBranchProjects(rootUuid); commands.deleteApplicationProjects(rootUuid); + commands.deleteProjectInPortfolios(rootUuid); commands.deleteComponents(rootUuid); commands.deleteComponentsByMainBranchProjectUuid(rootUuid); commands.deleteProject(rootUuid); 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 4d7c72313f1..5569cea2192 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 @@ -147,6 +147,8 @@ public interface PurgeMapper { void deleteApplicationBranchProjectBranchesByProjectBranchUuid(@Param("projectBranchUuid") String projectBranchUuid); + void deletePortfolioProjectsByProjectUuid(@Param("projectUuid") String projectUuid); + void deleteBranchByUuid(@Param("uuid") String uuid); void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid); @@ -158,4 +160,5 @@ public interface PurgeMapper { void deleteProjectAlmSettingsByProjectUuid(@Param("projectUuid") String projectUuid); void deleteUserDismissedMessagesByProjectUuid(@Param("projectUuid") String projectUuid); + } 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 75c93120ff2..997914ba086 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 @@ -349,6 +349,13 @@ where project_branch_uuid=#{projectBranchUuid,jdbcType=VARCHAR} </delete> + <delete id="deletePortfolioProjectsByProjectUuid" parameterType="map"> + DELETE + FROM portfolio_projects + WHERE + project_uuid=#{projectUuid,jdbcType=VARCHAR} + </delete> + <delete id="deleteIssueChangesFromIssueKeys" parameterType="map"> DELETE FROM issue_changes WHERE issue_key IN 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 e90f267625f..794e3a90721 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 @@ -47,6 +47,7 @@ import org.sonar.db.issue.IssueDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.newcodeperiod.NewCodePeriodType; import org.sonar.db.permission.GlobalPermission; +import org.sonar.db.portfolio.PortfolioProjectDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.db.user.GroupDto; @@ -57,6 +58,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.component.ComponentTesting.newBranchDto; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newProjectCopy; @@ -648,6 +650,27 @@ public class PurgeCommandsTest { assertThat(dbTester.countRowsOfTable("user_dismissed_messages")).isEqualTo(1); } + @Test + public void deleteProjectInPortfolios_deletes_project_from_portfolios() { + var portfolio1 = dbTester.components().insertPrivatePortfolio(); + var portfolio2 = dbTester.components().insertPrivatePortfolio(); + 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()); + + 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())); + } + private void addPermissions(ComponentDto root) { if (!root.isPrivate()) { dbTester.users().insertProjectPermissionOnAnyone("foo1", root); @@ -666,10 +689,6 @@ public class PurgeCommandsTest { assertThat(dbTester.countRowsOfTable("user_roles")).isEqualTo(2); } - private int countMeasures(SnapshotDto analysis, MetricDto metric) { - return dbTester.countSql("select count(*) from project_measures where analysis_uuid='" + analysis.getUuid() + "' and metric_id=" + metric.getUuid()); - } - private int countComponentOfRoot(ComponentDto projectOrView) { return dbTester.countSql("select count(1) from components where project_uuid='" + projectOrView.uuid() + "'"); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index 2e27954c644..5809ea842ea 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -73,6 +73,7 @@ import org.sonar.db.measure.MeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.newcodeperiod.NewCodePeriodDto; import org.sonar.db.newcodeperiod.NewCodePeriodType; +import org.sonar.db.portfolio.PortfolioProjectDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.rule.RuleDefinitionDto; @@ -89,7 +90,7 @@ import static java.util.Collections.emptySet; import static java.util.Collections.singletonList; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; +import static org.assertj.core.groups.Tuple.tuple; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; @@ -400,7 +401,7 @@ public class PurgeDaoTest { @Test public void selectPurgeableAnalyses() { - SnapshotDto[] analyses = new SnapshotDto[]{ + SnapshotDto[] analyses = new SnapshotDto[] { newSnapshot() .setUuid("u1") .setComponentUuid(PROJECT_UUID) @@ -1371,6 +1372,24 @@ public class PurgeDaoTest { } @Test + public void deleteProject_deletes_portfolio_projects() { + ComponentDto portfolio1 = db.components().insertPrivatePortfolio(); + ComponentDto portfolio2 = db.components().insertPrivatePortfolio(); + + ProjectDto project = db.components().insertPublicProjectDto(); + ProjectDto otherProject = db.components().insertPublicProjectDto(); + + db.components().addPortfolioProject(portfolio1, project.getUuid(), otherProject.getUuid()); + db.components().addPortfolioProject(portfolio2, project.getUuid()); + + underTest.deleteProject(dbSession, project.getUuid(), project.getQualifier(), project.getName(), project.getKey()); + + assertThat(dbClient.portfolioDao().selectAllPortfolioProjects(dbSession)) + .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid) + .containsExactlyInAnyOrder(tuple(portfolio1.uuid(), otherProject.getUuid())); + } + + @Test public void deleteNonRootComponents_has_no_effect_when_parameter_is_empty() { DbSession dbSession = mock(DbSession.class); @@ -1643,18 +1662,18 @@ public class PurgeDaoTest { private void insertPropertyFor(ComponentDto... components) { Stream.of(components).forEach(componentDto -> db.properties().insertProperty(new PropertyDto() - .setKey(randomAlphabetic(3)) - .setValue(randomAlphabetic(3)) - .setComponentUuid(componentDto.uuid()), + .setKey(randomAlphabetic(3)) + .setValue(randomAlphabetic(3)) + .setComponentUuid(componentDto.uuid()), componentDto.getKey(), componentDto.name(), componentDto.qualifier(), null)); } private void insertPropertyFor(Collection<BranchDto> branches) { branches.stream().forEach(branchDto -> db.properties().insertProperty(new PropertyDto() - .setKey(randomAlphabetic(3)) - .setValue(randomAlphabetic(3)) - .setComponentUuid(branchDto.getUuid()), - null, branchDto.getKey(), null,null)); + .setKey(randomAlphabetic(3)) + .setValue(randomAlphabetic(3)) + .setComponentUuid(branchDto.getUuid()), + null, branchDto.getKey(), null, null)); } private Stream<String> getComponentUuidsOfMeasures() { |