import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.purge.IdUuidPair;
-import org.sonar.db.purge.PurgeProfiler;
import org.sonar.server.issue.index.IssueAuthorizationIndexer;
import org.sonar.server.issue.index.IssueIndexer;
import org.sonar.server.test.index.TestIndexer;
if (hasNotProjectScope(project) || isNotDeletable(project)) {
throw new IllegalArgumentException("Only projects can be deleted");
}
- dbClient.purgeDao().deleteResourceTree(dbSession, new IdUuidPair(project.getId(), project.uuid()), new PurgeProfiler());
+ dbClient.purgeDao().deleteProject(dbSession, project.uuid());
dbSession.commit();
deleteFromIndices(project.uuid());
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.property.PropertiesMapper;
import org.sonar.db.property.PropertyDto;
-import org.sonar.db.purge.IdUuidPair;
import org.sonar.db.purge.PurgeMapper;
import org.sonar.db.purge.PurgeableSnapshotDto;
import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
confBuilder.loadAlias("RequirementMigration", RequirementMigrationDto.class);
confBuilder.loadAlias("Activity", ActivityDto.class);
confBuilder.loadAlias("AnalysisReport", AnalysisReportDto.class);
- confBuilder.loadAlias("IdUuidPair", IdUuidPair.class);
confBuilder.loadAlias("FilePathWithHash", FilePathWithHashDto.class);
confBuilder.loadAlias("UuidWithProjectUuid", UuidWithProjectUuidDto.class);
confBuilder.loadAlias("Event", EventDto.class);
deleteOldClosedIssues(conf, mapper);
}
- private void deleteOldClosedIssues(PurgeConfiguration conf, PurgeMapper mapper) {
+ private static void deleteOldClosedIssues(PurgeConfiguration conf, PurgeMapper mapper) {
Date toDate = conf.maxLiveDateOfClosedIssues();
mapper.deleteOldClosedIssueChanges(conf.rootProjectIdUuid().getUuid(), dateToLong(toDate));
mapper.deleteOldClosedIssues(conf.rootProjectIdUuid().getUuid(), dateToLong(toDate));
}
- private void deleteAbortedBuilds(ResourceDto project, PurgeCommands commands) {
+ private static void deleteAbortedBuilds(ResourceDto project, PurgeCommands commands) {
if (hasAbortedBuilds(project.getId(), commands)) {
LOG.debug("<- Delete aborted builds");
PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
}
}
- private boolean hasAbortedBuilds(Long projectId, PurgeCommands commands) {
+ private static boolean hasAbortedBuilds(Long projectId, PurgeCommands commands) {
PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
.setIslast(false)
.setStatus(new String[] {"U"})
return !commands.selectSnapshotIds(query).isEmpty();
}
- private void purge(ResourceDto project, String[] scopesWithoutHistoricalData, PurgeCommands purgeCommands) {
+ private static void purge(ResourceDto project, String[] scopesWithoutHistoricalData, PurgeCommands purgeCommands) {
List<Long> projectSnapshotIds = purgeCommands.selectSnapshotIds(
PurgeSnapshotQuery.create()
.setResourceId(project.getId())
return result;
}
- public PurgeDao deleteResourceTree(IdUuidPair rootIdUuid, PurgeProfiler profiler) {
- DbSession session = mybatis.openSession(true);
- try {
- return deleteResourceTree(session, rootIdUuid, profiler);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public PurgeDao deleteResourceTree(DbSession session, IdUuidPair rootIdUuid, PurgeProfiler profiler) {
- deleteProject(rootIdUuid, mapper(session), new PurgeCommands(session, profiler));
- deleteFileSources(rootIdUuid.getUuid(), new PurgeCommands(session, profiler));
+ public PurgeDao deleteProject(DbSession session, String uuid) {
+ PurgeProfiler profiler = new PurgeProfiler();
+ PurgeCommands purgeCommands = new PurgeCommands(session, profiler);
+ deleteProject(uuid, mapper(session), purgeCommands);
+ deleteFileSources(uuid, purgeCommands);
return this;
}
commands.deleteFileSources(rootUuid);
}
- private static void deleteProject(IdUuidPair rootProjectId, PurgeMapper mapper, PurgeCommands commands) {
- List<IdUuidPair> childrenIdUuid = mapper.selectProjectIdUuidsByRootId(rootProjectId.getId());
- for (IdUuidPair childId : childrenIdUuid) {
- deleteProject(childId, mapper, commands);
- }
-
- List<IdUuidPair> componentIdUuids = mapper.selectComponentIdUuidsByRootId(rootProjectId.getId());
- commands.deleteResources(componentIdUuids);
+ private static void deleteProject(String rootUuid, PurgeMapper mapper, PurgeCommands commands) {
+ List<IdUuidPair> childrenIds = mapper.selectComponentsByProjectUuid(rootUuid);
+ commands.deleteResources(childrenIds);
}
private void disableResource(IdUuidPair componentIdUuid, PurgeMapper mapper) {
List<Long> selectSnapshotIdsByResource(@Param("resourceIds") List<Long> resourceIds);
- List<IdUuidPair> selectProjectIdUuidsByRootId(long rootResourceId);
-
- List<IdUuidPair> selectComponentIdUuidsByRootId(long rootProjectId);
+ /**
+ * Returns the list of components of a project from a project_uuid. The project itself is also returned.
+ */
+ List<IdUuidPair> selectComponentsByProjectUuid(String projectUuid);
void deleteSnapshot(@Param("snapshotIds") List<Long> snapshotIds);
not exists(select e.id from events e where e.snapshot_id=s.id)
</select>
- <select id="selectComponentIdUuidsToDisable" resultType="IdUuidPair" parameterType="long">
+ <select id="selectComponentIdUuidsToDisable" resultType="org.sonar.db.purge.IdUuidPair" parameterType="long">
select p.id, p.uuid from projects p
where (p.id=#{id} or p.root_id=#{id}) and p.enabled=${_true}
and not exists(select s.project_id from snapshots s where s.islast=${_true} and s.project_id=p.id)
select id from metrics where delete_historical_data=${_true}
</select>
- <select id="selectProjectIdUuidsByRootId" resultType="IdUuidPair" parameterType="long">
- select id, uuid from projects where root_id=#{id} and scope='PRJ'
- </select>
-
- <select id="selectComponentIdUuidsByRootId" resultType="IdUuidPair" parameterType="long">
- select id, uuid from projects where root_id=#{id} or id=#{id}
+ <select id="selectComponentsByProjectUuid" resultType="org.sonar.db.purge.IdUuidPair" parameterType="String">
+ select id, uuid from projects where project_uuid=#{uuid}
</select>
<delete id="deleteSnapshotMeasures" parameterType="map">
import org.junit.experimental.categories.Category;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.test.DbTests;
@Rule
public DbTester dbTester = DbTester.create(system2);
+ DbSession dbSession = dbTester.getSession();
+
PurgeDao underTest = dbTester.getDbClient().purgeDao();
@Test
}
@Test
- public void should_delete_project_and_associated_data() {
+ public void delete_project_and_associated_data() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteProject.xml");
- underTest.deleteResourceTree(new IdUuidPair(1L, "A"), new PurgeProfiler());
+ underTest.deleteProject(dbSession, "A");
+ dbSession.commit();
assertThat(dbTester.countRowsOfTable("projects")).isZero();
assertThat(dbTester.countRowsOfTable("snapshots")).isZero();
assertThat(dbTester.countRowsOfTable("action_plans")).isZero();