From 37587a03da2ea2289e2c2c6b0ca213e8274fe5c7 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 14 Oct 2020 16:43:30 -0500 Subject: [PATCH] SONAR-14033 Refactor storage of applications --- build.gradle | 2 + .../java/org/sonar/ce/queue/CeTaskSubmit.java | 14 + .../java/org/sonar/db/version/SqTables.java | 2 + .../src/main/java/org/sonar/db/DaoModule.java | 2 + .../src/main/java/org/sonar/db/DbClient.java | 7 + .../src/main/java/org/sonar/db/MyBatis.java | 2 + .../db/component/ApplicationProjectsDao.java | 108 +++ .../component/ApplicationProjectsMapper.java | 69 ++ .../org/sonar/db/component/BranchDao.java | 13 +- .../org/sonar/db/component/BranchMapper.java | 1 + .../org/sonar/db/component/SelectionMode.java | 27 + .../org/sonar/db/component/SnapshotDao.java | 5 +- .../java/org/sonar/db/project/ProjectDao.java | 12 + .../java/org/sonar/db/project/ProjectDto.java | 1 + .../org/sonar/db/project/ProjectMapper.java | 4 + .../org/sonar/db/purge/PurgeCommands.java | 17 +- .../java/org/sonar/db/purge/PurgeDao.java | 2 + .../java/org/sonar/db/purge/PurgeMapper.java | 8 + .../component/ApplicationProjectsMapper.xml | 196 +++++ .../org/sonar/db/component/BranchMapper.xml | 7 + .../org/sonar/db/project/ProjectMapper.xml | 22 +- .../org/sonar/db/purge/PurgeMapper.xml | 27 + server/sonar-db-dao/src/schema/schema-sq.ddl | 26 + .../component/ApplicationProjectsDaoTest.java | 157 ++++ .../ProjectMeasuresIndexerIteratorTest.java | 4 +- .../java/org/sonar/db/purge/PurgeDaoTest.java | 78 ++ .../sonar/db/component/ComponentDbTester.java | 61 +- .../sonar/db/component/ComponentTesting.java | 2 +- server/sonar-db-migration/build.gradle | 2 + .../v86/AddIndexToApplicationBranchProjs.java | 68 ++ .../v86/AddIndexToApplicationProjects.java | 56 ++ .../v86/AddPkToApplicationBranchProjs.java | 38 + .../v86/AddPkToApplicationProjects.java | 38 + .../v86/CreateApplicationBranchProjs.java | 48 ++ .../v86/CreateApplicationProjectsTable.java | 45 ++ .../db/migration/version/v86/DbVersion86.java | 10 + ...rateApplicationDefinitionsFromXmlToDb.java | 732 ++++++++++++++++++ .../src/main/resources/static/views.xsd | 85 ++ .../AddIndexToApplicationBranchProjsTest.java | 44 ++ .../AddIndexToApplicationProjectsTest.java | 42 + .../AddPkToApplicationBranchProjsTest.java | 40 + .../v86/AddPkToApplicationProjectsTest.java | 40 + .../CreateApplicationProjectsTableTest.java | 43 + ...ApplicationDefinitionsFromXmlToDbTest.java | 717 +++++++++++++++++ .../schema.sql | 9 + .../schema.sql | 7 + .../schema.sql | 8 + .../AddPkToApplicationProjectsTest/schema.sql | 6 + .../schema.sql | 66 ++ .../org/sonar/server/project/Visibility.java | 2 +- .../sonar/server/tester/UserSessionRule.java | 7 +- .../component/ComponentCleanerService.java | 7 +- .../server/component/ComponentUpdater.java | 9 +- .../sonar/server/component/NewComponent.java | 15 + .../server/ce/queue/ReportSubmitterTest.java | 3 +- .../ComponentCleanerServiceTest.java | 69 +- .../component/ComponentUpdaterTest.java | 3 +- .../server/project/ws/CreateActionTest.java | 3 +- 58 files changed, 3090 insertions(+), 48 deletions(-) create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsDao.java create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsMapper.java create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/component/SelectionMode.java create mode 100644 server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml create mode 100644 server/sonar-db-dao/src/test/java/org/sonar/db/component/ApplicationProjectsDaoTest.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/AddIndexToApplicationBranchProjs.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/AddIndexToApplicationProjects.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/AddPkToApplicationBranchProjs.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/AddPkToApplicationProjects.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/CreateApplicationBranchProjs.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/CreateApplicationProjectsTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDb.java create mode 100644 server/sonar-db-migration/src/main/resources/static/views.xsd create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/AddIndexToApplicationBranchProjsTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/AddIndexToApplicationProjectsTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/AddPkToApplicationBranchProjsTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/AddPkToApplicationProjectsTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/CreateApplicationProjectsTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/AddIndexToApplicationBranchProjsTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/AddIndexToApplicationProjectsTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/AddPkToApplicationBranchProjsTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/AddPkToApplicationProjectsTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest/schema.sql diff --git a/build.gradle b/build.gradle index 767692f9a90..b1c5a7ed5aa 100644 --- a/build.gradle +++ b/build.gradle @@ -295,6 +295,8 @@ subprojects { dependency 'javax.xml.bind:jaxb-api:2.3.0' dependency 'junit:junit:4.13' dependency 'org.junit.jupiter:junit-jupiter-api:5.6.0' + dependency 'org.xmlunit:xmlunit-core:2.6.4' + dependency 'org.xmlunit:xmlunit-matchers:2.6.4' dependency 'net.jpountz.lz4:lz4:1.3.0' dependency 'net.lightbody.bmp:littleproxy:1.1.0-beta-bmp-17' dependency 'org.awaitility:awaitility:4.0.2' diff --git a/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeTaskSubmit.java b/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeTaskSubmit.java index 71e7b38bf5f..9d7db56caf3 100644 --- a/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeTaskSubmit.java +++ b/server/sonar-ce-common/src/main/java/org/sonar/ce/queue/CeTaskSubmit.java @@ -25,7 +25,9 @@ import java.util.Optional; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Strings.emptyToNull; @@ -128,6 +130,18 @@ public final class CeTaskSubmit { return new Component(uuid, firstNonNull(dto.getMainBranchProjectUuid(), uuid)); } + public static Component fromDto(ProjectDto dto) { + return new Component(dto.getUuid(), dto.getUuid()); + } + + public static Component fromDto(BranchDto dto) { + return new Component(dto.getUuid(), dto.getProjectUuid()); + } + + public static Component fromProjectUuid(String projectUuid) { + return new Component(projectUuid, projectUuid); + } + public String getUuid() { return uuid; } diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java index 4c705ec451d..3cb3fff44c5 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java @@ -39,6 +39,8 @@ public final class SqTables { "alm_settings", "alm_pats", "analysis_properties", + "app_branch_project_branch", + "app_projects", "ce_activity", "ce_queue", "ce_task_characteristics", diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index 205d02d18e2..a76ef07ffc6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -35,6 +35,7 @@ import org.sonar.db.ce.CeTaskCharacteristicDao; import org.sonar.db.ce.CeTaskInputDao; import org.sonar.db.ce.CeTaskMessageDao; import org.sonar.db.component.AnalysisPropertiesDao; +import org.sonar.db.component.ApplicationProjectsDao; import org.sonar.db.component.BranchDao; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentKeyUpdaterDao; @@ -101,6 +102,7 @@ public class DaoModule extends Module { ActiveRuleDao.class, AnalysisPropertiesDao.class, AuthorizationDao.class, + ApplicationProjectsDao.class, BranchDao.class, CeActivityDao.class, CeQueueDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index 78d717373ed..bd6c3c878d6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -33,6 +33,7 @@ import org.sonar.db.ce.CeTaskCharacteristicDao; import org.sonar.db.ce.CeTaskInputDao; import org.sonar.db.ce.CeTaskMessageDao; import org.sonar.db.component.AnalysisPropertiesDao; +import org.sonar.db.component.ApplicationProjectsDao; import org.sonar.db.component.BranchDao; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentKeyUpdaterDao; @@ -166,6 +167,7 @@ public class DbClient { private final SessionTokensDao sessionTokensDao; private final SamlMessageIdDao samlMessageIdDao; private final UserDismissedMessagesDao userDismissedMessagesDao; + private final ApplicationProjectsDao applicationProjectsDao; public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { this.database = database; @@ -245,6 +247,7 @@ public class DbClient { sessionTokensDao = getDao(map, SessionTokensDao.class); samlMessageIdDao = getDao(map, SamlMessageIdDao.class); userDismissedMessagesDao = getDao(map, UserDismissedMessagesDao.class); + applicationProjectsDao = getDao(map, ApplicationProjectsDao.class); } public DbSession openSession(boolean batch) { @@ -267,6 +270,10 @@ public class DbClient { return almPatDao; } + public ApplicationProjectsDao applicationProjectsDao() { + return applicationProjectsDao; + } + public ProjectAlmSettingDao projectAlmSettingDao() { return projectAlmSettingDao; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 8c1f4a3b487..77205277bb5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -47,6 +47,7 @@ import org.sonar.db.ce.CeTaskCharacteristicMapper; import org.sonar.db.ce.CeTaskInputMapper; import org.sonar.db.ce.CeTaskMessageMapper; import org.sonar.db.component.AnalysisPropertiesMapper; +import org.sonar.db.component.ApplicationProjectsMapper; import org.sonar.db.component.BranchMapper; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDtoWithSnapshotId; @@ -231,6 +232,7 @@ public class MyBatis implements Startable { AlmPatMapper.class, AlmSettingMapper.class, AnalysisPropertiesMapper.class, + ApplicationProjectsMapper.class, AuthorizationMapper.class, BranchMapper.class, CeActivityMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsDao.java new file mode 100644 index 00000000000..713d67dd5dd --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsDao.java @@ -0,0 +1,108 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.component; + +import java.util.Collection; +import java.util.Set; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.project.ProjectDto; + +public class ApplicationProjectsDao implements Dao { + private final System2 system2; + private final UuidFactory uuidFactory; + + public ApplicationProjectsDao(System2 system2, UuidFactory uuidFactory) { + this.system2 = system2; + this.uuidFactory = uuidFactory; + } + + public void addProject(DbSession dbSession, String applicationUuid, String projectUuid) { + getMapper(dbSession).addProject(uuidFactory.create(), applicationUuid, projectUuid, system2.now()); + } + + public void removeApplicationProjectsByApplicationAndProject(DbSession dbSession, String applicationUuid, String projectUuid) { + getMapper(dbSession).removeApplicationBranchProjectBranchesByApplicationAndProject(applicationUuid, projectUuid); + getMapper(dbSession).removeApplicationProjectsByApplicationAndProject(applicationUuid, projectUuid); + } + + public int countApplicationProjects(DbSession dbSession, String applicationUuid) { + return getMapper(dbSession).countApplicationProjects(applicationUuid); + } + + public Set selectProjects(DbSession dbSession, String applicationUuid) { + return getMapper(dbSession).selectProjects(applicationUuid); + } + + public void remove(DbSession dbSession, String applicationUuid) { + getMapper(dbSession).removeApplicationBranchProjectBranchesByApplication(applicationUuid); + getMapper(dbSession).removeApplicationProjectsByApplication(applicationUuid); + } + + public void addProjectBranchToAppBranch(DbSession dbSession, BranchDto applicationBranch, BranchDto projectBranch) { + getMapper(dbSession).addProjectBranchToAppBranch( + uuidFactory.create(), + applicationBranch.getProjectUuid(), + applicationBranch.getUuid(), + projectBranch.getProjectUuid(), + projectBranch.getUuid(), + system2.now()); + } + + public void addProjectBranchToAppBranch(DbSession dbSession, String applicationUuid, String applicationBranchUuid, String projectUuid, String projectBranchUuid) { + getMapper(dbSession).addProjectBranchToAppBranch( + uuidFactory.create(), + applicationUuid, + applicationBranchUuid, + projectUuid, + projectBranchUuid, + system2.now()); + } + + public void removeProjectBranchFromAppBranch(DbSession dbSession, String applicationBranchUuid, String projectBranchUuid) { + getMapper(dbSession).removeProjectBranchFromAppBranch(applicationBranchUuid, projectBranchUuid); + } + + public Set selectProjectBranchesFromAppBranch(DbSession dbSession, String applicationBranchUuid) { + return getMapper(dbSession).selectProjectBranchesFromAppBranch(applicationBranchUuid); + } + + public Set selectApplicationsFromProjectBranch(DbSession dbSession, String projectUuid, String branchKey) { + return getMapper(dbSession).selectApplicationsFromProjectBranch(projectUuid, branchKey); + } + + public Set selectApplicationsFromProjects(DbSession dbSession, Collection projectUuids) { + return getMapper(dbSession).selectApplicationsFromProjects(projectUuids); + } + + private static ApplicationProjectsMapper getMapper(DbSession session) { + return session.getMapper(ApplicationProjectsMapper.class); + } + + public void updateApplicationBranchName(DbSession dbSession, String applicationBranchUuid, String newName) { + getMapper(dbSession).updateApplicationBranchName(applicationBranchUuid, newName); + } + + public void removeAllProjectBranchesOfAppBranch(DbSession dbSession, String applicationBranchUuid) { + getMapper(dbSession).removeAllProjectBranchesOfAppBranch(applicationBranchUuid); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsMapper.java new file mode 100644 index 00000000000..a45cb6529bc --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ApplicationProjectsMapper.java @@ -0,0 +1,69 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.component; + +import java.util.Collection; +import java.util.Set; +import org.apache.ibatis.annotations.Param; +import org.sonar.db.project.ProjectDto; + +public interface ApplicationProjectsMapper { + void addProject( + @Param("uuid") String uuid, + @Param("applicationUuid") String applicationUuid, + @Param("projectUuid") String projectUuid, + @Param("now") long now); + + void removeApplicationBranchProjectBranchesByApplicationAndProject( + @Param("applicationUuid") String applicationUuid, + @Param("projectUuid") String projectUuid); + + void removeApplicationProjectsByApplicationAndProject( + @Param("applicationUuid") String applicationUuid, + @Param("projectUuid") String projectUuid); + + Set selectProjects(@Param("applicationUuid") String applicationUuid); + + void removeApplicationProjectsByApplication(String applicationUuid); + + void removeApplicationBranchProjectBranchesByApplication(String applicationUuid); + + void addProjectBranchToAppBranch( + @Param("uuid") String uuid, + @Param("applicationUuid") String applicationUuid, + @Param("applicationBranchUuid") String applicationBranchUuid, + @Param("projectUuid") String projectUuid, + @Param("projectBranchUuid") String projectBranchUuid, + @Param("now") long now); + + void removeProjectBranchFromAppBranch(@Param("applicationBranchUuid") String applicationBranchUuid, @Param("projectBranchUuid") String projectBranchUuid); + + Set selectProjectBranchesFromAppBranch(@Param("applicationBranchUuid") String applicationBranchUuid); + + int countApplicationProjects(@Param("applicationUuid") String applicationUuid); + + void updateApplicationBranchName(@Param("uuid") String uuid, @Param("newName") String newName); + + Set selectApplicationsFromProjectBranch(@Param("projectUuid") String projectUuid, @Param("branchKey") String branchKey); + + Set selectApplicationsFromProjects(@Param("projectUuids") Collection projectUuids); + + void removeAllProjectBranchesOfAppBranch(@Param("applicationBranchUuid") String applicationBranchUuid); +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index eea97147e49..3abeac13c01 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -20,7 +20,6 @@ package org.sonar.db.component; import java.util.Collection; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -30,6 +29,7 @@ import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.project.ProjectDto; +import static java.util.Collections.emptyList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class BranchDao implements Dao { @@ -68,7 +68,7 @@ public class BranchDao implements Dao { public List selectByBranchKeys(DbSession dbSession, Map branchKeyByProjectUuid) { if (branchKeyByProjectUuid.isEmpty()) { - return Collections.emptyList(); + return emptyList(); } return mapper(dbSession).selectByBranchKeys(branchKeyByProjectUuid); } @@ -94,6 +94,9 @@ public class BranchDao implements Dao { } public List selectByUuids(DbSession session, Collection uuids) { + if (uuids.isEmpty()) { + return emptyList(); + } return executeLargeInputs(uuids, mapper(session)::selectByUuids); } @@ -103,7 +106,7 @@ public class BranchDao implements Dao { public List selectProjectUuidsWithIssuesNeedSync(DbSession session, Collection uuids) { if (uuids.isEmpty()) { - return Collections.emptyList(); + return emptyList(); } return executeLargeInputs(uuids, mapper(session)::selectProjectUuidsWithIssuesNeedSync); @@ -146,6 +149,10 @@ public class BranchDao implements Dao { return mapper(dbSession).updateNeedIssueSync(branchUuid, needIssueSync, now); } + public void deleteBranch(DbSession dbSession, String projectUuid, String branchKey) { + mapper(dbSession).deleteBranch(projectUuid, branchKey); + } + public boolean doAnyOfComponentsNeedIssueSync(DbSession session, List components) { if (!components.isEmpty()) { List result = new LinkedList<>(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java index c44507015fd..db9667426f7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java @@ -68,4 +68,5 @@ public interface BranchMapper { short doAnyOfComponentsNeedIssueSync(@Param("componentKeys") List components); + void deleteBranch(@Param("projectUuid") String projectUuid, @Param("branchKey") String branchKey); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SelectionMode.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SelectionMode.java new file mode 100644 index 00000000000..6a636de8542 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SelectionMode.java @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.component; + +public enum SelectionMode { + MANUAL_MEASURE, + REGEXP, + TAGS, + REMAINING_PROJECTS +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java index f70760d6541..0f62ef98e7d 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -34,7 +34,6 @@ import org.sonar.db.Dao; import org.sonar.db.DbSession; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.collect.FluentIterable.from; import static java.util.Objects.requireNonNull; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -125,9 +124,7 @@ public class SnapshotDao implements Dao { */ @CheckForNull public ViewsSnapshotDto selectSnapshotBefore(String componentUuid, long date, DbSession dbSession) { - return from(mapper(dbSession).selectSnapshotBefore(componentUuid, date)) - .first() - .orNull(); + return mapper(dbSession).selectSnapshotBefore(componentUuid, date).stream().findFirst().orElse(null); } private static SnapshotMapper mapper(DbSession session) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java index 46dd8218e9a..8d2509cf449 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java @@ -50,6 +50,10 @@ public class ProjectDao implements Dao { return Optional.ofNullable(mapper(session).selectProjectOrAppByKey(key)); } + public List selectAllApplications(DbSession session){ + return mapper(session).selectAllApplications(); + } + public List selectProjectsByKeys(DbSession session, Set keys) { if (keys.isEmpty()) { return Collections.emptyList(); @@ -57,6 +61,13 @@ public class ProjectDao implements Dao { return mapper(session).selectProjectsByKeys(keys); } + public List selectApplicationsByKeys(DbSession session, Set keys) { + if (keys.isEmpty()) { + return Collections.emptyList(); + } + return mapper(session).selectApplicationsByKeys(keys); + } + public List selectProjects(DbSession session) { return mapper(session).selectProjects(); } @@ -99,4 +110,5 @@ public class ProjectDao implements Dao { private static ProjectMapper mapper(DbSession session) { return session.getMapper(ProjectMapper.class); } + } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java index e01902c2eb5..656fe8e67ae 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java @@ -181,4 +181,5 @@ public class ProjectDto { public int hashCode() { return uuid != null ? uuid.hashCode() : 0; } + } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java index 01ff1c11e97..c2fafe6a8f3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java @@ -57,4 +57,8 @@ public interface ProjectMapper { List selectProjectsByOrganizationUuid(String organizationUuid); void updateVisibility(@Param("uuid") String uuid, @Param("isPrivate") boolean isPrivate, @Param("updatedAt") long updatedAt); + + List selectAllApplications(); + + List selectApplicationsByKeys(@Param("kees") Collection kees); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java index b11c7febd7f..6ab82d82e1b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -49,7 +49,6 @@ class PurgeCommands { this.system2 = system2; } - @VisibleForTesting PurgeCommands(DbSession session, PurgeProfiler profiler, System2 system2) { this(session, session.getMapper(PurgeMapper.class), profiler, system2); } @@ -406,6 +405,21 @@ class PurgeCommands { profiler.stop(); } + void deleteApplicationProjects(String applicationUuid) { + profiler.start("deleteApplicationProjects (app_projects)"); + purgeMapper.deleteApplicationBranchProjectBranchesByApplicationUuid(applicationUuid); + purgeMapper.deleteApplicationProjectsByApplicationUuid(applicationUuid); + session.commit(); + profiler.stop(); + } + + void deleteApplicationBranchProjects(String applicationBranchUuid) { + profiler.start("deleteApplicationBranchProjects (app_branch_project_branch)"); + purgeMapper.deleteApplicationBranchProjects(applicationBranchUuid); + session.commit(); + profiler.stop(); + } + public void deleteProjectAlmSettings(String rootUuid) { profiler.start("deleteProjectAlmSettings (project_alm_settings)"); purgeMapper.deleteProjectAlmSettingsByProjectUuid(rootUuid); @@ -415,6 +429,7 @@ class PurgeCommands { void deleteBranch(String rootUuid) { profiler.start("deleteBranch (project_branches)"); + purgeMapper.deleteApplicationBranchProjectBranchesByProjectBranchUuid(rootUuid); purgeMapper.deleteBranchByUuid(rootUuid); session.commit(); profiler.stop(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java index bb39d91a389..3498ad1f49d 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -221,6 +221,8 @@ public class PurgeDao implements Dao { commands.deletePermissions(rootUuid); commands.deleteNewCodePeriods(rootUuid); commands.deleteBranch(rootUuid); + commands.deleteApplicationBranchProjects(rootUuid); + commands.deleteApplicationProjects(rootUuid); commands.deleteComponents(rootUuid); commands.deleteComponentsByMainBranchProjectUuid(rootUuid); commands.deleteProject(rootUuid); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java index 840533516f9..57ca6f26a3e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -141,6 +141,14 @@ public interface PurgeMapper { void deleteProjectMappingsByProjectUuid(@Param("projectUuid") String projectUuid); + void deleteApplicationProjectsByApplicationUuid(@Param("applicationUuid") String applicationUuid); + + void deleteApplicationBranchProjectBranchesByApplicationUuid(@Param("applicationUuid") String applicationUuid); + + void deleteApplicationBranchProjects(@Param("branchUuid") String applicationBranchUuid); + + void deleteApplicationBranchProjectBranchesByProjectBranchUuid(@Param("projectBranchUuid") String projectBranchUuid); + void deleteBranchByUuid(@Param("uuid") String uuid); void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml new file mode 100644 index 00000000000..9b7533340f0 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ApplicationProjectsMapper.xml @@ -0,0 +1,196 @@ + + + + + + app.uuid as uuid + + + + pb.uuid as uuid, + pb.project_uuid as projectUuid, + pb.kee as kee, + pb.branch_type as branchType, + pb.merge_branch_uuid as mergeBranchUuid, + pb.pull_request_binary as pullRequestBinary, + pb.exclude_from_purge as excludeFromPurge, + pb.need_issue_sync as needIssueSync + + + + p.uuid as uuid, + p.organization_uuid as organizationUuid, + p.kee as kee, + p.qualifier as qualifier, + p.name as name, + p.description as description, + p.tags as tagsString, + p.private as isPrivate, + p.created_at as createdAt, + p.updated_at as updatedAt + + + + + + INSERT INTO app_projects ( + uuid, + application_uuid, + project_uuid, + created_at + ) + VALUES ( + #{uuid,jdbcType=VARCHAR}, + #{applicationUuid,jdbcType=VARCHAR}, + #{projectUuid,jdbcType=VARCHAR}, + #{now,jdbcType=BIGINT} + ) + + + + DELETE FROM + app_branch_project_branch + WHERE + app_branch_project_branch.application_uuid = #{applicationUuid,jdbcType=VARCHAR} + AND + app_branch_project_branch.project_uuid=#{projectUuid,jdbcType=VARCHAR} + + + + DELETE FROM + app_projects + WHERE + application_uuid=#{applicationUuid,jdbcType=VARCHAR} + AND + project_uuid=#{projectUuid,jdbcType=VARCHAR} + + + + DELETE FROM + app_branch_project_branch + where + application_uuid=#{applicationUuid,jdbcType=VARCHAR} + + + + DELETE FROM + app_projects + WHERE + application_uuid=#{applicationUuid,jdbcType=VARCHAR} + + + + INSERT INTO app_branch_project_branch ( + uuid, + application_uuid, + application_branch_uuid, + project_uuid, + project_branch_uuid, + created_at + ) + VALUES ( + #{uuid,jdbcType=VARCHAR}, + #{applicationUuid,jdbcType=VARCHAR}, + #{applicationBranchUuid,jdbcType=VARCHAR}, + #{projectUuid,jdbcType=VARCHAR}, + #{projectBranchUuid,jdbcType=VARCHAR}, + #{now,jdbcType=BIGINT} + ) + + + + DELETE FROM + app_branch_project_branch + WHERE + app_branch_project_branch.application_branch_uuid = #{applicationBranchUuid,jdbcType=VARCHAR} + AND + app_branch_project_branch.project_branch_uuid = #{projectBranchUuid,jdbcType=VARCHAR} + + + + + + + + UPDATE + project_branches + SET + kee = #{newName,jdbcType=VARCHAR} + WHERE + uuid = #{uuid,jdbcType=VARCHAR} + + + + + + + + DELETE FROM + app_branch_project_branch + WHERE + application_branch_uuid=#{applicationBranchUuid,jdbcType=VARCHAR} + + + diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml index dccb832be36..8cdc4b70ddc 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml @@ -48,6 +48,13 @@ uuid = #{projectUuid, jdbcType=VARCHAR} + + delete from project_branches + where + project_uuid = #{projectUuid, jdbcType=VARCHAR}, + kee = #{branchKey, jdbcType=VARCHAR} + + update project_branches set diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml index d3903f6567a..dfc9c9d51fc 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml @@ -46,6 +46,18 @@ + + - select from projects p @@ -71,6 +83,14 @@ p.organization_uuid=#{organizationUuid,jdbcType=VARCHAR} + +