From: Janos Gyerik Date: Wed, 16 Aug 2017 14:24:27 +0000 (+0200) Subject: SONAR-9616 Delete branch content when deleting project (#2368) X-Git-Tag: 6.6-RC1~380^2~93 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fe946b35cfad0e572dc07408e6ac1fabc9f65958;p=sonarqube.git SONAR-9616 Delete branch content when deleting project (#2368) --- 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 ef9cdaa0013..9cf7789d96b 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 @@ -255,4 +255,11 @@ class PurgeCommands { session.commit(); profiler.stop(); } + + void deleteBranches(String rootUuid) { + profiler.start("deleteBranches (project_branches)"); + purgeMapper.deleteBranchesByProjectUuid(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 369cd7db93b..67fb3b701c6 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 @@ -33,6 +33,7 @@ import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchMapper; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTreeQuery; @@ -150,8 +151,15 @@ public class PurgeDao implements Dao { public void deleteRootComponent(DbSession session, String uuid) { PurgeProfiler profiler = new PurgeProfiler(); + PurgeMapper purgeMapper = mapper(session); PurgeCommands purgeCommands = new PurgeCommands(session, profiler); - deleteRootComponent(uuid, mapper(session), purgeCommands); + + session.getMapper(BranchMapper.class).selectByProjectUuid(uuid) + .stream() + .filter(branch -> !uuid.equals(branch.getUuid())) + .forEach(branch -> deleteRootComponent(branch.getUuid(), purgeMapper, purgeCommands)); + + deleteRootComponent(uuid, purgeMapper, purgeCommands); } private static void deleteRootComponent(String rootUuid, PurgeMapper mapper, PurgeCommands commands) { @@ -171,6 +179,7 @@ public class PurgeDao implements Dao { commands.deleteCeActivity(rootUuid); commands.deleteCeQueue(rootUuid); commands.deleteWebhookDeliveries(rootUuid); + commands.deleteBranches(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 0aefe633502..23d68ee1bc1 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 @@ -91,4 +91,6 @@ public interface PurgeMapper { void deleteCeQueueByProjectUuid(@Param("projectUuid") String projectUuid); void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid); + + void deleteBranchesByProjectUuid(@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 0a7661ccb32..d22fc0b973e 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 @@ -309,5 +309,9 @@ delete from webhook_deliveries where component_uuid=#{projectUuid,jdbcType=VARCHAR} + + delete from project_branches where project_uuid=#{projectUuid,jdbcType=VARCHAR} + + 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 1b817dae823..903b59d161f 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 @@ -47,6 +47,7 @@ import org.sonar.db.component.ComponentTesting; import org.sonar.db.measure.MeasureDto; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.property.PropertyDto; +import org.sonar.db.rule.RuleDefinitionDto; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -59,6 +60,7 @@ import static org.mockito.Mockito.when; import static org.sonar.db.ce.CeTaskTypes.REPORT; import static org.sonar.db.component.ComponentTesting.newDirectory; import static org.sonar.db.component.ComponentTesting.newFileDto; +import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.component.ComponentTesting.newProjectCopy; import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto; import static org.sonar.db.webhook.WebhookDbTesting.selectAllDeliveryUuids; @@ -192,6 +194,39 @@ public class PurgeDaoTest { assertThat(dbTester.countSql("select count(*) from ce_queue where component_uuid='" + projectToBeDeleted.uuid() + "'")).isEqualTo(0); } + private ComponentDto insertProjectWithBranchAndRelatedData() { + RuleDefinitionDto rule = dbTester.rules().insert(); + ComponentDto project = dbTester.components().insertMainBranch(); + ComponentDto branch = dbTester.components().insertProjectBranch(project); + ComponentDto module = dbTester.components().insertComponent(newModuleDto(branch)); + ComponentDto subModule = dbTester.components().insertComponent(newModuleDto(module)); + ComponentDto file = dbTester.components().insertComponent(newFileDto(subModule)); + dbTester.issues().insert(rule, branch, file); + dbTester.issues().insert(rule, branch, subModule); + dbTester.issues().insert(rule, branch, module); + return project; + } + + @Test + public void delete_branch_content_when_deleting_project() { + ComponentDto anotherLivingProject = insertProjectWithBranchAndRelatedData(); + int projectEntryCount = dbTester.countRowsOfTable("projects"); + int issueCount = dbTester.countRowsOfTable("issues"); + int branchCount = dbTester.countRowsOfTable("project_branches"); + + ComponentDto projectToDelete = insertProjectWithBranchAndRelatedData(); + assertThat(dbTester.countRowsOfTable("projects")).isGreaterThan(projectEntryCount); + assertThat(dbTester.countRowsOfTable("issues")).isGreaterThan(issueCount); + assertThat(dbTester.countRowsOfTable("project_branches")).isGreaterThan(branchCount); + + underTest.deleteRootComponent(dbSession, projectToDelete.uuid()); + dbSession.commit(); + + assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(projectEntryCount); + assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(issueCount); + assertThat(dbTester.countRowsOfTable("project_branches")).isEqualTo(branchCount); + } + @Test public void delete_view_and_child() { dbTester.prepareDbUnit(getClass(), "view_sub_view_and_tech_project.xml");