From 50b12df3787d87958fcef7fbf8c00598b141f1ee Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 30 Sep 2022 12:23:26 -0500 Subject: SONAR-17352 Refactor component keys to not include branch suffix --- .../java/org/sonar/db/component/ComponentDao.java | 244 ++++++++++----------- .../java/org/sonar/db/component/ComponentDto.java | 2 - .../sonar/db/component/ComponentKeyUpdaterDao.java | 34 +-- .../db/component/ComponentKeyUpdaterMapper.java | 4 +- .../org/sonar/db/component/ComponentMapper.java | 21 +- .../java/org/sonar/db/property/PropertiesDao.java | 13 +- .../java/org/sonar/db/purge/PurgeCommands.java | 2 +- .../main/java/org/sonar/db/purge/PurgeMapper.java | 2 +- .../db/component/ComponentKeyUpdaterMapper.xml | 4 +- .../org/sonar/db/component/ComponentMapper.xml | 108 ++++++--- .../resources/org/sonar/db/purge/PurgeMapper.xml | 2 +- .../org/sonar/db/component/ComponentDaoTest.java | 160 +++++++++++--- .../db/component/ComponentKeyUpdaterDaoTest.java | 59 +---- .../org/sonar/db/property/PropertiesDaoTest.java | 39 +--- .../org/sonar/db/component/ComponentDbTester.java | 5 + .../org/sonar/db/property/PropertyDbTester.java | 28 +-- 16 files changed, 373 insertions(+), 354 deletions(-) (limited to 'server/sonar-db-dao') diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 56f6bae2fd1..687d4e574aa 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -23,17 +23,14 @@ import com.google.common.collect.Ordering; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.sonar.api.resources.Qualifiers; @@ -45,6 +42,7 @@ import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.ComponentNewValue; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; import static java.util.Collections.emptyList; import static org.sonar.db.DatabaseUtils.checkThatNotTooManyConditions; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -58,26 +56,9 @@ public class ComponentDao implements Dao { this.auditPersister = auditPersister; } - private static List selectByQueryImpl(DbSession session, ComponentQuery query, int offset, int limit) { - if (query.hasEmptySetOfComponents()) { - return emptyList(); - } - checkThatNotTooManyComponents(query); - return mapper(session).selectByQuery(query, new RowBounds(offset, limit)); - } - - private static int countByQueryImpl(DbSession session, ComponentQuery query) { - if (query.hasEmptySetOfComponents()) { - return 0; - } - checkThatNotTooManyComponents(query); - return mapper(session).countByQuery(query); - } - - private static ComponentMapper mapper(DbSession session) { - return session.getMapper(ComponentMapper.class); - } - + /* + * SELECT BY UUID + */ public Optional selectByUuid(DbSession session, String uuid) { return Optional.ofNullable(mapper(session).selectByUuid(uuid)); } @@ -86,22 +67,12 @@ public class ComponentDao implements Dao { return selectByUuid(session, uuid).orElseThrow(() -> new RowNotFoundException(String.format("Component with uuid '%s' not found", uuid))); } - /** - * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values - * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values - * @throws IllegalArgumentException if parameter query#getMainComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values - */ - public List selectByQuery(DbSession dbSession, ComponentQuery query, int offset, int limit) { - return selectByQueryImpl(dbSession, query, offset, limit); + public List selectByUuids(DbSession session, Collection uuids) { + return executeLargeInputs(uuids, mapper(session)::selectByUuids); } - /** - * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values - * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values - * @throws IllegalArgumentException if parameter query#getMainComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values - */ - public int countByQuery(DbSession session, ComponentQuery query) { - return countByQueryImpl(session, query); + public List selectExistingUuids(DbSession session, Collection uuids) { + return executeLargeInputs(uuids, mapper(session)::selectExistingUuids); } public List selectSubProjectsByComponentUuids(DbSession session, Collection uuids) { @@ -127,81 +98,100 @@ public class ComponentDao implements Dao { return mapper(session).selectEnabledFilesFromProject(rootComponentUuid); } - public List selectByUuids(DbSession session, Collection uuids) { - return executeLargeInputs(uuids, mapper(session)::selectByUuids); + /** + * Retrieves all components with a specific branch UUID, no other filtering is done by this method. + */ + public List selectByBranchUuid(String branchUuid, DbSession dbSession) { + return mapper(dbSession).selectByBranchUuid(branchUuid); } - public List selectExistingUuids(DbSession session, Collection uuids) { - return executeLargeInputs(uuids, mapper(session)::selectExistingUuids); + public int countEnabledModulesByBranchUuid(DbSession session, String branchUuid) { + return mapper(session).countEnabledModulesByBranchUuid(branchUuid); } + /* + SELECT BY QUERY + */ + /** - * Return all components of a project (including disable ones) + * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values + * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values + * @throws IllegalArgumentException if parameter query#getMainComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values */ - public List selectAllComponentsFromProjectKey(DbSession session, String projectKey) { - return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, null, false); + public List selectByQuery(DbSession dbSession, ComponentQuery query, int offset, int limit) { + return selectByQueryImpl(dbSession, query, offset, limit); } - public List selectUuidsByKeyFromProjectKey(DbSession session, String projectKey) { - return mapper(session).selectUuidsByKeyFromProjectKey(projectKey); + /** + * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values + * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values + * @throws IllegalArgumentException if parameter query#getMainComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values + */ + public int countByQuery(DbSession session, ComponentQuery query) { + return countByQueryImpl(session, query); } - public List selectProjectAndModulesFromProjectKey(DbSession session, String projectKey, boolean excludeDisabled) { - return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, Scopes.PROJECT, excludeDisabled); + private static List selectByQueryImpl(DbSession session, ComponentQuery query, int offset, int limit) { + if (query.hasEmptySetOfComponents()) { + return emptyList(); + } + checkThatNotTooManyComponents(query); + return mapper(session).selectByQuery(query, new RowBounds(offset, limit)); } - public int countEnabledModulesByBranchUuid(DbSession session, String branchUuid) { - return mapper(session).countEnabledModulesByBranchUuid(branchUuid); + private static int countByQueryImpl(DbSession session, ComponentQuery query) { + if (query.hasEmptySetOfComponents()) { + return 0; + } + checkThatNotTooManyComponents(query); + return mapper(session).countByQuery(query); } - public List selectEnabledModulesFromProjectKey(DbSession session, String projectKey) { - return selectProjectAndModulesFromProjectKey(session, projectKey, true); - } + /* + SELECT BY KEY + */ - public List selectByKeys(DbSession session, Collection keys) { - return executeLargeInputs(keys, mapper(session)::selectByKeys); + /** + * Return all components of a project (including disable ones) + */ + public List selectUuidsByKeyFromProjectKey(DbSession session, String projectKey) { + return mapper(session).selectUuidsByKeyFromProjectKeyAndBranchOrPr(projectKey, null, null); } - public List selectByKeysAndBranch(DbSession session, Collection keys, String branch) { - return executeLargeInputs(keys, subKeys -> mapper(session).selectByKeysAndBranch(subKeys, branch)); + public List selectUuidsByKeyFromProjectKeyAndBranch(DbSession session, String projectKey, String branch) { + return mapper(session).selectUuidsByKeyFromProjectKeyAndBranchOrPr(projectKey, branch, null); } - public List selectByKeysAndPullRequest(DbSession session, Collection keys, String pullRequestId) { - return executeLargeInputs(keys, subKeys -> mapper(session).selectByKeysAndBranch(subKeys, pullRequestId)); + public List selectUuidsByKeyFromProjectKeyAndPullRequest(DbSession session, String projectKey, String pullrequest) { + return mapper(session).selectUuidsByKeyFromProjectKeyAndBranchOrPr(projectKey, null, pullrequest); } - public List selectByDbKeys(DbSession session, Collection dbKeys) { - Map> keyByBranchKey = new HashMap<>(); - Map> keyByPrKey = new HashMap<>(); - List mainBranchKeys = new LinkedList<>(); - - for (String dbKey : dbKeys) { - String branchKey = StringUtils.substringAfterLast(dbKey, ComponentDto.BRANCH_KEY_SEPARATOR); - if (!StringUtils.isEmpty(branchKey)) { - keyByBranchKey.computeIfAbsent(branchKey, b -> new LinkedList<>()) - .add(StringUtils.substringBeforeLast(dbKey, ComponentDto.BRANCH_KEY_SEPARATOR)); - continue; - } + /** + * If no branch or pull request is provided, returns components in the main branch + */ + public List selectProjectAndModulesFromProjectKey(DbSession session, String projectKey, boolean excludeDisabled, + @Nullable String branch, @Nullable String pullRequest) { + checkState(branch == null || pullRequest == null, "Can't set both branch and pull request"); + return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, Scopes.PROJECT, excludeDisabled, branch, pullRequest); + } - String prKey = StringUtils.substringAfterLast(dbKey, ComponentDto.PULL_REQUEST_SEPARATOR); - if (!StringUtils.isEmpty(prKey)) { - keyByPrKey.computeIfAbsent(prKey, b -> new LinkedList<>()) - .add(StringUtils.substringBeforeLast(dbKey, ComponentDto.PULL_REQUEST_SEPARATOR)); - continue; - } + /** + * If no branch or pull request is provided, returns components in the main branch + */ + public List selectEnabledModulesFromProjectKey(DbSession session, String projectKey, @Nullable String branch, @Nullable String pullRequest) { + return selectProjectAndModulesFromProjectKey(session, projectKey, true, branch, pullRequest); + } - mainBranchKeys.add(dbKey); - } + public List selectByKeys(DbSession session, Collection keys) { + return selectByKeys(session, keys, null, null); + } - List components = new LinkedList<>(); - for (Map.Entry> e : keyByBranchKey.entrySet()) { - components.addAll(selectByKeysAndBranch(session, e.getValue(), e.getKey())); - } - for (Map.Entry> e : keyByPrKey.entrySet()) { - components.addAll(selectByKeysAndPullRequest(session, e.getValue(), e.getKey())); - } - components.addAll(selectByKeys(session, mainBranchKeys)); - return components; + /** + * If no branch or pull request is provided, returns components in the main branch + */ + public List selectByKeys(DbSession session, Collection keys, @Nullable String branch, @Nullable String pullRequest) { + checkState(branch == null || pullRequest == null, "Can't set both branch and pull request"); + return executeLargeInputs(keys, subKeys -> mapper(session).selectByKeysAndBranchOrPr(subKeys, branch, pullRequest)); } /** @@ -258,10 +248,25 @@ public class ComponentDao implements Dao { return Optional.ofNullable(mapper(session).selectByKeyAndPrKey(key, pullRequestId)); } + /* + SELECT ALL + */ public List selectAllViewsAndSubViews(DbSession session) { return mapper(session).selectUuidsForQualifiers(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.SUBVIEW); } + /** + * Returns all projects (Scope {@link Scopes#PROJECT} and qualifier + * {@link Qualifiers#PROJECT}) which are enabled. + *

+ * Branches are not returned. + *

+ * Used by Views. + */ + public List selectProjects(DbSession session) { + return mapper(session).selectProjects(); + } + /** * Used by Governance */ @@ -276,18 +281,6 @@ public class ComponentDao implements Dao { return mapper(session).selectProjectsFromView("%." + escapedViewUuid + ".%", projectViewUuid); } - /** - * Returns all projects (Scope {@link Scopes#PROJECT} and qualifier - * {@link Qualifiers#PROJECT}) which are enabled. - *

- * Branches are not returned. - *

- * Used by Views. - */ - public List selectProjects(DbSession session) { - return mapper(session).selectProjects(); - } - /** * Selects all components that are relevant for indexing. The result is not returned (since it is usually too big), but handed over to the handler * @@ -299,15 +292,6 @@ public class ComponentDao implements Dao { mapper(session).scrollForIndexing(projectUuid, handler); } - /** - * Retrieves all components with a specific branch UUID, no other filtering is done by this method. - *

- * Used by Views plugin - */ - public List selectByBranchUuid(String branchUuid, DbSession dbSession) { - return mapper(dbSession).selectByBranchUuid(branchUuid); - } - /** * Retrieve enabled components keys with given qualifiers *

@@ -349,6 +333,25 @@ public class ComponentDao implements Dao { mapper(session).scrollAllFilesForFileMove(branchUuid, handler); } + public List selectPrivateProjectsWithNcloc(DbSession dbSession) { + return mapper(dbSession).selectPrivateProjectsWithNcloc(); + } + + public boolean existAnyOfComponentsWithQualifiers(DbSession session, Collection componentKeys, Set qualifiers) { + if (!componentKeys.isEmpty()) { + List result = new LinkedList<>(); + return executeLargeInputs(componentKeys, input -> { + boolean groupNeedIssueSync = mapper(session).checkIfAnyOfComponentsWithQualifiers(input, qualifiers) > 0; + result.add(groupNeedIssueSync); + return result; + }).stream().anyMatch(b -> b); + } + return false; + } + + /* + INSERT / UPDATE + */ public void insert(DbSession session, ComponentDto item) { mapper(session).insert(item); if (!isBranchOrPullRequest(item)) { @@ -396,27 +399,18 @@ public class ComponentDao implements Dao { mapper(session).setPrivateForRootComponentUuid(branchUuid, isPrivate); } + /* + UTIL + */ + private static ComponentMapper mapper(DbSession session) { + return session.getMapper(ComponentMapper.class); + } + private static void checkThatNotTooManyComponents(ComponentQuery query) { checkThatNotTooManyConditions(query.getComponentKeys(), "Too many component keys in query"); checkThatNotTooManyConditions(query.getComponentUuids(), "Too many component UUIDs in query"); } - public List selectPrivateProjectsWithNcloc(DbSession dbSession) { - return mapper(dbSession).selectPrivateProjectsWithNcloc(); - } - - public boolean existAnyOfComponentsWithQualifiers(DbSession session, Collection componentKeys, Set qualifiers) { - if (!componentKeys.isEmpty()) { - List result = new LinkedList<>(); - return executeLargeInputs(componentKeys, input -> { - boolean groupNeedIssueSync = mapper(session).checkIfAnyOfComponentsWithQualifiers(input, qualifiers) > 0; - result.add(groupNeedIssueSync); - return result; - }).stream().anyMatch(b -> b); - } - return false; - } - private static boolean isBranchOrPullRequest(ComponentDto item) { return item.getMainBranchProjectUuid() != null; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java index d3303761465..ba308ad3ec2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java @@ -46,8 +46,6 @@ public class ComponentDto { public static final String BRANCH_KEY_SEPARATOR = ":BRANCH:"; public static final String PULL_REQUEST_SEPARATOR = ":PULL_REQUEST:"; - private static final Splitter BRANCH_KEY_SPLITTER = Splitter.on(BRANCH_KEY_SEPARATOR); - public static final String UUID_PATH_SEPARATOR = "."; public static final String UUID_PATH_OF_ROOT = UUID_PATH_SEPARATOR; private static final Splitter UUID_PATH_SPLITTER = Splitter.on(UUID_PATH_SEPARATOR).omitEmptyStrings(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java index ec86e389fe3..5e247061a92 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java @@ -32,9 +32,6 @@ import org.sonar.db.DbSession; import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.ComponentKeyNewValue; -import static org.sonar.db.component.ComponentDto.BRANCH_KEY_SEPARATOR; -import static org.sonar.db.component.ComponentDto.generateBranchKey; - /** * Class used to rename the key of a project and its resources. * @@ -52,17 +49,17 @@ public class ComponentKeyUpdaterDao implements Dao { checkExistentKey(mapper, newKey); // must SELECT first everything - ResourceDto project = mapper.selectProjectByUuid(projectUuid); + ResourceDto project = mapper.selectComponentByUuid(projectUuid); String projectOldKey = project.getKey(); - List resources = mapper.selectProjectResources(projectUuid); + List resources = mapper.selectBranchResources(projectUuid); resources.add(project); // add branch components dbSession.getMapper(BranchMapper.class).selectByProjectUuid(projectUuid).stream() .filter(branch -> !projectUuid.equals(branch.getUuid())) .forEach(branch -> { - resources.addAll(mapper.selectProjectResources(branch.getUuid())); - resources.add(mapper.selectProjectByUuid(branch.getUuid())); + resources.addAll(mapper.selectBranchResources(branch.getUuid())); + resources.add(mapper.selectComponentByUuid(branch.getUuid())); }); // and then proceed with the batch UPDATE at once @@ -70,29 +67,6 @@ public class ComponentKeyUpdaterDao implements Dao { }, dbSession); } - public void updateApplicationBranchKey(DbSession dbSession, String appBranchUuid, String appKey, String newBranchName) { - // TODO review - ComponentKeyUpdaterMapper mapper = dbSession.getMapper(ComponentKeyUpdaterMapper.class); - - String newAppBranchKey = generateBranchKey(appKey, newBranchName); - checkExistentKey(mapper, newAppBranchKey); - - ResourceDto appBranch = mapper.selectProjectByUuid(appBranchUuid); - String appBranchOldKey = appBranch.getKey(); - appBranch.setKey(newAppBranchKey); - mapper.updateComponent(appBranch); - - auditPersister.componentKeyBranchUpdate(dbSession, new ComponentKeyNewValue(appBranchUuid, appBranchOldKey, newAppBranchKey), Qualifiers.APP); - - String oldAppBranchFragment = appBranchOldKey.replace(BRANCH_KEY_SEPARATOR, ""); - String newAppBranchFragment = appKey + newBranchName; - for (ResourceDto appBranchResource : mapper.selectProjectResources(appBranchUuid)) { - String newKey = computeNewKey(appBranchResource.getKey(), oldAppBranchFragment, newAppBranchFragment); - appBranchResource.setKey(newKey); - mapper.updateComponent(appBranchResource); - } - } - @VisibleForTesting static String computeNewKey(String key, String stringToReplace, String replacementString) { return key.replace(stringToReplace, replacementString); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterMapper.java index 6a67af03dc2..f4c38fdf60c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentKeyUpdaterMapper.java @@ -26,9 +26,9 @@ public interface ComponentKeyUpdaterMapper { int countComponentsByKey(String key); - ResourceDto selectProjectByUuid(@Param("uuid") String uuid); + ResourceDto selectComponentByUuid(@Param("uuid") String uuid); - List selectProjectResources(@Param("rootUuid") String rootUuid); + List selectBranchResources(@Param("rootUuid") String rootUuid); void updateComponent(ResourceDto resource); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index 98297278093..f669ff574df 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -29,18 +29,11 @@ import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; public interface ComponentMapper { - - @CheckForNull - ComponentDto selectByKey(@Param("key") String key); - @CheckForNull ComponentDto selectByKeyCaseInsensitive(@Param("key") String key); @CheckForNull - ComponentDto selectByKeyAndBranchKey(@Param("key") String key, @Param("branch") String branch); - - @CheckForNull - ComponentDto selectByKeyAndPrKey(@Param("key") String key, @Param("pr") String pr); + ComponentDto selectByKeyAndBranchOrPr(@Param("key") String key, @Nullable @Param("branch") String branch, @Nullable @Param("pullRequest") String pullRequest); @CheckForNull ComponentDto selectByUuid(@Param("uuid") String uuid); @@ -50,9 +43,8 @@ public interface ComponentMapper { */ List selectSubProjectsByComponentUuids(@Param("uuids") Collection uuids); - List selectByKeys(@Param("keys") Collection keys); - - List selectByKeysAndBranch(@Param("keys") Collection keys, @Param("branch") String branch); + List selectByKeysAndBranchOrPr(@Param("keys") Collection keys, + @Nullable @Param("branch") String branch, @Nullable @Param("pullRequest") String pullRequest); List selectByUuids(@Param("uuids") Collection uuids); @@ -109,12 +101,13 @@ public interface ComponentMapper { * @param scope scope of components to return. If null, all components are returned */ List selectComponentsFromProjectKeyAndScope(@Param("projectKey") String projectKey, @Nullable @Param("scope") String scope, - @Param(value = "excludeDisabled") boolean excludeDisabled); + @Param(value = "excludeDisabled") boolean excludeDisabled, @Nullable @Param("branch") String branch, @Nullable @Param("pullRequest") String pullRequest); /** * Return keys and UUIDs of all components belonging to a project */ - List selectUuidsByKeyFromProjectKey(@Param("projectKey") String projectKey); + List selectUuidsByKeyFromProjectKeyAndBranchOrPr(@Param("projectKey") String projectKey, + @Nullable @Param("branch") String branch, @Nullable @Param("pullRequest") String pullRequest); Set selectViewKeysWithEnabledCopyOfProject(@Param("projectUuids") Collection projectUuids); @@ -142,7 +135,7 @@ public interface ComponentMapper { void delete(String componentUuid); List selectComponentsFromPullRequestsTargetingCurrentBranchThatHaveOpenIssues(@Param("referenceBranchUuid") String referenceBranchUuid, - @Param("currentBranchUuid") String currentBranchUuid); + @Param("currentBranchUuid") String currentBranchUuid); List selectComponentsFromBranchesThatHaveOpenIssues(@Param("branchUuids") List branchUuids); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java index ecf0f8520b8..ae54ec9fd17 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java @@ -42,6 +42,8 @@ import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.PropertyNewValue; import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; import static org.apache.commons.lang.StringUtils.repeat; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet; @@ -67,7 +69,6 @@ public class PropertiesDao implements Dao { /** * Returns the logins of users who have subscribed to the given notification dispatcher with the given notification channel. * If a resource ID is passed, the search is made on users who have specifically subscribed for the given resource. - * * Note that {@link UserRole#USER} permission is not checked here, filter the results with * {@link org.sonar.db.permission.AuthorizationDao#keepAuthorizedLoginsOnProject} * @@ -151,14 +152,13 @@ public class PropertiesDao implements Dao { } } - // TODO distinguish branch from project - public List selectProjectProperties(DbSession session, String projectKey) { - return getMapper(session).selectProjectProperties(projectKey); + public List selectComponentProperties(DbSession session, String uuid) { + return getMapper(session).selectByComponentUuids(singletonList(uuid)); } - public List selectProjectProperties(String resourceKey) { + public List selectComponentProperties(String uuid) { try (DbSession session = mybatis.openSession(false)) { - return selectProjectProperties(session, resourceKey); + return selectComponentProperties(session, uuid); } } @@ -261,7 +261,6 @@ public class PropertiesDao implements Dao { * Delete either global, user, component or component per user properties. *

Behaves in exactly the same way as {@link #selectByQuery(PropertyQuery, DbSession)} but deletes rather than * selects

- * * Used by Governance. */ public int deleteByQuery(DbSession dbSession, PropertyQuery query) { 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 2b4255ef3e0..e5975bd3458 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 @@ -262,7 +262,7 @@ class PurgeCommands { void deleteComponents(String rootUuid) { profiler.start("deleteComponents (projects)"); - purgeMapper.deleteComponentsByProjectUuid(rootUuid); + purgeMapper.deleteComponentsByBranchUuid(rootUuid); session.commit(); profiler.stop(); } 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 1e8687178d4..c6139a76461 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 @@ -66,7 +66,7 @@ public interface PurgeMapper { void deletePropertiesByComponentUuids(@Param("componentUuids") List componentUuids); - void deleteComponentsByProjectUuid(@Param("rootUuid") String rootUuid); + void deleteComponentsByBranchUuid(@Param("rootUuid") String rootUuid); void deleteComponentsByMainBranchProjectUuid(@Param("uuid") String uuid); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml index 8112fb214dd..e004a66d79a 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml @@ -18,12 +18,12 @@ WHERE kee = #{key,jdbcType=VARCHAR} - select * from components where uuid = #{uuid,jdbcType=VARCHAR} - select * from components where root_uuid = #{rootUuid,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index c25cc05c20b..5f67411da9d 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -52,15 +52,28 @@ and pb.branch_type='BRANCH' - select from components p - inner join project_branches pb on pb.uuid = p.branch_uuid + + inner join project_branches pb on pb.uuid = p.branch_uuid + where p.kee=#{key,jdbcType=VARCHAR} - and pb.kee=#{pr,jdbcType=VARCHAR} - and pb.branch_type='PULL_REQUEST' + + + AND pb.kee=#{branch,jdbcType=VARCHAR} + AND pb.branch_type = 'BRANCH' + + + AND pb.kee=#{pullRequest,jdbcType=VARCHAR} + AND pb.branch_type = 'PULL_REQUEST' + + + AND p.main_branch_project_uuid is NULL + + - - - - - SELECT FROM components p - INNER JOIN project_branches pb on pb.uuid = p.branch_uuid - + + INNER JOIN project_branches pb on pb.uuid = p.branch_uuid + + WHERE p.enabled=${_true} AND p.kee IN #{key,jdbcType=VARCHAR} - AND pb.kee=#{branch,jdbcType=VARCHAR} - + + + AND pb.kee=#{branch,jdbcType=VARCHAR} + AND pb.branch_type = 'BRANCH' + + + AND pb.kee=#{pullRequest,jdbcType=VARCHAR} + AND pb.branch_type = 'PULL_REQUEST' + + + AND p.main_branch_project_uuid IS NULL + + + + - SELECT p.kee, p.uuid FROM components p INNER JOIN components root ON root.uuid=p.branch_uuid AND root.kee=#{projectKey,jdbcType=VARCHAR} + + INNER JOIN project_branches pb ON pb.uuid = root.uuid + + + + + AND pb.kee = #{branch,jdbcType=VARCHAR} + AND pb.branch_type = 'BRANCH' + + + AND pb.kee=#{pullRequest,jdbcType=VARCHAR} + AND pb.branch_type = 'PULL_REQUEST' + + + AND root.main_branch_project_uuid is NULL + + +