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
session.commit();
profiler.stop();
}
+
+ public void deleteCeActivity(String rootUuid) {
+ profiler.start("deleteCeActivity (ce_activity)");
+ purgeMapper.deleteCeActivityByProjectUuid(rootUuid);
+ session.commit();
+ profiler.stop();
+ }
}
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) {
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();
@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
}
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 {
@Rule
public DbTester dbTester = DbTester.create(system2);
+ DbClient dbClient = dbTester.getDbClient();
+
DbSession dbSession = dbTester.getSession();
PurgeDao underTest = dbTester.getDbClient().purgeDao();
assertThat(dbTester.countRowsOfTable("file_sources")).isZero();
}
+ @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");
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) {