From e6fea33be6dd9333abdd82444317a708804d2f85 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 28 Nov 2017 10:28:32 +0100 Subject: [PATCH] SONAR-10131 purge test files when purging project history --- .../db/component/ComponentTreeQuery.java | 15 ++ .../sonar/db/purge/PurgeConfiguration.java | 15 +- .../java/org/sonar/db/purge/PurgeDao.java | 9 +- .../sonar/db/component/ComponentMapper.xml | 6 + .../org/sonar/db/measure/MeasureMapper.xml | 15 +- .../sonar/db/component/ComponentDaoTest.java | 25 +- .../db/purge/PurgeConfigurationTest.java | 27 +-- .../java/org/sonar/db/purge/PurgeDaoTest.java | 11 +- ...oricalDataOfDirectoriesAndFiles-result.xml | 216 +++++++++++++++--- ...eteHistoricalDataOfDirectoriesAndFiles.xml | 210 +++++++++++++++-- 10 files changed, 469 insertions(+), 80 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentTreeQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentTreeQuery.java index 6119f3cb9ae..a6099250007 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentTreeQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentTreeQuery.java @@ -42,12 +42,15 @@ public class ComponentTreeQuery { // SONAR-7681 a public implementation of List must be used in MyBatis - potential concurrency exceptions otherwise @CheckForNull private final ArrayList qualifiers; + @CheckForNull + private final ArrayList scopes; private final String baseUuid; private final Strategy strategy; private ComponentTreeQuery(Builder builder) { this.nameOrKeyQuery = builder.nameOrKeyQuery; this.qualifiers = builder.qualifiers == null ? null : newArrayList(builder.qualifiers); + this.scopes = builder.scopes == null ? null : newArrayList(builder.scopes); this.baseUuid = builder.baseUuid; this.strategy = requireNonNull(builder.strategy); } @@ -57,6 +60,11 @@ public class ComponentTreeQuery { return qualifiers; } + @CheckForNull + public Collection getScopes() { + return scopes; + } + @CheckForNull public String getNameOrKeyQuery() { return nameOrKeyQuery; @@ -98,6 +106,8 @@ public class ComponentTreeQuery { private String nameOrKeyQuery; @CheckForNull private Collection qualifiers; + @CheckForNull + private Collection scopes; private String baseUuid; private Strategy strategy; @@ -120,6 +130,11 @@ public class ComponentTreeQuery { return this; } + public Builder setScopes(Collection scopes) { + this.scopes = scopes; + return this; + } + public Builder setBaseUuid(String uuid) { this.baseUuid = uuid; return this; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeConfiguration.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeConfiguration.java index c9d903f883f..15fc642e842 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeConfiguration.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeConfiguration.java @@ -30,16 +30,19 @@ import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; import org.sonar.core.config.PurgeConstants; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + public class PurgeConfiguration { private final IdUuidPair rootProjectIdUuid; - private final String[] scopesWithoutHistoricalData; + private final Collection scopesWithoutHistoricalData; private final int maxAgeInDaysOfClosedIssues; private final Optional maxAgeInDaysOfInactiveShortLivingBranches; private final System2 system2; private final Collection disabledComponentUuids; - public PurgeConfiguration(IdUuidPair rootProjectId, String[] scopesWithoutHistoricalData, int maxAgeInDaysOfClosedIssues, + public PurgeConfiguration(IdUuidPair rootProjectId, Collection scopesWithoutHistoricalData, int maxAgeInDaysOfClosedIssues, Optional maxAgeInDaysOfInactiveShortLivingBranches, System2 system2, Collection disabledComponentUuids) { this.rootProjectIdUuid = rootProjectId; this.scopesWithoutHistoricalData = scopesWithoutHistoricalData; @@ -50,11 +53,11 @@ public class PurgeConfiguration { } public static PurgeConfiguration newDefaultPurgeConfiguration(Configuration config, IdUuidPair idUuidPair, Collection disabledComponentUuids) { - String[] scopes = new String[] {Scopes.FILE}; + Collection scopesWithoutHistoricalData = singletonList(Scopes.FILE); if (config.getBoolean(PurgeConstants.PROPERTY_CLEAN_DIRECTORY).orElse(false)) { - scopes = new String[] {Scopes.DIRECTORY, Scopes.FILE}; + scopesWithoutHistoricalData = asList(Scopes.FILE, Scopes.DIRECTORY); } - return new PurgeConfiguration(idUuidPair, scopes, config.getInt(PurgeConstants.DAYS_BEFORE_DELETING_CLOSED_ISSUES).get(), + return new PurgeConfiguration(idUuidPair, scopesWithoutHistoricalData, config.getInt(PurgeConstants.DAYS_BEFORE_DELETING_CLOSED_ISSUES).get(), config.getInt(PurgeConstants.DAYS_BEFORE_DELETING_INACTIVE_SHORT_LIVING_BRANCHES), System2.INSTANCE, disabledComponentUuids); } @@ -62,7 +65,7 @@ public class PurgeConfiguration { return rootProjectIdUuid; } - public String[] scopesWithoutHistoricalData() { + public Collection getScopesWithoutHistoricalData() { return scopesWithoutHistoricalData; } 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 de29166e268..05cb3aee82b 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 @@ -21,7 +21,6 @@ package org.sonar.db.purge; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -64,7 +63,7 @@ public class PurgeDao implements Dao { PurgeCommands commands = new PurgeCommands(session, mapper, profiler); String rootUuid = conf.rootProjectIdUuid().getUuid(); deleteAbortedAnalyses(rootUuid, commands); - deleteDataOfComponentsWithoutHistoricalData(session, rootUuid, conf.scopesWithoutHistoricalData(), commands); + deleteDataOfComponentsWithoutHistoricalData(session, rootUuid, conf.getScopesWithoutHistoricalData(), commands); purgeAnalyses(commands, rootUuid); purgeDisabledComponents(session, conf, listener); deleteOldClosedIssues(conf, mapper, listener); @@ -119,8 +118,8 @@ public class PurgeDao implements Dao { commands.deleteAnalyses(query); } - private void deleteDataOfComponentsWithoutHistoricalData(DbSession dbSession, String rootUuid, String[] scopesWithoutHistoricalData, PurgeCommands purgeCommands) { - if (scopesWithoutHistoricalData.length == 0) { + private void deleteDataOfComponentsWithoutHistoricalData(DbSession dbSession, String rootUuid, Collection scopesWithoutHistoricalData, PurgeCommands purgeCommands) { + if (scopesWithoutHistoricalData.isEmpty()) { return; } @@ -134,7 +133,7 @@ public class PurgeDao implements Dao { dbSession, ComponentTreeQuery.builder() .setBaseUuid(rootUuid) - .setQualifiers(Arrays.asList(scopesWithoutHistoricalData)) + .setScopes(scopesWithoutHistoricalData) .setStrategy(Strategy.LEAVES) .build()) .stream().map(ComponentDto::uuid) 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 5d7e051032f..b22881f4b64 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 @@ -374,6 +374,12 @@ #{qualifier,jdbcType=VARCHAR} + + and p.scope in + + #{scope,jdbcType=VARCHAR} + + and ( p.kee = #{query.nameOrKeyQuery,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml index 860b4e73f47..d29bcf17665 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml @@ -121,7 +121,20 @@ and pm.person_id is null - + and p.enabled = ${_true} + + and p.qualifier in + + #{qualifier,jdbcType=VARCHAR} + + + + and ( + p.kee = #{query.nameOrKeyQuery,jdbcType=VARCHAR} + or + upper(p.name) like #{query.nameOrKeyUpperLikeQuery,jdbcType=VARCHAR} escape '/' + ) +