import static org.sonar.api.utils.DateUtils.dateToLong;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
-/**
- * @since 2.14
- */
public class PurgeDao implements Dao {
private static final Logger LOG = Loggers.get(PurgeDao.class);
private static final String[] UNPROCESSED_STATUS = new String[] {"U"};
private static final ImmutableSet<String> QUALIFIERS_PROJECT_VIEW = ImmutableSet.of("TRK", "VW");
private static final ImmutableSet<String> QUALIFIERS_MODULE_SUBVIEW = ImmutableSet.of("BRC", "SVW");
private static final String SCOPE_PROJECT = "PRJ";
- private static final String SCOPE_FILE = "FIL";
- private static final String QUALIFIER_FILE = "FIL";
- private static final String QUALIFIER_UNIT_TEST = "UTS";
private final ComponentDao componentDao;
private final System2 system2;
return result;
}
- public PurgeDao deleteRootComponent(DbSession session, String uuid) {
+ public void deleteRootComponent(DbSession session, String uuid) {
PurgeProfiler profiler = new PurgeProfiler();
PurgeCommands purgeCommands = new PurgeCommands(session, profiler);
deleteRootComponent(uuid, mapper(session), purgeCommands);
- return this;
}
private static void deleteRootComponent(String rootUuid, PurgeMapper mapper, PurgeCommands commands) {
}
/**
- * Delete the non root components (ie. neither project nor view) from the specified collection of {@link ComponentDto}
+ * Delete the non root components (ie. sub-view, application or project copy) from the specified collection of {@link ComponentDto}
* and data from their child tables.
* <p>
* This method has no effect when passed an empty collection or only root components.
* </p>
*/
- public PurgeDao deleteNonRootComponents(DbSession dbSession, Collection<ComponentDto> components) {
+ public void deleteNonRootComponentsInView(DbSession dbSession, Collection<ComponentDto> components) {
Set<ComponentDto> nonRootComponents = components.stream().filter(PurgeDao::isNotRoot).collect(MoreCollectors.toSet());
if (nonRootComponents.isEmpty()) {
- return this;
+ return;
}
PurgeProfiler profiler = new PurgeProfiler();
PurgeCommands purgeCommands = new PurgeCommands(dbSession, profiler);
- deleteNonRootComponents(nonRootComponents, purgeCommands);
-
- return this;
+ deleteNonRootComponentsInView(nonRootComponents, purgeCommands);
}
- private static void deleteNonRootComponents(Set<ComponentDto> nonRootComponents, PurgeCommands purgeCommands) {
- List<IdUuidPair> modulesOrSubviews = nonRootComponents.stream()
+ private static void deleteNonRootComponentsInView(Set<ComponentDto> nonRootComponents, PurgeCommands purgeCommands) {
+ List<IdUuidPair> subviewsOrProjectCopies = nonRootComponents.stream()
.filter(PurgeDao::isModuleOrSubview)
.map(PurgeDao::toIdUuidPair)
.collect(MoreCollectors.toList());
- purgeCommands.deleteByRootAndModulesOrSubviews(modulesOrSubviews);
+ purgeCommands.deleteByRootAndModulesOrSubviews(subviewsOrProjectCopies);
List<String> nonRootComponentUuids = nonRootComponents.stream().map(ComponentDto::uuid).collect(MoreCollectors.toList(nonRootComponents.size()));
purgeCommands.deleteComponentMeasures(nonRootComponentUuids);
- purgeCommands.deleteFileSources(nonRootComponents.stream().filter(PurgeDao::isFile).map(ComponentDto::uuid).collect(MoreCollectors.toList()));
- purgeCommands.deleteIssues(nonRootComponentUuids);
purgeCommands.deleteComponents(nonRootComponentUuids);
}
return SCOPE_PROJECT.equals(dto.scope()) && QUALIFIERS_MODULE_SUBVIEW.contains(dto.qualifier());
}
- private static boolean isFile(ComponentDto dto) {
- return SCOPE_FILE.equals(dto.qualifier()) && ImmutableSet.of(QUALIFIER_FILE, QUALIFIER_UNIT_TEST).contains(dto.qualifier());
- }
-
private static IdUuidPair toIdUuidPair(ComponentDto dto) {
return new IdUuidPair(dto.getId(), dto.uuid());
}
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.issue.IssueDto;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.property.PropertyDto;
-import org.sonar.db.rule.RuleTesting;
-import org.sonar.db.source.FileSourceDto;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
public void deleteNonRootComponents_has_no_effect_when_parameter_is_empty() {
DbSession dbSession = mock(DbSession.class);
- underTest.deleteNonRootComponents(dbSession, Collections.emptyList());
+ underTest.deleteNonRootComponentsInView(dbSession, Collections.emptyList());
verifyZeroInteractions(dbSession);
}
List<ComponentDto> componentDtos = Stream.concat(Stream.of(firstRoot), Arrays.stream(otherRoots)).collect(Collectors.toList());
Collections.shuffle(componentDtos); // order of collection must not matter
- underTest.deleteNonRootComponents(dbSession, componentDtos);
+ underTest.deleteNonRootComponentsInView(dbSession, componentDtos);
verifyZeroInteractions(dbSession);
}
dbTester.components().insertComponent(newFileDto(project)));
Collections.shuffle(components);
- underTest.deleteNonRootComponents(dbSession, components);
+ underTest.deleteNonRootComponentsInView(dbSession, components);
assertThat(getUuidsInTableProjects())
.containsOnly(project.uuid());
dbTester.components().insertComponent(newProjectCopy("c", projects[2], subview2)));
Collections.shuffle(components);
- underTest.deleteNonRootComponents(dbSession, components);
+ underTest.deleteNonRootComponentsInView(dbSession, components);
assertThat(getUuidsInTableProjects())
.containsOnly(view.uuid(), projects[0].uuid(), projects[1].uuid(), projects[2].uuid());
ComponentDto file2 = dbTester.components().insertComponent(newFileDto(module2));
ComponentDto file3 = dbTester.components().insertComponent(newFileDto(project));
- underTest.deleteNonRootComponents(dbSession, singletonList(file3));
+ underTest.deleteNonRootComponentsInView(dbSession, singletonList(file3));
assertThat(getUuidsInTableProjects())
.containsOnly(project.uuid(), module1.uuid(), module2.uuid(), dir1.uuid(), dir2.uuid(), file1.uuid(), file2.uuid());
- underTest.deleteNonRootComponents(dbSession, asList(module1, dir2, file1));
+ underTest.deleteNonRootComponentsInView(dbSession, asList(module1, dir2, file1));
assertThat(getUuidsInTableProjects())
.containsOnly(project.uuid(), module2.uuid(), dir1.uuid(), file2.uuid());
}
ComponentDto pc2 = dbTester.components().insertComponent(newProjectCopy("b", projects[1], subview1));
ComponentDto pc3 = dbTester.components().insertComponent(newProjectCopy("c", projects[2], subview2));
- underTest.deleteNonRootComponents(dbSession, singletonList(pc3));
+ underTest.deleteNonRootComponentsInView(dbSession, singletonList(pc3));
assertThat(getUuidsInTableProjects())
.containsOnly(view.uuid(), projects[0].uuid(), projects[1].uuid(), projects[2].uuid(),
subview1.uuid(), subview2.uuid(), pc1.uuid(), pc2.uuid());
- underTest.deleteNonRootComponents(dbSession, asList(subview1, pc2));
+ underTest.deleteNonRootComponentsInView(dbSession, asList(subview1, pc2));
assertThat(getUuidsInTableProjects())
.containsOnly(view.uuid(), projects[0].uuid(), projects[1].uuid(), projects[2].uuid(), subview2.uuid(), pc1.uuid());
}
- @Test
- public void deleteNonRootComponents_deletes_measures_of_any_non_root_component_of_a_project() {
- ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPublicProject() : dbTester.components().insertPrivateProject();
- ComponentDto module1 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto dir1 = dbTester.components().insertComponent(newDirectory(module1, "A/B"));
- ComponentDto file1 = dbTester.components().insertComponent(newFileDto(dir1));
- insertIssueAndChangesFor(project, module1, dir1, file1);
- assertThat(getComponentUuidsOfIssueChanges()).containsOnly(project.uuid(), module1.uuid(), dir1.uuid(), file1.uuid());
-
- underTest.deleteNonRootComponents(dbSession, singletonList(file1));
- assertThat(getComponentUuidsOfIssueChanges())
- .containsOnly(project.uuid(), module1.uuid(), dir1.uuid());
-
- underTest.deleteNonRootComponents(dbSession, asList(module1, dir1));
- assertThat(getComponentUuidsOfIssueChanges())
- .containsOnly(project.uuid());
- }
-
@Test
public void deleteNonRootComponents_deletes_measures_of_any_non_root_component_of_a_view() {
ComponentDto view = dbTester.components().insertView();
insertMeasureFor(view, subview, pc);
assertThat(getComponentUuidsOfMeasures()).containsOnly(view.uuid(), subview.uuid(), pc.uuid());
- underTest.deleteNonRootComponents(dbSession, singletonList(pc));
+ underTest.deleteNonRootComponentsInView(dbSession, singletonList(pc));
assertThat(getComponentUuidsOfMeasures())
.containsOnly(view.uuid(), subview.uuid());
- underTest.deleteNonRootComponents(dbSession, singletonList(subview));
+ underTest.deleteNonRootComponentsInView(dbSession, singletonList(subview));
assertThat(getComponentUuidsOfMeasures())
.containsOnly(view.uuid());
}
@Test
- public void deleteNonRootComponents_deletes_issues_and_changes_of_any_non_root_component_of_a_project() {
- ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPublicProject() : dbTester.components().insertPrivateProject();
- ComponentDto module1 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto dir1 = dbTester.components().insertComponent(newDirectory(module1, "A/B"));
- ComponentDto file1 = dbTester.components().insertComponent(newFileDto(dir1));
- insertIssueAndChangesFor(project, module1, dir1, file1);
- assertThat(getComponentUuidsOfIssueChanges()).containsOnly(project.uuid(), module1.uuid(), dir1.uuid(), file1.uuid());
-
- underTest.deleteNonRootComponents(dbSession, singletonList(file1));
- assertThat(getComponentUuidsOfIssueChanges())
- .containsOnly(project.uuid(), module1.uuid(), dir1.uuid());
-
- underTest.deleteNonRootComponents(dbSession, asList(module1, dir1));
- assertThat(getComponentUuidsOfIssueChanges())
- .containsOnly(project.uuid());
- }
-
- @Test
- public void deleteNonRootComponents_deletes_file_sources_of_file_component_of_a_project_only() {
- ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPublicProject() : dbTester.components().insertPrivateProject();
- ComponentDto module1 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto dir1 = dbTester.components().insertComponent(newDirectory(module1, "A/B"));
- ComponentDto file1 = dbTester.components().insertComponent(newFileDto(dir1));
- ComponentDto file2 = dbTester.components().insertComponent(newFileDto(dir1));
- insertFileSources(project, module1, dir1, file1, file2);
- assertThat(getComponentUuidsOfFileSources()).containsOnly(project.uuid(), module1.uuid(), dir1.uuid(), file1.uuid(), file2.uuid());
-
- underTest.deleteNonRootComponents(dbSession, singletonList(file1));
- assertThat(getComponentUuidsOfFileSources())
- .containsOnly(project.uuid(), module1.uuid(), dir1.uuid(), file2.uuid());
-
- underTest.deleteNonRootComponents(dbSession, asList(module1, dir1, file2));
- assertThat(getComponentUuidsOfFileSources())
- .containsOnly(project.uuid(), module1.uuid(), dir1.uuid());
- }
-
- @Test
- public void deleteNonRootComponents_does_not_delete_file_sources_of_non_root_components_of_a_view() {
- ComponentDto view = dbTester.components().insertView();
- ComponentDto subview = dbTester.components().insertComponent(ComponentTesting.newSubView(view));
- ComponentDto pc1 = dbTester.components().insertComponent(newProjectCopy("a", dbTester.components().insertPrivateProject(), view));
- ComponentDto pc2 = dbTester.components().insertComponent(newProjectCopy("b", dbTester.components().insertPrivateProject(), view));
- insertFileSources(view, subview, pc1, pc2);
- assertThat(getComponentUuidsOfFileSources()).containsOnly(view.uuid(), subview.uuid(), pc1.uuid(), pc2.uuid());
-
- underTest.deleteNonRootComponents(dbSession, singletonList(pc1));
- assertThat(getComponentUuidsOfFileSources())
- .containsOnly(view.uuid(), subview.uuid(), pc1.uuid(), pc2.uuid());
-
- underTest.deleteNonRootComponents(dbSession, asList(subview, pc2));
- assertThat(getComponentUuidsOfFileSources())
- .containsOnly(view.uuid(), subview.uuid(), pc1.uuid(), pc2.uuid());
- }
-
- @Test
- public void deleteNonRootComponents_deletes_properties_of_modules_of_a_project() {
- ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPublicProject() : dbTester.components().insertPrivateProject();
- ComponentDto module1 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto module2 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(module1));
- ComponentDto module3 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto dir1 = dbTester.components().insertComponent(newDirectory(module1, "A/B"));
- ComponentDto file1 = dbTester.components().insertComponent(newFileDto(dir1));
- insertPropertyFor(project, module1, module2, module3, dir1, file1);
- assertThat(getResourceIdOfProperties()).containsOnly(project.getId(), module1.getId(), module2.getId(), module3.getId(), dir1.getId(), file1.getId());
-
- underTest.deleteNonRootComponents(dbSession, singletonList(module3));
- assertThat(getResourceIdOfProperties())
- .containsOnly(project.getId(), module1.getId(), module2.getId(), dir1.getId(), file1.getId());
-
- underTest.deleteNonRootComponents(dbSession, asList(module1, module2, dir1, file1));
- assertThat(getResourceIdOfProperties())
- .containsOnly(project.getId(), dir1.getId(), file1.getId());
- }
-
- @Test
- public void deleteNonRootComponents_properties_of_subviews_of_a_view() {
+ public void deleteNonRootComponents_deletes_properties_of_subviews_of_a_view() {
ComponentDto view = dbTester.components().insertView();
ComponentDto subview1 = dbTester.components().insertComponent(ComponentTesting.newSubView(view));
ComponentDto subview2 = dbTester.components().insertComponent(ComponentTesting.newSubView(subview1));
insertPropertyFor(view, subview1, subview2, subview3, pc);
assertThat(getResourceIdOfProperties()).containsOnly(view.getId(), subview1.getId(), subview2.getId(), subview3.getId(), pc.getId());
- underTest.deleteNonRootComponents(dbSession, singletonList(subview1));
+ underTest.deleteNonRootComponentsInView(dbSession, singletonList(subview1));
assertThat(getResourceIdOfProperties())
.containsOnly(view.getId(), subview2.getId(), subview3.getId(), pc.getId());
- underTest.deleteNonRootComponents(dbSession, asList(subview2, subview3, pc));
+ underTest.deleteNonRootComponentsInView(dbSession, asList(subview2, subview3, pc));
assertThat(getResourceIdOfProperties())
.containsOnly(view.getId(), pc.getId());
}
@Test
- public void deleteNonRootComponents_deletes_manual_measures_of_modules_of_a_project() {
- ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPublicProject() : dbTester.components().insertPrivateProject();
- ComponentDto module1 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto module2 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(module1));
- ComponentDto module3 = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
- ComponentDto dir1 = dbTester.components().insertComponent(newDirectory(module1, "A/B"));
- ComponentDto file1 = dbTester.components().insertComponent(newFileDto(dir1));
- insertManualMeasureFor(project, module1, module2, module3, dir1, file1);
- assertThat(getComponentUuidsOfManualMeasures()).containsOnly(project.uuid(), module1.uuid(), module2.uuid(), module3.uuid(), dir1.uuid(), file1.uuid());
-
- underTest.deleteNonRootComponents(dbSession, singletonList(module3));
- assertThat(getComponentUuidsOfManualMeasures())
- .containsOnly(project.uuid(), module1.uuid(), module2.uuid(), dir1.uuid(), file1.uuid());
-
- underTest.deleteNonRootComponents(dbSession, asList(module1, module2, dir1, file1));
- assertThat(getComponentUuidsOfManualMeasures())
- .containsOnly(project.uuid(), dir1.uuid(), file1.uuid());
- }
-
- @Test
- public void deleteNonRootComponents_manual_measures_of_subviews_of_a_view() {
+ public void deleteNonRootComponentsInView_deletes_manual_measures_of_subviews_of_a_view() {
ComponentDto view = dbTester.components().insertView();
ComponentDto subview1 = dbTester.components().insertComponent(ComponentTesting.newSubView(view));
ComponentDto subview2 = dbTester.components().insertComponent(ComponentTesting.newSubView(subview1));
insertManualMeasureFor(view, subview1, subview2, subview3, pc);
assertThat(getComponentUuidsOfManualMeasures()).containsOnly(view.uuid(), subview1.uuid(), subview2.uuid(), subview3.uuid(), pc.uuid());
- underTest.deleteNonRootComponents(dbSession, singletonList(subview1));
+ underTest.deleteNonRootComponentsInView(dbSession, singletonList(subview1));
assertThat(getComponentUuidsOfManualMeasures())
.containsOnly(view.uuid(), subview2.uuid(), subview3.uuid(), pc.uuid());
- underTest.deleteNonRootComponents(dbSession, asList(subview2, subview3, pc));
+ underTest.deleteNonRootComponentsInView(dbSession, asList(subview2, subview3, pc));
assertThat(getComponentUuidsOfManualMeasures())
.containsOnly(view.uuid(), pc.uuid());
}
dbSession.commit();
}
- private Stream<String> getComponentUuidsOfFileSources() {
- return dbTester.select("select file_uuid as \"COMPONENT_UUID\" from file_sources").stream()
- .map(row -> (String) row.get("COMPONENT_UUID"));
- }
-
- private void insertFileSources(ComponentDto... components) {
- Arrays.stream(components).forEach(component -> dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
- .setFileUuid(component.uuid())
- .setCreatedAt(new Random().nextInt())
- .setUpdatedAt(new Random().nextInt())
- .setProjectUuid(randomAlphabetic(3))
- .setDataType("SOURCE")
- .setBinaryData(new byte[] {0, 1})));
- dbSession.commit();
- }
-
- private Stream<String> getComponentUuidsOfIssueChanges() {
- return dbTester.select("select i.component_uuid as \"COMPONENT_UUID\" from issue_changes ic inner join issues i on i.kee = ic.issue_key").stream()
- .map(row -> (String) row.get("COMPONENT_UUID"));
- }
-
- private void insertIssueAndChangesFor(ComponentDto project, ComponentDto... otherComponents) {
- Stream.concat(Stream.of(project), Arrays.stream(otherComponents)).forEach(componentDto -> {
- IssueDto issue = dbTester.issues().insert(RuleTesting.newRule(), project, componentDto);
- dbTester.issues().insertComment(issue, "foo", "bar");
- });
- dbSession.commit();
- }
-
private CeQueueDto createCeQueue(ComponentDto component, Status status) {
CeQueueDto queueDto = new CeQueueDto();
queueDto.setUuid(Uuids.create());