]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15259 Use portfolios realted tables in DefineAction WS
authorJacek <jacek.poreda@sonarsource.com>
Fri, 20 Aug 2021 14:58:53 +0000 (16:58 +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 d9c49ae21784baadbc9ab6df1d2dec1d15a43cdc..d779c6845fa847a6c6ff5b01b22da958e109e0c4 100644 (file)
@@ -64,9 +64,6 @@ public class PortfolioDao implements Dao {
   }
 
   public void insert(DbSession dbSession, PortfolioDto portfolio) {
-    if (portfolio.getUuid() == null) {
-      portfolio.setUuid(uuidFactory.create());
-    }
     mapper(dbSession).insert(portfolio);
   }
 
@@ -160,4 +157,22 @@ public class PortfolioDao implements Dao {
     return executeLargeInputs(uuids, uuids1 -> mapper(dbSession).selectByUuids(uuids1)).stream()
       .collect(Collectors.toMap(PortfolioDto::getUuid, PortfolioDto::getKey));
   }
+
+  public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
+    mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
+  }
+
+  public void deleteAllReferences(DbSession dbSession) {
+    mapper(dbSession).deleteAllReferences();
+
+  }
+
+  public void deleteAllProjects(DbSession dbSession) {
+    mapper(dbSession).deleteAllProjects();
+  }
+
+  public List<PortfolioProjectDto> selectAllProjectsOfPortfolios(DbSession dbSession) {
+    return mapper(dbSession).selectAllProjectsOfPortfolios();
+  }
+
 }
index f019dbaed05c47b9c0027bdb5d53ff9492263966..6587896849bacb5f4f9f418378d1a9c3abd4d119 100644 (file)
@@ -76,4 +76,15 @@ public interface PortfolioMapper {
   void deleteProjects(String portfolioUuid);
 
   void deleteProject(@Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid);
+
+  void deleteAllDescendantPortfolios(String rootUuid);
+
+  void deleteAllReferences();
+
+  void deleteAllProjects();
+
+  List<PortfolioProjectDto> selectAllProjectsOfPortfolios();
+
+  List<ReferenceDto> selectAllReferencesOfPortfolios();
+
 }
index 3e58120500401156cd6a90f80a1252ab87c3f2c3..938e314e1294c3da3deb648ed02f4bded953a432 100644 (file)
       p.kee=#{kee,jdbcType=VARCHAR}
   </select>
 
-  <select id="selectAllRoots" resultType="Portfolio">
+  <select id="selectAll" resultType="Portfolio">
     SELECT
-      <include refid="portfolioColumns"/>
+    <include refid="portfolioColumns"/>
     FROM portfolios p
-    where
-      p.parent_uuid is null
   </select>
 
-  <select id="selectAll" resultType="Portfolio">
+  <select id="selectAllRoots" resultType="Portfolio">
     SELECT
       <include refid="portfolioColumns"/>
     FROM portfolios p
+    where
+      p.parent_uuid is null
   </select>
 
    <select id="selectProjects" resultType="Project">
       pp.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR}
   </select>
 
+  <select id="selectAllProjectsOfPortfolios" resultType="org.sonar.db.portfolio.PortfolioProjectDto">
+    SELECT
+       pp.uuid,
+       pp.project_uuid as projectUuid,
+       pp.portfolio_uuid as portfolioUuid,
+       pp.project_uuid as projectUuid,
+       pp.created_at as createdAt
+    FROM portfolio_projects pp
+    INNER JOIN projects p on p.uuid = pp.project_uuid
+  </select>
+
   <select id="selectAllProjectsInHierarchy" resultType="org.sonar.db.portfolio.PortfolioProjectDto">
     SELECT
       pp.uuid,
     <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
   </delete>
 
