/** * 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 void deleteNonRootComponentsInView(DbSession dbSession, Collection<ComponentDto> components) { Set<ComponentDto> nonRootComponents = components.stream().filter(PurgeDao::isNotRoot).collect(MoreCollectors.toSet()); if (nonRootComponents.isEmpty()) { return; } PurgeProfiler profiler = new PurgeProfiler(); PurgeCommands purgeCommands = new PurgeCommands(dbSession, profiler); deleteNonRootComponentsInView(nonRootComponents, purgeCommands); }
public void deleteBranch(DbSession session, String uuid) { PurgeProfiler profiler = new PurgeProfiler(); PurgeMapper purgeMapper = mapper(session); PurgeCommands purgeCommands = new PurgeCommands(session, profiler); deleteRootComponent(uuid, purgeMapper, purgeCommands); }
public void deleteProject(DbSession session, String uuid) { PurgeProfiler profiler = new PurgeProfiler(); PurgeMapper purgeMapper = mapper(session); PurgeCommands purgeCommands = new PurgeCommands(session, profiler); session.getMapper(BranchMapper.class).selectByProjectUuid(uuid) .stream() .filter(branch -> !uuid.equals(branch.getUuid())) .forEach(branch -> deleteRootComponent(branch.getUuid(), purgeMapper, purgeCommands)); deleteRootComponent(uuid, purgeMapper, purgeCommands); }
@Before public void prepare() { clock = new MockedClock(); profiler = new PurgeProfiler(clock); logger = mock(Logger.class); }
@Test public void shouldDeleteAnalyses() { db.prepareDbUnit(getClass(), "shouldDeleteAnalyses.xml"); underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(3, "u3"))); db.assertDbUnit(getClass(), "shouldDeleteAnalyses-result.xml", "snapshots"); }
@Test public void shouldDeleteHistoricalDataOfDirectoriesAndFiles() { db.prepareDbUnit(getClass(), "shouldDeleteHistoricalDataOfDirectoriesAndFiles.xml"); PurgeConfiguration conf = new PurgeConfiguration(new IdUuidPair(THE_PROJECT_ID, "PROJECT_UUID"), asList(Scopes.DIRECTORY, Scopes.FILE), 30, Optional.of(30), System2.INSTANCE, Collections.emptyList()); underTest.purge(dbSession, conf, PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); db.assertDbUnit(getClass(), "shouldDeleteHistoricalDataOfDirectoriesAndFiles-result.xml", "projects", "snapshots", "project_measures"); }
@Test public void purge_history_of_project() { db.prepareDbUnit(getClass(), "shouldPurgeProject.xml"); underTest.purge(dbSession, newConfigurationWith30Days(), PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); db.assertDbUnit(getClass(), "shouldPurgeProject-result.xml", "projects", "snapshots"); }
@Test public void purge_failed_ce_tasks() { db.prepareDbUnit(getClass(), "shouldDeleteAbortedBuilds.xml"); underTest.purge(dbSession, newConfigurationWith30Days(), PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); db.assertDbUnit(getClass(), "shouldDeleteAbortedBuilds-result.xml", "snapshots"); }
@Test public void doClean() { PurgeDao dao = mock(PurgeDao.class); DbSession session = mock(DbSession.class); when(dao.selectPurgeableAnalyses("uuid_123", session)).thenReturn(Arrays.asList( new PurgeableAnalysisDto().setAnalysisId(999).setAnalysisUuid("u999").setDate(System2.INSTANCE.now()), new PurgeableAnalysisDto().setAnalysisId(456).setAnalysisUuid("u456").setDate(System2.INSTANCE.now()) )); Filter filter1 = newFirstSnapshotInListFilter(); Filter filter2 = newFirstSnapshotInListFilter(); PurgeProfiler profiler = new PurgeProfiler(); DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, profiler); cleaner.doClean("uuid_123", Arrays.asList(filter1, filter2), session); InOrder inOrder = Mockito.inOrder(dao, filter1, filter2); inOrder.verify(filter1).log(); inOrder.verify(dao, times(1)).deleteAnalyses(eq(session), eq(profiler), eq(ImmutableList.of(new IdUuidPair(999, "u999")))); inOrder.verify(filter2).log(); inOrder.verify(dao, times(1)).deleteAnalyses(eq(session), eq(profiler), eq(ImmutableList.of(new IdUuidPair(456, "u456")))); inOrder.verifyNoMoreInteractions(); }
@Test public void purge_inactive_short_living_branches() { when(system2.now()).thenReturn(new Date().getTime()); RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertMainBranch(); ComponentDto longBranch = db.components().insertProjectBranch(project); ComponentDto recentShortBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.SHORT)); // short branch with other components and issues, updated 31 days ago when(system2.now()).thenReturn(DateUtils.addDays(new Date(), -31).getTime()); ComponentDto shortBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.SHORT)); ComponentDto module = db.components().insertComponent(newModuleDto(shortBranch)); ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); ComponentDto file = db.components().insertComponent(newFileDto(subModule)); db.issues().insert(rule, shortBranch, file); db.issues().insert(rule, shortBranch, subModule); db.issues().insert(rule, shortBranch, module); // back to present when(system2.now()).thenReturn(new Date().getTime()); underTest.purge(dbSession, newConfigurationWith30Days(system2, project.uuid()), PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); assertThat(uuidsIn("projects")).containsOnly(project.uuid(), longBranch.uuid(), recentShortBranch.uuid()); }
@Test public void purge_inactive_pull_request() { when(system2.now()).thenReturn(new Date().getTime()); RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertMainBranch(); ComponentDto longBranch = db.components().insertProjectBranch(project); ComponentDto recentPullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); // pull request with other components and issues, updated 31 days ago when(system2.now()).thenReturn(DateUtils.addDays(new Date(), -31).getTime()); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); ComponentDto module = db.components().insertComponent(newModuleDto(pullRequest)); ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); ComponentDto file = db.components().insertComponent(newFileDto(subModule)); db.issues().insert(rule, pullRequest, file); db.issues().insert(rule, pullRequest, subModule); db.issues().insert(rule, pullRequest, module); // back to present when(system2.now()).thenReturn(new Date().getTime()); underTest.purge(dbSession, newConfigurationWith30Days(system2, project.uuid()), PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); assertThat(uuidsIn("projects")).containsOnly(project.uuid(), longBranch.uuid(), recentPullRequest.uuid()); }
@Test public void should_delete_old_closed_issues() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertMainBranch(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto file = db.components().insertComponent(newFileDto(module)); IssueDto oldClosed = db.issues().insert(rule, project, file, issue -> { issue.setStatus("CLOSED"); issue.setIssueCloseDate(DateUtils.addDays(new Date(), -31)); }); IssueDto notOldEnoughClosed = db.issues().insert(rule, project, file, issue -> { issue.setStatus("CLOSED"); issue.setIssueCloseDate(new Date()); }); IssueDto notClosed = db.issues().insert(rule, project, file); when(system2.now()).thenReturn(new Date().getTime()); underTest.purge(dbSession, newConfigurationWith30Days(system2, project.uuid()), PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); // old closed got deleted assertThat(db.getDbClient().issueDao().selectByKey(dbSession, oldClosed.getKey())).isEmpty(); // others remain assertThat(db.countRowsOfTable("issues")).isEqualTo(2); assertThat(db.getDbClient().issueDao().selectByKey(dbSession, notOldEnoughClosed.getKey())).isNotEmpty(); assertThat(db.getDbClient().issueDao().selectByKey(dbSession, notClosed.getKey())).isNotEmpty(); }
underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(3, "foo"))); assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")) .containsOnly(projectAnalysis1.getUuid(), projectAnalysis2.getUuid()); .containsOnly(projectEvent1.getUuid(), projectEvent2.getUuid(), projectEvent3.getUuid()); underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(projectAnalysis1.getId(), projectAnalysis1.getUuid()))); assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")) .containsOnly(projectAnalysis2.getUuid()); .containsOnly(projectEvent2.getUuid(), projectEvent3.getUuid()); underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(projectAnalysis4.getId(), projectAnalysis4.getUuid()))); assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")) .containsOnly(projectAnalysis2.getUuid()); .containsOnly(projectEvent2.getUuid(), projectEvent3.getUuid()); underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(projectAnalysis3.getId(), projectAnalysis3.getUuid()))); assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")) .containsOnly(projectAnalysis2.getUuid()); .containsOnly(projectEvent2.getUuid()); underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(projectAnalysis2.getId(), projectAnalysis2.getUuid()))); assertThat(db.countRowsOfTable("event_component_changes")) .isZero();
new PurgeProfiler()); dbSession.commit();
public PurgeDao deleteProject(DbSession session, String uuid) { PurgeProfiler profiler = new PurgeProfiler(); PurgeCommands purgeCommands = new PurgeCommands(session, profiler); deleteProject(uuid, mapper(session), purgeCommands); return this; }