aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-10-17 18:16:28 +0200
committerTeryk Bellahsene <teryk@users.noreply.github.com>2017-10-19 12:12:11 +0200
commit8cbb514261e2af99b4ee7f79a5c1210e1cee3714 (patch)
tree021da2b2a022f581649e37f71f281231a8e0a6f8 /server/sonar-db-dao/src/main
parentd9564eed6baf772c31d3ee2888dffc7d8c51fca2 (diff)
downloadsonarqube-8cbb514261e2af99b4ee7f79a5c1210e1cee3714.tar.gz
sonarqube-8cbb514261e2af99b4ee7f79a5c1210e1cee3714.zip
SONAR-7309 DB Cleaner strategy to keep only analyses with a version
Diffstat (limited to 'server/sonar-db-dao/src/main')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java15
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java55
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml11
5 files changed, 77 insertions, 8 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java
index 1d91f886174..1d8f8116651 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java
@@ -20,16 +20,19 @@
package org.sonar.db.purge;
import java.util.Date;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
/**
- * Represents an analysis, aka. root snapshot, aka. snapshot of a project, developer or view
+ * Represents an analysis, aka. root snapshot, aka. snapshot of a project or portfolio
*/
public class PurgeableAnalysisDto implements Comparable<PurgeableAnalysisDto> {
private Date date;
private long analysisId;
private String analysisUuid;
+ private String version;
private boolean hasEvents;
private boolean isLast;
@@ -78,6 +81,16 @@ public class PurgeableAnalysisDto implements Comparable<PurgeableAnalysisDto> {
return this;
}
+ @CheckForNull
+ public String getVersion() {
+ return version;
+ }
+
+ public PurgeableAnalysisDto setVersion(@Nullable String version) {
+ this.version = version;
+ return this;
+ }
+
@Override
public int compareTo(PurgeableAnalysisDto other) {
return date.compareTo(other.date);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java
index 8ed4a3c7a6b..8c550e30760 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java
@@ -34,11 +34,13 @@ class Filters {
Date dateToStartKeepingOneSnapshotByDay = getDateFromHours(config, PurgeConstants.HOURS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_DAY);
Date dateToStartKeepingOneSnapshotByWeek = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK);
Date dateToStartKeepingOneSnapshotByMonth = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH);
+ Date dateToStartKeepingOnlyAnalysisWithVersion = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_KEEPING_ONLY_ANALYSES_WITH_VERSION);
Date dateToStartDeletingAllSnapshots = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS);
all.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, dateToStartKeepingOneSnapshotByDay, Calendar.DAY_OF_YEAR, "day"));
all.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByMonth, dateToStartKeepingOneSnapshotByWeek, Calendar.WEEK_OF_YEAR, "week"));
all.add(new KeepOneFilter(dateToStartDeletingAllSnapshots, dateToStartKeepingOneSnapshotByMonth, Calendar.MONTH, "month"));
+ all.add(new KeepWithVersionFilter(dateToStartKeepingOnlyAnalysisWithVersion));
all.add(new DeleteAllFilter(dateToStartDeletingAllSnapshots));
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java
index e67a4c9657a..cc5054cbbec 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java
@@ -57,7 +57,7 @@ class KeepOneFilter implements Filter {
Loggers.get(getClass()).debug("-> Keep one snapshot per {} between {} and {}", label, DateUtils.formatDate(start), DateUtils.formatDate(end));
}
- private void appendSnapshotsToDelete(Interval interval, List<PurgeableAnalysisDto> toDelete) {
+ private static void appendSnapshotsToDelete(Interval interval, List<PurgeableAnalysisDto> toDelete) {
if (interval.count() > 1) {
List<PurgeableAnalysisDto> deletables = Lists.newArrayList();
List<PurgeableAnalysisDto> toKeep = Lists.newArrayList();
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java
new file mode 100644
index 00000000000..d692bc6c298
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java
@@ -0,0 +1,55 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.purge.period;
+
+import com.google.common.base.Strings;
+import java.util.Date;
+import java.util.List;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.stream.MoreCollectors;
+import org.sonar.db.purge.PurgeableAnalysisDto;
+
+class KeepWithVersionFilter implements Filter {
+
+ private final Date before;
+
+ KeepWithVersionFilter(Date before) {
+ this.before = before;
+ }
+
+ @Override
+ public List<PurgeableAnalysisDto> filter(List<PurgeableAnalysisDto> history) {
+ return history.stream()
+ .filter(analysis -> analysis.getDate().before(before))
+ .filter(KeepWithVersionFilter::isDeletable)
+ .collect(MoreCollectors.toList());
+ }
+
+ @Override
+ public void log() {
+ Loggers.get(getClass()).debug("-> Keep analyses with a version prior to {}", DateUtils.formatDate(before));
+ }
+
+ private static boolean isDeletable(PurgeableAnalysisDto snapshot) {
+ return !snapshot.isLast() && Strings.isNullOrEmpty(snapshot.getVersion());
+ }
+
+}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
index 1316351dced..36f668e5626 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
@@ -27,9 +27,9 @@
<select id="selectPurgeableAnalysesWithEvents" parameterType="String" resultType="PurgeableAnalysis">
select
- s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast"
- from
- snapshots s
+ s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast", ve.name as "version"
+ from snapshots s
+ left outer join events ve on ve.analysis_uuid=s.uuid and ve.category='Version'
where
s.component_uuid=#{componentUuid,jdbcType=VARCHAR}
and s.status='P'
@@ -38,9 +38,8 @@
<select id="selectPurgeableAnalysesWithoutEvents" parameterType="String" resultType="PurgeableAnalysis">
select
- s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_false} as "hasEvents", islast as "isLast"
- from
- snapshots s
+ s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_false} as "hasEvents", islast as "isLast", NULL as "version"
+ from snapshots s
where
s.component_uuid=#{componentUuid,jdbcType=VARCHAR}
and s.status='P'