From 11073a1928aef0a3a0e8d8bd7fe78fafddff7f25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 26 May 2017 11:01:42 +0200 Subject: [PATCH] SONAR-9326 drop issues by project_uuid when deleting a project --- .../org/sonar/db/purge/PurgeCommands.java | 30 ++++++++++--------- .../java/org/sonar/db/purge/PurgeDao.java | 1 + .../java/org/sonar/db/purge/PurgeMapper.java | 4 +-- .../org/sonar/db/purge/PurgeMapper.xml | 30 ++++++++----------- .../org/sonar/db/purge/PurgeCommandsTest.java | 21 +++++++++++-- .../shouldDeleteResource.xml | 2 +- 6 files changed, 50 insertions(+), 38 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 e09b7e7dd11..d57990db4bf 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 @@ -63,7 +63,7 @@ class PurgeCommands { profiler.stop(); List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(purgeMapper.selectAnalysisIdsAndUuids(new PurgeSnapshotQuery().setComponentUuid(rootUuid))), - MAX_SNAPSHOTS_PER_QUERY); + MAX_SNAPSHOTS_PER_QUERY); deleteAnalysisDuplications(analysisUuidsPartitions); @@ -80,8 +80,8 @@ class PurgeCommands { void deleteAnalyses(PurgeSnapshotQuery... queries) { List snapshotIds = from(asList(queries)) - .transformAndConcat(purgeMapper::selectAnalysisIdsAndUuids) - .toList(); + .transformAndConcat(purgeMapper::selectAnalysisIdsAndUuids) + .toList(); deleteAnalyses(snapshotIds); } @@ -115,7 +115,7 @@ class PurgeCommands { profiler.start("deleteSnapshotWastedMeasures (project_measures)"); List metricIdsWithoutHistoricalData = purgeMapper.selectMetricIdsWithoutHistoricalData(); analysisUuidsPartitions - .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData)); + .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData)); session.commit(); profiler.stop(); @@ -144,6 +144,18 @@ class PurgeCommands { profiler.stop(); } + void deleteIssues(String rootUuid) { + profiler.start("deleteIssues (issue_changes)"); + purgeMapper.deleteComponentIssueChanges(rootUuid); + session.commit(); + profiler.stop(); + + profiler.start("deleteIssues (issues)"); + purgeMapper.deleteComponentIssues(rootUuid); + session.commit(); + profiler.stop(); + } + void deleteComponents(List componentIdUuids) { List> componentIdPartitions = Lists.partition(IdUuidPairs.ids(componentIdUuids), MAX_RESOURCES_PER_QUERY); List> componentUuidsPartitions = Lists.partition(IdUuidPairs.uuids(componentIdUuids), MAX_RESOURCES_PER_QUERY); @@ -168,16 +180,6 @@ class PurgeCommands { session.commit(); profiler.stop(); - profiler.start("deleteComponentIssueChanges (issue_changes)"); - componentUuidsPartitions.forEach(purgeMapper::deleteComponentIssueChanges); - session.commit(); - profiler.stop(); - - profiler.start("deleteComponentIssues (issues)"); - componentUuidsPartitions.forEach(purgeMapper::deleteComponentIssues); - session.commit(); - profiler.stop(); - profiler.start("deleteResource (projects)"); componentUuidsPartitions.forEach(purgeMapper::deleteComponents); session.commit(); 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 8c411368257..1e804170efd 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 @@ -162,6 +162,7 @@ public class PurgeDao implements Dao { commands.deletePermissions(rootId); commands.deleteAnalyses(rootUuid); commands.deleteComponents(childrenIds); + commands.deleteIssues(rootUuid); commands.deleteFileSources(rootUuid); commands.deleteCeActivity(rootUuid); commands.deleteCeQueue(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 3d99f8063be..f860348e6e8 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,9 +68,9 @@ public interface PurgeMapper { List selectPurgeableAnalysesWithoutEvents(@Param("componentUuid") String componentUuid); - void deleteComponentIssueChanges(@Param("componentUuids") List componentUuids); + void deleteComponentIssueChanges(@Param("rootUuid") String rootUuid); - void deleteComponentIssues(@Param("componentUuids") List componentUuids); + void deleteComponentIssues(@Param("rootUuid") String rootUuid); List selectOldClosedIssueKeys(@Param("projectUuid") String projectUuid, @Nullable @Param("toDate") Long toDate); 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 6b668699bd9..7a93f7136be 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 @@ -205,36 +205,30 @@ delete from issue_changes ic - where exists (select * from issues i where i.kee=ic.issue_key and i.component_uuid in - - #{componentUuid,jdbcType=VARCHAR} - - ) + where + exists (select 1 from issues i where i.kee=ic.issue_key and i.project_uuid = #{rootUuid,jdbcType=VARCHAR}) delete issue_changes from issue_changes - inner join issues on issue_changes.issue_key=issues.kee - where issues.component_uuid in - - #{componentUuid,jdbcType=VARCHAR} - + inner join issues on + issue_changes.issue_key=issues.kee + where + issues.project_uuid = #{rootUuid,jdbcType=VARCHAR} - delete ic from issue_changes as ic, issues as i where ic.issue_key=i.kee and i.component_uuid in - - #{componentUuid,jdbcType=VARCHAR} - + delete ic from issue_changes as ic, issues as i + where + ic.issue_key=i.kee + and i.component_uuid = #{rootUuid,jdbcType=VARCHAR} - delete from issues where component_uuid in - - #{componentUuid,jdbcType=VARCHAR} - + delete from issues + where project_uuid = #{rootUuid,jdbcType=VARCHAR} 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 688413d0d14..25c7ee39c21 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 @@ -91,8 +91,8 @@ public class PurgeCommandsTest { assertThat(dbTester.countRowsOfTable("projects")).isZero(); assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1); assertThat(dbTester.countRowsOfTable("events")).isEqualTo(3); - assertThat(dbTester.countRowsOfTable("issues")).isZero(); - assertThat(dbTester.countRowsOfTable("issue_changes")).isZero(); + assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(1); + assertThat(dbTester.countRowsOfTable("issue_changes")).isEqualTo(1); } @@ -105,11 +105,26 @@ public class PurgeCommandsTest { assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(1); assertThat(dbTester.countRowsOfTable("snapshots")).isZero(); - assertThat(dbTester.countRowsOfTable("events")).isEqualTo(0); + assertThat(dbTester.countRowsOfTable("events")).isZero(); assertThat(dbTester.countRowsOfTable("issues")).isEqualTo(1); assertThat(dbTester.countRowsOfTable("issue_changes")).isEqualTo(1); } + + @Test + public void shouldDeleteIssuesAndIssueChanges() { + dbTester.prepareDbUnit(getClass(), "shouldDeleteResource.xml"); + + PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler); + purgeCommands.deleteIssues("uuid_1"); + + assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(1); + assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1); + assertThat(dbTester.countRowsOfTable("events")).isEqualTo(3); + assertThat(dbTester.countRowsOfTable("issues")).isZero(); + assertThat(dbTester.countRowsOfTable("issue_changes")).isZero(); + } + @Test public void deletePermissions_deletes_permissions_of_public_project() { OrganizationDto organization = dbTester.organizations().insert(); diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml index 2d521a1fded..330057a1b73 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml @@ -80,7 +80,7 @@