]> source.dussan.org Git - sonarqube.git/commitdiff
Simplify deletion of a project in PurgeDao 506/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 15 Sep 2015 10:01:40 +0000 (12:01 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 15 Sep 2015 11:08:46 +0000 (13:08 +0200)
- Rename deleteResourceTree to deleteProject
- deleteProject only takes a uuid as parameter (instead of a IdUUidPair)

server/sonar-server/src/main/java/org/sonar/server/component/ComponentCleanerService.java
sonar-db/src/main/java/org/sonar/db/MyBatis.java
sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java
sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java
sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java

index ee342adf1ad5faf92f6093db184174dac315fcd8..943e587f0fdcc8dd7f3541f225918ad4f33b484d 100644 (file)
@@ -29,8 +29,6 @@ import org.sonar.db.DbClient;
 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;
@@ -75,7 +73,7 @@ public class ComponentCleanerService {
     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());
index dbd1dba7bc4eb2d57da96682419c39dbd509fe45..e8345295856c6c18d21b374eb9e582e64ca0fed8 100644 (file)
@@ -96,7 +96,6 @@ import org.sonar.db.permission.PermissionTemplateUserDto;
 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;
@@ -207,7 +206,6 @@ public class MyBatis {
     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);
index abd114538ce8862689e1a279117145be1c228e16..75213d7d2031f8137380599b9056872e6acb7e74 100644 (file)
@@ -81,13 +81,13 @@ public class PurgeDao implements Dao {
     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()
@@ -98,7 +98,7 @@ public class PurgeDao implements Dao {
     }
   }
 
-  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"})
@@ -106,7 +106,7 @@ public class PurgeDao implements Dao {
     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())
@@ -169,18 +169,11 @@ public class PurgeDao implements Dao {
     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;
   }
 
@@ -188,14 +181,9 @@ public class PurgeDao implements Dao {
     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) {
index 2cc04037aed2235473a5ac0e4566dba4e0c31391..e6c8c845fcd956affa133345c7df92368d8f1168 100644 (file)
@@ -29,9 +29,10 @@ public interface PurgeMapper {
 
   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);
 
index 49555dd8ca5485c756bb24facb936d9affda2a2c..6307b26275be4a20bcf4735e9c125c2f272772ea 100644 (file)
@@ -72,7 +72,7 @@
     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">
index 84aaa26470aac3a25213b7a005a67a7313bcbbb1..0ff1acd378fb2a59a64234a1be9666947a853ac3 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Test;
 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;
 
@@ -40,6 +41,8 @@ public class PurgeDaoTest {
   @Rule
   public DbTester dbTester = DbTester.create(system2);
 
+  DbSession dbSession = dbTester.getSession();
+
   PurgeDao underTest = dbTester.getDbClient().purgeDao();
 
   @Test
@@ -101,9 +104,10 @@ public class PurgeDaoTest {
   }
 
   @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();