+  <delete id="deleteAllDescendantPortfolios" parameterType="String">
+    DELETE FROM portfolios WHERE root_uuid = #{rootUuid,jdbcType=VARCHAR} and parent_uuid is not null
+  </delete>
+
+  <delete id="deleteAllReferences" parameterType="String">
+    DELETE FROM portfolio_references
+  </delete>
+
+  <delete id="deleteAllProjects" parameterType="String">
+    DELETE FROM portfolio_projects
+  </delete>
+
   <insert id="insertReference" parameterType="PortfolioReference">
     INSERT INTO portfolio_references (
       uuid,
index d921b1477deb4b4b6881074392d817e17422a874..afbd38a25b7d3b9dc74f22b2c44dd57d7b48088d 100644 (file)
@@ -237,6 +237,61 @@ public class PortfolioDaoTest {
     assertThat(portfolioDao.getAllProjectsInHierarchy(db.getSession(), "nonexisting")).isEmpty();
   }
 
+  @Test
+  public void deleteAllDescendantPortfolios() {
+    createPortfolio("root", null, "root");
+    createPortfolio("child1", null, "root");
+    createPortfolio("child11", "child1", "root");
+
+    createPortfolio("root2", null, "root2");
+
+    portfolioDao.deleteAllDescendantPortfolios(db.getSession(), "root");
+    portfolioDao.deleteAllDescendantPortfolios(db.getSession(), "root2");
+
+    assertThat(db.countSql(db.getSession(), "select count(*) from portfolios where parent_uuid is not null")).isZero();
+  }
+
+  @Test
+  public void deleteAllReferences() {
+    createPortfolio("root", null, "root");
+    createPortfolio("child1", null, "root");
+    createPortfolio("child11", "child1", "root");
+
+    createPortfolio("root2", null, "root2");
+    createPortfolio("root3", null, "root3");
+    createPortfolio("root4", null, "root4");
+
+    portfolioDao.addReference(db.getSession(), "child1", "root2");
+    portfolioDao.addReference(db.getSession(), "root3", "root4");
+
+    portfolioDao.deleteAllReferences(db.getSession());
+
+    assertThat(db.countSql(db.getSession(), "select count(*) from portfolio_references")).isZero();
+  }
+
+  @Test
+  public void deleteAllProjects() {
+    db.components().insertPrivateProject("p1");
+    db.components().insertPrivateProject("p2");
+    db.components().insertPrivateProject("p3");
+    db.components().insertPrivateProject("p4");
+
+    createPortfolio("root", null, "root");
+    createPortfolio("child1", null, "root");
+    createPortfolio("child11", "child1", "root");
+
+    createPortfolio("root2", null, "root2");
+
+    portfolioDao.addProject(db.getSession(), "root", "p1");
+    portfolioDao.addProject(db.getSession(), "child1", "p2");
+    portfolioDao.addProject(db.getSession(), "child11", "p3");
+    portfolioDao.addProject(db.getSession(), "root2", "p4");
+
+    portfolioDao.deleteAllProjects(db.getSession());
+
+    assertThat(db.countSql(db.getSession(), "select count(*) from portfolio_projects")).isZero();
+  }
+
   private PortfolioDto createPortfolio(String uuid, @Nullable String parentUuid, String rootUuid) {
     PortfolioDto p = new PortfolioDto()
       .setName("name_" + uuid)
index 905d329773146fa383541aa251a50e6f630b241b..79e61335dd6bb837571c807c113643c1215c4fc3 100644 (file)
@@ -236,6 +236,13 @@ public class ComponentDbTester {
     return insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator, portfolioPopulator);
   }
 
+  public void addPortfolioReference(ComponentDto portfolio, String... referencerUuids) {
+    for (String uuid : referencerUuids) {
+      dbClient.portfolioDao().addReference(dbSession, portfolio.uuid(), uuid);
+    }
+    db.commit();
+  }
+
   public void addPortfolioProject(ComponentDto portfolio, String... projectUuids) {
     for (String uuid : projectUuids) {
       dbClient.portfolioDao().addProject(dbSession, portfolio.uuid(), uuid);