diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-10-27 11:16:50 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-10-27 12:07:04 +0100 |
commit | a4774e7d5a7a1e5903ed79f431a7c0f3c071c145 (patch) | |
tree | 97a47e5fae1cb6886f18dca19e0184d9bf28b47d /sonar-db | |
parent | 19350f7eb06f0f46674ad394e0919edbae2b2a2a (diff) | |
download | sonarqube-a4774e7d5a7a1e5903ed79f431a7c0f3c071c145.tar.gz sonarqube-a4774e7d5a7a1e5903ed79f431a7c0f3c071c145.zip |
SONAR-6834 Purge CE_ACTIVITY when deleting a project
Diffstat (limited to 'sonar-db')
7 files changed, 73 insertions, 9 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java index 8e93918e780..20a4cf742e4 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -48,7 +48,7 @@ class PurgeCommands { return purgeMapper.selectSnapshotIds(query); } - void deleteResources(List<IdUuidPair> componentIdUuids) { + void deleteComponents(List<IdUuidPair> componentIdUuids) { List<List<Long>> componentIdPartitions = Lists.partition(IdUuidPairs.ids(componentIdUuids), MAX_RESOURCES_PER_QUERY); List<List<String>> componentUuidsPartitions = Lists.partition(IdUuidPairs.uuids(componentIdUuids), MAX_RESOURCES_PER_QUERY); // Note : do not merge the delete statements into a single loop of resource ids. It's @@ -221,4 +221,11 @@ class PurgeCommands { session.commit(); profiler.stop(); } + + public void deleteCeActivity(String rootUuid) { + profiler.start("deleteCeActivity (ce_activity)"); + purgeMapper.deleteCeActivityByProjectUuid(rootUuid); + session.commit(); + profiler.stop(); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java index 75213d7d203..dc645cfd0e6 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -173,17 +173,14 @@ public class PurgeDao implements Dao { PurgeProfiler profiler = new PurgeProfiler(); PurgeCommands purgeCommands = new PurgeCommands(session, profiler); deleteProject(uuid, mapper(session), purgeCommands); - deleteFileSources(uuid, purgeCommands); return this; } - private static void deleteFileSources(String rootUuid, PurgeCommands commands) { - commands.deleteFileSources(rootUuid); - } - private static void deleteProject(String rootUuid, PurgeMapper mapper, PurgeCommands commands) { List<IdUuidPair> childrenIds = mapper.selectComponentsByProjectUuid(rootUuid); - commands.deleteResources(childrenIds); + commands.deleteComponents(childrenIds); + commands.deleteFileSources(rootUuid); + commands.deleteCeActivity(rootUuid); } private void disableResource(IdUuidPair componentIdUuid, PurgeMapper mapper) { diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java index e6c8c845fcd..00c9a4d23eb 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -92,4 +92,6 @@ public interface PurgeMapper { void deleteFileSourcesByUuid(String fileUuid); + void deleteCeActivityByProjectUuid(String projectUuid); + } diff --git a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index 8dcfbf87609..1185c61863e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -327,5 +327,9 @@ </choose> </delete> + <delete id="deleteCeActivityByProjectUuid"> + delete from ce_activity where component_uuid=#{rootProjectUuid} + </delete> + </mapper> diff --git a/sonar-db/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java b/sonar-db/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java index 49922e94eda..3cd0783b93c 100644 --- a/sonar-db/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java @@ -97,7 +97,7 @@ public class PurgeCommandsTest { public void shouldDeleteResource() { dbTester.prepareDbUnit(getClass(), "shouldDeleteResource.xml"); - new PurgeCommands(dbTester.getSession(), profiler).deleteResources(newArrayList(new IdUuidPair(1L, "1"))); + new PurgeCommands(dbTester.getSession(), profiler).deleteComponents(newArrayList(new IdUuidPair(1L, "1"))); assertThat(dbTester.countRowsOfTable("projects")).isZero(); assertThat(dbTester.countRowsOfTable("snapshots")).isZero(); @@ -113,7 +113,7 @@ public class PurgeCommandsTest { @Test public void should_not_fail_when_deleting_huge_number_of_resources() { dbTester.truncateTables(); - new PurgeCommands(dbTester.getSession(), profiler).deleteResources(getHugeNumberOfIdUuids()); + new PurgeCommands(dbTester.getSession(), profiler).deleteComponents(getHugeNumberOfIdUuids()); // The goal of this test is only to check that the query do no fail, not to check result } diff --git a/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index ac64984651a..805591f7dc5 100644 --- a/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -25,13 +25,20 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; +import org.sonar.core.util.Uuids; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.ce.CeActivityDto; +import org.sonar.db.ce.CeQueueDto; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.db.ce.CeTaskTypes.REPORT; @Category(DbTests.class) public class PurgeDaoTest { @@ -41,6 +48,8 @@ public class PurgeDaoTest { @Rule public DbTester dbTester = DbTester.create(system2); + DbClient dbClient = dbTester.getDbClient(); + DbSession dbSession = dbTester.getSession(); PurgeDao underTest = dbTester.getDbClient().purgeDao(); @@ -117,6 +126,23 @@ public class PurgeDaoTest { } @Test + public void delete_project_in_ce_activity_when_deleting_project() { + ComponentDto projectToBeDeleted = ComponentTesting.newProjectDto(); + ComponentDto anotherLivingProject = ComponentTesting.newProjectDto(); + dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject); + + // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and on on another project + insertCeActivity(projectToBeDeleted.uuid()); + insertCeActivity(anotherLivingProject.uuid()); + dbSession.commit(); + + underTest.deleteProject(dbSession, projectToBeDeleted.uuid()); + dbSession.commit(); + + assertThat(dbTester.countRowsOfTable("ce_activity")).isEqualTo(1); + } + + @Test public void delete_view_and_child() { dbTester.prepareDbUnit(getClass(), "view_sub_view_and_tech_project.xml"); @@ -160,6 +186,23 @@ public class PurgeDaoTest { dbTester.assertDbUnit(getClass(), "should_delete_all_closed_issues-result.xml", "issues", "issue_changes"); } + private CeActivityDto insertCeActivity(String componentUuid) { + CeQueueDto queueDto = new CeQueueDto(); + queueDto.setUuid(Uuids.create()); + queueDto.setTaskType(REPORT); + queueDto.setComponentUuid(componentUuid); + queueDto.setSubmitterLogin("henri"); + queueDto.setCreatedAt(1_300_000_000_000L); + + CeActivityDto dto = new CeActivityDto(queueDto); + dto.setStatus(CeActivityDto.Status.SUCCESS); + dto.setStartedAt(1_500_000_000_000L); + dto.setExecutedAt(1_500_000_000_500L); + dto.setExecutionTimeMs(500L); + dbClient.ceActivityDao().insert(dbSession, dto); + return dto; + } + private static PurgeableSnapshotDto getById(List<PurgeableSnapshotDto> snapshots, long id) { for (PurgeableSnapshotDto snapshot : snapshots) { if (snapshot.getSnapshotId() == id) { diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/delete_project_in_ce_activity_when_deleting_project.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/delete_project_in_ce_activity_when_deleting_project.xml new file mode 100644 index 00000000000..b357e0afd7b --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/delete_project_in_ce_activity_when_deleting_project.xml @@ -0,0 +1,11 @@ +<dataset> + + <!-- project --> + <projects id="1" enabled="[true]" root_id="[null]" + uuid="A" project_uuid="A" module_uuid="[null]" module_uuid_path=".A." + long_name="[null]" scope="PRJ" qualifier="TRK" kee="project" name="project" + description="[null]" language="java" copy_resource_id="[null]" person_id="[null]" + authorization_updated_at="[null]"/> + + +</dataset> |