From 680b2cc53f291a91ef00e54772d82b06baf28a74 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 29 May 2017 11:55:30 +0200 Subject: [PATCH] SONAR-9334 delete events by project uuid when deleting a project --- .../org/sonar/db/purge/PurgeCommands.java | 133 ++++++++++-------- .../java/org/sonar/db/purge/PurgeMapper.java | 2 +- .../org/sonar/db/purge/PurgeMapper.xml | 5 +- .../org/sonar/db/purge/PurgeCommandsTest.java | 2 +- 4 files changed, 76 insertions(+), 66 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 38e9e8e6140..e09b7e7dd11 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 @@ -57,7 +57,79 @@ class PurgeCommands { } void deleteAnalyses(String rootUuid) { - deleteAnalyses(purgeMapper.selectAnalysisIdsAndUuids(new PurgeSnapshotQuery().setComponentUuid(rootUuid))); + profiler.start("deleteAnalyses (events)"); + purgeMapper.deleteComponentEvents(rootUuid); + session.commit(); + profiler.stop(); + + List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(purgeMapper.selectAnalysisIdsAndUuids(new PurgeSnapshotQuery().setComponentUuid(rootUuid))), + MAX_SNAPSHOTS_PER_QUERY); + + deleteAnalysisDuplications(analysisUuidsPartitions); + + profiler.start("deleteAnalyses (project_measures)"); + analysisUuidsPartitions.forEach(purgeMapper::deleteAnalysisMeasures); + session.commit(); + profiler.stop(); + + profiler.start("deleteAnalyses (snapshots)"); + analysisUuidsPartitions.forEach(purgeMapper::deleteAnalyses); + session.commit(); + profiler.stop(); + } + + void deleteAnalyses(PurgeSnapshotQuery... queries) { + List snapshotIds = from(asList(queries)) + .transformAndConcat(purgeMapper::selectAnalysisIdsAndUuids) + .toList(); + deleteAnalyses(snapshotIds); + } + + @VisibleForTesting + protected void deleteAnalyses(List analysisIdUuids) { + List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(analysisIdUuids), MAX_SNAPSHOTS_PER_QUERY); + + deleteAnalysisDuplications(analysisUuidsPartitions); + + profiler.start("deleteAnalyses (events)"); + analysisUuidsPartitions.forEach(purgeMapper::deleteAnalysisEvents); + session.commit(); + profiler.stop(); + + profiler.start("deleteAnalyses (project_measures)"); + analysisUuidsPartitions.forEach(purgeMapper::deleteAnalysisMeasures); + session.commit(); + profiler.stop(); + + profiler.start("deleteAnalyses (snapshots)"); + analysisUuidsPartitions.forEach(purgeMapper::deleteAnalyses); + session.commit(); + profiler.stop(); + } + + public void purgeAnalyses(List analysisUuids) { + List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(analysisUuids), MAX_SNAPSHOTS_PER_QUERY); + + deleteAnalysisDuplications(analysisUuidsPartitions); + + profiler.start("deleteSnapshotWastedMeasures (project_measures)"); + List metricIdsWithoutHistoricalData = purgeMapper.selectMetricIdsWithoutHistoricalData(); + analysisUuidsPartitions + .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData)); + session.commit(); + profiler.stop(); + + profiler.start("updatePurgeStatusToOne (snapshots)"); + analysisUuidsPartitions.forEach(purgeMapper::updatePurgeStatusToOne); + session.commit(); + profiler.stop(); + } + + private void deleteAnalysisDuplications(List> snapshotUuidsPartitions) { + profiler.start("deleteAnalysisDuplications (duplications_index)"); + snapshotUuidsPartitions.forEach(purgeMapper::deleteAnalysisDuplications); + session.commit(); + profiler.stop(); } void deletePermissions(long rootId) { @@ -106,11 +178,6 @@ class PurgeCommands { session.commit(); profiler.stop(); - profiler.start("deleteComponentEvents (events)"); - componentUuidsPartitions.forEach(purgeMapper::deleteComponentEvents); - session.commit(); - profiler.stop(); - profiler.start("deleteResource (projects)"); componentUuidsPartitions.forEach(purgeMapper::deleteComponents); session.commit(); @@ -135,60 +202,6 @@ class PurgeCommands { profiler.stop(); } - void deleteAnalyses(PurgeSnapshotQuery... queries) { - List snapshotIds = from(asList(queries)) - .transformAndConcat(purgeMapper::selectAnalysisIdsAndUuids) - .toList(); - deleteAnalyses(snapshotIds); - } - - @VisibleForTesting - protected void deleteAnalyses(List analysisIdUuids) { - List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(analysisIdUuids), MAX_SNAPSHOTS_PER_QUERY); - - deleteAnalysisDuplications(analysisUuidsPartitions); - - profiler.start("deleteAnalyses (events)"); - analysisUuidsPartitions.forEach(purgeMapper::deleteAnalysisEvents); - session.commit(); - profiler.stop(); - - profiler.start("deleteAnalyses (project_measures)"); - analysisUuidsPartitions.forEach(purgeMapper::deleteAnalysisMeasures); - session.commit(); - profiler.stop(); - - profiler.start("deleteAnalyses (snapshots)"); - analysisUuidsPartitions.forEach(purgeMapper::deleteAnalyses); - session.commit(); - profiler.stop(); - } - - public void purgeAnalyses(List analysisUuids) { - List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(analysisUuids), MAX_SNAPSHOTS_PER_QUERY); - - deleteAnalysisDuplications(analysisUuidsPartitions); - - profiler.start("deleteSnapshotWastedMeasures (project_measures)"); - List metricIdsWithoutHistoricalData = purgeMapper.selectMetricIdsWithoutHistoricalData(); - analysisUuidsPartitions.stream() - .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData)); - session.commit(); - profiler.stop(); - - profiler.start("updatePurgeStatusToOne (snapshots)"); - analysisUuidsPartitions.forEach(purgeMapper::updatePurgeStatusToOne); - session.commit(); - profiler.stop(); - } - - private void deleteAnalysisDuplications(List> snapshotUuidsPartitions) { - profiler.start("deleteAnalysisDuplications (duplications_index)"); - snapshotUuidsPartitions.forEach(purgeMapper::deleteAnalysisDuplications); - session.commit(); - profiler.stop(); - } - public void deleteFileSources(String rootUuid) { profiler.start("deleteFileSources (file_sources)"); purgeMapper.deleteFileSourcesByProjectUuid(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 a0e3248320f..3d99f8063be 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 @@ -62,7 +62,7 @@ public interface PurgeMapper { void deleteComponentManualMeasures(@Param("componentUuids") List componentUuids); - void deleteComponentEvents(@Param("componentUuids") List componentUuids); + void deleteComponentEvents(@Param("componentUuid") String componentUuid); List selectPurgeableAnalysesWithEvents(@Param("componentUuid") String componentUuid); 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 edec6e6e0e5..6b668699bd9 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 @@ -200,10 +200,7 @@ delete from events where - component_uuid in - - #{componentUuid,jdbcType=VARCHAR} - + component_uuid = #{componentUuid,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 2a33ea8c06d..688413d0d14 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 @@ -90,7 +90,7 @@ public class PurgeCommandsTest { assertThat(dbTester.countRowsOfTable("projects")).isZero(); assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1); - assertThat(dbTester.countRowsOfTable("events")).isZero(); + assertThat(dbTester.countRowsOfTable("events")).isEqualTo(3); assertThat(dbTester.countRowsOfTable("issues")).isZero(); assertThat(dbTester.countRowsOfTable("issue_changes")).isZero(); } -- 2.39.5