From 7aca0ec0cce7c8129ab96690dc31548a1979bb6d Mon Sep 17 00:00:00 2001 From: Jacek Date: Tue, 24 Aug 2021 16:18:06 +0200 Subject: [PATCH] SONAR-15259 Use portfolios related tables in RemoveDeprecatedViewsStep --- .../org/sonar/db/portfolio/PortfolioDao.java | 22 +++++++++--------- .../sonar/db/portfolio/PortfolioMapper.java | 3 ++- .../sonar/db/portfolio/PortfolioMapper.xml | 23 +++++++++++++++++++ .../sonar/db/portfolio/PortfolioDaoTest.java | 3 ++- .../sonar/db/component/ComponentDbTester.java | 21 ++++++++++------- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java index dffc1713542..14162c0b7c5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java @@ -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> getAllProjectsInHierarchy(DbSession dbSession, String rootUuid) { - return mapper(dbSession).selectAllProjectsInHierarchy(rootUuid) - .stream() - .collect(groupingBy( - PortfolioProjectDto::getProjectUuid, - mapping(PortfolioProjectDto::getPortfolioUuid, toSet()))); + public List 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 selectAllProjectsOfPortfolios(DbSession dbSession) { return mapper(dbSession).selectAllProjectsOfPortfolios(); } + public List selectAllReferencesInHierarchy(DbSession dbSession, String uuid) { + return mapper(dbSession).selectAllReferencesInHierarchy(uuid); + } + + public List selectByUuids(DbSession dbSession, Set uuidsToRefresh) { + return mapper(dbSession).selectByUuids(uuidsToRefresh); + } + private static PortfolioMapper mapper(DbSession session) { return session.getMapper(PortfolioMapper.class); } - } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java index 34c9f699659..49cc54ba540 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java @@ -59,7 +59,7 @@ public interface PortfolioMapper { List selectAllReferencesToApplications(); - Set selectAllProjectsInHierarchy(String rootUuid); + List selectAllProjectsInHierarchy(String rootUuid); List selectByUuids(@Param("uuids") Collection uuids); @@ -91,4 +91,5 @@ public interface PortfolioMapper { List selectAllReferencesOfPortfolios(); + List selectAllReferencesInHierarchy(String rootUuid); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml index 089e4bad195..8176cd6719a 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml @@ -235,6 +235,29 @@ INNER JOIN projects target ON pr.reference_uuid = target.uuid where target.qualifier = 'APP' + + DELETE FROM portfolios WHERE uuid in #{uuid,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java index fb40b95eb77..7bbfe7b3a1c 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java @@ -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(); } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index ed3f1498fd9..fe8b4374f26 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -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)); } -- 2.39.5