]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15259 Use portfolios related tables in RemoveDeprecatedViewsStep
authorJacek <jacek.poreda@sonarsource.com>
Tue, 24 Aug 2021 14:18:06 +0000 (16:18 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 8 Sep 2021 20:03:35 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java

index dffc171354290caec1bf73336b8cfa42c0521c0a..14162c0b7c55653c7a6c5e150e7f9a344f62df03 100644 (file)
@@ -33,9 +33,6 @@ import org.sonar.db.project.ProjectDto;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Collections.singleton;
-import static java.util.stream.Collectors.groupingBy;
-import static java.util.stream.Collectors.mapping;
-import static java.util.stream.Collectors.toSet;
 import static org.sonar.db.DatabaseUtils.executeLargeInputs;
 
 public class PortfolioDao implements Dao {
@@ -149,12 +146,8 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectProjects(portfolioUuid);
   }
 
-  public Map<String, Set<String>> getAllProjectsInHierarchy(DbSession dbSession, String rootUuid) {
-    return mapper(dbSession).selectAllProjectsInHierarchy(rootUuid)
-      .stream()
-      .collect(groupingBy(
-        PortfolioProjectDto::getProjectUuid,
-        mapping(PortfolioProjectDto::getPortfolioUuid, toSet())));
+  public List<PortfolioProjectDto> getAllProjectsInHierarchy(DbSession dbSession, String rootUuid) {
+    return mapper(dbSession).selectAllProjectsInHierarchy(rootUuid);
   }
 
   public void addProject(DbSession dbSession, String portfolioUuid, String projectUuid) {
@@ -176,13 +169,20 @@ public class PortfolioDao implements Dao {
   public void deleteAllProjects(DbSession dbSession) {
     mapper(dbSession).deleteAllProjects();
   }
-  
+
   public List<PortfolioProjectDto> selectAllProjectsOfPortfolios(DbSession dbSession) {
     return mapper(dbSession).selectAllProjectsOfPortfolios();
   }
 
+  public List<ReferenceDto> selectAllReferencesInHierarchy(DbSession dbSession, String uuid) {
+    return mapper(dbSession).selectAllReferencesInHierarchy(uuid);
+  }
+
+  public List<PortfolioDto> selectByUuids(DbSession dbSession, Set<String> uuidsToRefresh) {
+    return mapper(dbSession).selectByUuids(uuidsToRefresh);
+  }
+
   private static PortfolioMapper mapper(DbSession session) {
     return session.getMapper(PortfolioMapper.class);
   }
-
 }
index 34c9f699659b67e7c86c1c726a2d24a61c5a2592..49cc54ba540d50026b7b74c2381d1fae09f24cb0 100644 (file)
@@ -59,7 +59,7 @@ public interface PortfolioMapper {
 
   List<ReferenceDto> selectAllReferencesToApplications();
 
-  Set<PortfolioProjectDto> selectAllProjectsInHierarchy(String rootUuid);
+  List<PortfolioProjectDto> selectAllProjectsInHierarchy(String rootUuid);
 
   List<PortfolioDto> selectByUuids(@Param("uuids") Collection<String> uuids);
 
@@ -91,4 +91,5 @@ public interface PortfolioMapper {
 
   List<ReferenceDto> selectAllReferencesOfPortfolios();
 
+  List<ReferenceDto> selectAllReferencesInHierarchy(String rootUuid);
 }
index 089e4bad195a4c2aaaa8c4607294ba0f426bb42c..8176cd6719aa43ae4bc31ab8a6a1db907217139f 100644 (file)
     INNER JOIN projects target ON pr.reference_uuid = target.uuid where target.qualifier = 'APP'
   </select>
 
+  <select id="selectAllReferencesInHierarchy" resultType="org.sonar.db.portfolio.ReferenceDto">
+    (SELECT
+    source.uuid as sourceUuid,
+    source.root_uuid as sourceRootUuid,
+    target.uuid as targetUuid,
+    target.root_uuid as targetRootUuid
+    FROM portfolio_references pr
+    INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
+    INNER JOIN portfolios target ON pr.reference_uuid = target.uuid
+    WHERE source.root_uuid = #{rootUuid,jdbcType=VARCHAR}
+    UNION
+    SELECT
+    source.uuid as sourceUuid,
+    source.root_uuid as sourceRootUuid,
+    target.uuid as targetUuid,
+    target.uuid as targetRootUuid
+    FROM portfolio_references pr
+    INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
+    INNER JOIN projects target ON pr.reference_uuid = target.uuid
+    WHERE target.qualifier = 'APP'
+    AND source.root_uuid = #{rootUuid,jdbcType=VARCHAR})
+  </select>
+
   <delete id="deletePortfoliosByUuids" parameterType="String">
     DELETE FROM portfolios WHERE uuid in
     <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
index fb40b95eb77d458f997e6cdb82fbb2fd4f924c39..7bbfe7b3a1c7dc36f8254c87cae7beab20a53012 100644 (file)
@@ -296,7 +296,8 @@ public class PortfolioDaoTest {
     portfolioDao.addProject(session, root2.getUuid(), "p4");
     session.commit();
 
-    assertThat(portfolioDao.getAllProjectsInHierarchy(session, root.getUuid()).keySet()).containsExactly("p1", "p2", "p3");
+    assertThat(portfolioDao.getAllProjectsInHierarchy(session, root.getUuid()))
+      .extracting(PortfolioProjectDto::getProjectUuid).containsExactly("p1", "p2", "p3");
     assertThat(portfolioDao.getAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
   }
 
index ed3f1498fd9d766332ba3d2c64db1058a8e2c009..fe8b4374f268cb831263aff828f6f70199f1b029 100644 (file)
@@ -255,27 +255,32 @@ public class ComponentDbTester {
     return insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator, portfolioPopulator);
   }
 
-  public void addPortfolioReference(ComponentDto portfolio, String... referencerUuids) {
+  public void addPortfolioReference(String portfolioUuid, String... referencerUuids) {
     for (String uuid : referencerUuids) {
-      dbClient.portfolioDao().addReference(dbSession, portfolio.uuid(), uuid);
+      dbClient.portfolioDao().addReference(dbSession, portfolioUuid, uuid);
     }
     db.commit();
   }
 
+  public void addPortfolioReference(ComponentDto portfolio, String... referencerUuids) {
+    addPortfolioReference(portfolio.uuid(), referencerUuids);
+  }
+
   public void addPortfolioReference(PortfolioDto portfolio, String... referencerUuids) {
-    for (String uuid : referencerUuids) {
-      dbClient.portfolioDao().addReference(dbSession, portfolio.getUuid(), uuid);
-    }
-    db.commit();
+    addPortfolioReference(portfolio.getUuid(), referencerUuids);
   }
 
-  public void addPortfolioProject(ComponentDto portfolio, String... projectUuids) {
+  public void addPortfolioProject(String portfolioUuid, String... projectUuids) {
     for (String uuid : projectUuids) {
-      dbClient.portfolioDao().addProject(dbSession, portfolio.uuid(), uuid);
+      dbClient.portfolioDao().addProject(dbSession, portfolioUuid, uuid);
     }
     db.commit();
   }
 
+  public void addPortfolioProject(ComponentDto portfolio, String... projectUuids) {
+    addPortfolioProject(portfolio.uuid(), projectUuids);
+  }
+
   public void addPortfolioProject(ComponentDto portfolio, ComponentDto... projects) {
     addPortfolioProject(portfolio, Arrays.stream(projects).map(ComponentDto::uuid).toArray(String[]::new));
   }