From 24bf49516a4cf103fdb4009954587353cc5af3d3 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 21 Aug 2020 11:32:35 -0500 Subject: [PATCH] SONAR-13784 Deleting project should make sure all components are removed --- .../org/sonar/db/purge/PurgeCommands.java | 7 ++++++ .../java/org/sonar/db/purge/PurgeDao.java | 1 + .../java/org/sonar/db/purge/PurgeMapper.java | 2 ++ .../org/sonar/db/purge/PurgeMapper.xml | 6 +++++ .../org/sonar/db/purge/PurgeCommandsTest.java | 22 +++++++++++++++++++ 5 files changed, 38 insertions(+) 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 3d28191d9b1..33abd185e2e 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 @@ -266,6 +266,13 @@ class PurgeCommands { profiler.stop(); } + void deleteComponentsByMainBranchProjectUuid(String uuid) { + profiler.start("deleteComponentsByMainBranchProjectUuid (projects)"); + purgeMapper.deleteComponentsByMainBranchProjectUuid(uuid); + session.commit(); + profiler.stop(); + } + void deleteProject(String projectUuid) { profiler.start("deleteProject (projects)"); purgeMapper.deleteProjectsByProjectUuid(projectUuid); 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 16d0df3ffc6..a86217c5d83 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 @@ -207,6 +207,7 @@ public class PurgeDao implements Dao { commands.deleteNewCodePeriods(rootUuid); commands.deleteBranch(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 5e068e4cac9..56f09dda6b6 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 @@ -68,6 +68,8 @@ public interface PurgeMapper { void deleteComponentsByProjectUuid(@Param("rootUuid") String rootUuid); + void deleteComponentsByMainBranchProjectUuid(@Param("uuid") String uuid); + void deleteProjectsByProjectUuid(@Param("projectUuid") String projectUuid); void deleteComponentsByUuids(@Param("componentUuids") List componentUuids); 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 7172ba7bba9..c9cb7aad403 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 @@ -223,6 +223,12 @@ project_uuid = #{rootUuid,jdbcType=VARCHAR} + + delete from components + where + main_branch_project_uuid = #{uuid,jdbcType=VARCHAR} + + delete from projects where 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 717f259e0b3..596f943d0a2 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 @@ -148,6 +148,28 @@ public class PurgeCommandsTest { assertThat(countComponentOfRoot(otherProject)).isEqualTo(8); } + @Test + @UseDataProvider("projects") + public void deleteComponentsByMainBranchProjectUuid_deletes_all_branches_of_a_project(OrganizationDto organizationDto, ComponentDto project) { + dbTester.organizations().insert(organizationDto); + dbTester.components().insertComponent(project); + ComponentDto branch = dbTester.components().insertProjectBranch(project); + Stream.of(project, branch).forEach(prj -> { + ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(prj)); + ComponentDto directory1 = dbTester.components().insertComponent(ComponentTesting.newDirectory(module, "a")); + ComponentDto directory2 = dbTester.components().insertComponent(ComponentTesting.newDirectory(prj, "b")); + dbTester.components().insertComponent(newFileDto(prj)); + dbTester.components().insertComponent(newFileDto(module)); + dbTester.components().insertComponent(newFileDto(directory1)); + dbTester.components().insertComponent(newFileDto(directory2)); + }); + + underTest.deleteComponentsByMainBranchProjectUuid(project.uuid()); + + assertThat(countComponentOfRoot(project)).isEqualTo(8); + assertThat(countComponentOfRoot(branch)).isZero(); + } + @Test @UseDataProvider("views") public void deleteComponents_delete_tree_of_components_of_a_view(OrganizationDto organizationDto, ComponentDto view) { -- 2.39.5