Skip to content

Commit

Permalink
Simplify deletion of a project in PurgeDao
Browse files Browse the repository at this point in the history
- Rename deleteResourceTree to deleteProject
- deleteProject only takes a uuid as parameter (instead of a IdUUidPair)
  • Loading branch information
julienlancelot committed Sep 15, 2015
1 parent 2285e07 commit 62c3129
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 41 deletions.
Expand Up @@ -29,8 +29,6 @@
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis; import org.sonar.db.MyBatis;
import org.sonar.db.component.ComponentDto; 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.IssueAuthorizationIndexer;
import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.issue.index.IssueIndexer;
import org.sonar.server.test.index.TestIndexer; import org.sonar.server.test.index.TestIndexer;
Expand Down Expand Up @@ -75,7 +73,7 @@ private void delete(DbSession dbSession, ComponentDto project) {
if (hasNotProjectScope(project) || isNotDeletable(project)) { if (hasNotProjectScope(project) || isNotDeletable(project)) {
throw new IllegalArgumentException("Only projects can be deleted"); 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(); dbSession.commit();


deleteFromIndices(project.uuid()); deleteFromIndices(project.uuid());
Expand Down
2 changes: 0 additions & 2 deletions sonar-db/src/main/java/org/sonar/db/MyBatis.java
Expand Up @@ -96,7 +96,6 @@
import org.sonar.db.permission.UserWithPermissionDto; import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.property.PropertiesMapper; import org.sonar.db.property.PropertiesMapper;
import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyDto;
import org.sonar.db.purge.IdUuidPair;
import org.sonar.db.purge.PurgeMapper; import org.sonar.db.purge.PurgeMapper;
import org.sonar.db.purge.PurgeableSnapshotDto; import org.sonar.db.purge.PurgeableSnapshotDto;
import org.sonar.db.qualitygate.ProjectQgateAssociationDto; import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
Expand Down Expand Up @@ -207,7 +206,6 @@ public MyBatis start() {
confBuilder.loadAlias("RequirementMigration", RequirementMigrationDto.class); confBuilder.loadAlias("RequirementMigration", RequirementMigrationDto.class);
confBuilder.loadAlias("Activity", ActivityDto.class); confBuilder.loadAlias("Activity", ActivityDto.class);
confBuilder.loadAlias("AnalysisReport", AnalysisReportDto.class); confBuilder.loadAlias("AnalysisReport", AnalysisReportDto.class);
confBuilder.loadAlias("IdUuidPair", IdUuidPair.class);
confBuilder.loadAlias("FilePathWithHash", FilePathWithHashDto.class); confBuilder.loadAlias("FilePathWithHash", FilePathWithHashDto.class);
confBuilder.loadAlias("UuidWithProjectUuid", UuidWithProjectUuidDto.class); confBuilder.loadAlias("UuidWithProjectUuid", UuidWithProjectUuidDto.class);
confBuilder.loadAlias("Event", EventDto.class); confBuilder.loadAlias("Event", EventDto.class);
Expand Down
36 changes: 12 additions & 24 deletions sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java
Expand Up @@ -81,13 +81,13 @@ public void purge(DbSession session, PurgeConfiguration conf, PurgeListener list
deleteOldClosedIssues(conf, mapper); deleteOldClosedIssues(conf, mapper);
} }


private void deleteOldClosedIssues(PurgeConfiguration conf, PurgeMapper mapper) { private static void deleteOldClosedIssues(PurgeConfiguration conf, PurgeMapper mapper) {
Date toDate = conf.maxLiveDateOfClosedIssues(); Date toDate = conf.maxLiveDateOfClosedIssues();
mapper.deleteOldClosedIssueChanges(conf.rootProjectIdUuid().getUuid(), dateToLong(toDate)); mapper.deleteOldClosedIssueChanges(conf.rootProjectIdUuid().getUuid(), dateToLong(toDate));
mapper.deleteOldClosedIssues(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)) { if (hasAbortedBuilds(project.getId(), commands)) {
LOG.debug("<- Delete aborted builds"); LOG.debug("<- Delete aborted builds");
PurgeSnapshotQuery query = PurgeSnapshotQuery.create() PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
Expand All @@ -98,15 +98,15 @@ private void deleteAbortedBuilds(ResourceDto project, PurgeCommands commands) {
} }
} }


private boolean hasAbortedBuilds(Long projectId, PurgeCommands commands) { private static boolean hasAbortedBuilds(Long projectId, PurgeCommands commands) {
PurgeSnapshotQuery query = PurgeSnapshotQuery.create() PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
.setIslast(false) .setIslast(false)
.setStatus(new String[] {"U"}) .setStatus(new String[] {"U"})
.setResourceId(projectId); .setResourceId(projectId);
return !commands.selectSnapshotIds(query).isEmpty(); 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( List<Long> projectSnapshotIds = purgeCommands.selectSnapshotIds(
PurgeSnapshotQuery.create() PurgeSnapshotQuery.create()
.setResourceId(project.getId()) .setResourceId(project.getId())
Expand Down Expand Up @@ -169,33 +169,21 @@ public List<PurgeableSnapshotDto> selectPurgeableSnapshots(long resourceId, DbSe
return result; return result;
} }


public PurgeDao deleteResourceTree(IdUuidPair rootIdUuid, PurgeProfiler profiler) { public PurgeDao deleteProject(DbSession session, String uuid) {
DbSession session = mybatis.openSession(true); PurgeProfiler profiler = new PurgeProfiler();
try { PurgeCommands purgeCommands = new PurgeCommands(session, profiler);
return deleteResourceTree(session, rootIdUuid, profiler); deleteProject(uuid, mapper(session), purgeCommands);
} finally { deleteFileSources(uuid, purgeCommands);
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));
return this; return this;
} }


private static void deleteFileSources(String rootUuid, PurgeCommands commands) { private static void deleteFileSources(String rootUuid, PurgeCommands commands) {
commands.deleteFileSources(rootUuid); commands.deleteFileSources(rootUuid);
} }


private static void deleteProject(IdUuidPair rootProjectId, PurgeMapper mapper, PurgeCommands commands) { private static void deleteProject(String rootUuid, PurgeMapper mapper, PurgeCommands commands) {
List<IdUuidPair> childrenIdUuid = mapper.selectProjectIdUuidsByRootId(rootProjectId.getId()); List<IdUuidPair> childrenIds = mapper.selectComponentsByProjectUuid(rootUuid);
for (IdUuidPair childId : childrenIdUuid) { commands.deleteResources(childrenIds);
deleteProject(childId, mapper, commands);
}

List<IdUuidPair> componentIdUuids = mapper.selectComponentIdUuidsByRootId(rootProjectId.getId());
commands.deleteResources(componentIdUuids);
} }


private void disableResource(IdUuidPair componentIdUuid, PurgeMapper mapper) { private void disableResource(IdUuidPair componentIdUuid, PurgeMapper mapper) {
Expand Down
7 changes: 4 additions & 3 deletions sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java
Expand Up @@ -29,9 +29,10 @@ public interface PurgeMapper {


List<Long> selectSnapshotIdsByResource(@Param("resourceIds") List<Long> resourceIds); List<Long> selectSnapshotIdsByResource(@Param("resourceIds") List<Long> resourceIds);


List<IdUuidPair> selectProjectIdUuidsByRootId(long rootResourceId); /**

* Returns the list of components of a project from a project_uuid. The project itself is also returned.
List<IdUuidPair> selectComponentIdUuidsByRootId(long rootProjectId); */
List<IdUuidPair> selectComponentsByProjectUuid(String projectUuid);


void deleteSnapshot(@Param("snapshotIds") List<Long> snapshotIds); void deleteSnapshot(@Param("snapshotIds") List<Long> snapshotIds);


Expand Down
10 changes: 3 additions & 7 deletions sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
Expand Up @@ -72,7 +72,7 @@
not exists(select e.id from events e where e.snapshot_id=s.id) not exists(select e.id from events e where e.snapshot_id=s.id)
</select> </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 select p.id, p.uuid from projects p
where (p.id=#{id} or p.root_id=#{id}) and p.enabled=${_true} 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) and not exists(select s.project_id from snapshots s where s.islast=${_true} and s.project_id=p.id)
Expand All @@ -82,12 +82,8 @@
select id from metrics where delete_historical_data=${_true} select id from metrics where delete_historical_data=${_true}
</select> </select>


<select id="selectProjectIdUuidsByRootId" resultType="IdUuidPair" parameterType="long"> <select id="selectComponentsByProjectUuid" resultType="org.sonar.db.purge.IdUuidPair" parameterType="String">
select id, uuid from projects where root_id=#{id} and scope='PRJ' select id, uuid from projects where project_uuid=#{uuid}
</select>

<select id="selectComponentIdUuidsByRootId" resultType="IdUuidPair" parameterType="long">
select id, uuid from projects where root_id=#{id} or id=#{id}
</select> </select>


<delete id="deleteSnapshotMeasures" parameterType="map"> <delete id="deleteSnapshotMeasures" parameterType="map">
Expand Down
8 changes: 6 additions & 2 deletions sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
Expand Up @@ -25,6 +25,7 @@
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
import org.sonar.api.resources.Scopes; import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2; import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester; import org.sonar.db.DbTester;
import org.sonar.test.DbTests; import org.sonar.test.DbTests;


Expand All @@ -40,6 +41,8 @@ public class PurgeDaoTest {
@Rule @Rule
public DbTester dbTester = DbTester.create(system2); public DbTester dbTester = DbTester.create(system2);


DbSession dbSession = dbTester.getSession();

PurgeDao underTest = dbTester.getDbClient().purgeDao(); PurgeDao underTest = dbTester.getDbClient().purgeDao();


@Test @Test
Expand Down Expand Up @@ -101,9 +104,10 @@ public void shouldSelectPurgeableSnapshots() {
} }


@Test @Test
public void should_delete_project_and_associated_data() { public void delete_project_and_associated_data() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteProject.xml"); 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("projects")).isZero();
assertThat(dbTester.countRowsOfTable("snapshots")).isZero(); assertThat(dbTester.countRowsOfTable("snapshots")).isZero();
assertThat(dbTester.countRowsOfTable("action_plans")).isZero(); assertThat(dbTester.countRowsOfTable("action_plans")).isZero();
Expand Down

0 comments on commit 62c3129

Please sign in to comment.