]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1960 DBCleaner properties should be expressed in weeks instead of months
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 26 Jan 2012 17:14:17 +0000 (18:14 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 26 Jan 2012 17:14:17 +0000 (18:14 +0100)
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filters.java
sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java
sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql
sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb [new file with mode: 0644]

index a14b64b1f30c20de9898dbbeb7be46d24cfbaa2d..e039a1ff189c9d8347bfe3dccb8f67f6b0e1b562 100644 (file)
@@ -30,17 +30,17 @@ import java.util.Arrays;
 import java.util.List;
 
 @Properties({
-  @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, defaultValue = DbCleanerConstants.ONE_MONTH,
-    name = "Number of months before starting to keep only one snapshot by week",
-    description = "After this number of months, if there are several snapshots during the same week, "
+  @Property(key = DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, defaultValue = "4",
+    name = "Number of weeks before starting to keep only one snapshot by week",
+    description = "After this number of weeks, if there are several snapshots during the same week, "
       + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true),
-  @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, defaultValue = DbCleanerConstants.ONE_YEAR,
-    name = "Number of months before starting to keep only one snapshot by month",
-    description = "After this number of months, if there are several snapshots during the same month, "
+  @Property(key = DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, defaultValue = "52",
+    name = "Number of weeks before starting to keep only one snapshot by month",
+    description = "After this number of weeks, if there are several snapshots during the same month, "
       + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true),
-  @Property(key = DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS, defaultValue = DbCleanerConstants.FIVE_YEARS,
-    name = "Number of months before starting to delete all remaining snapshots",
-    description = "After this number of months, all snapshots are fully deleted.", global = true, project = true),
+  @Property(key = DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS, defaultValue = "260",
+    name = "Number of weeks before starting to delete all remaining snapshots",
+    description = "After this number of weeks, all snapshots are fully deleted.", global = true, project = true),
   @Property(key = "sonar.purge.minimumPeriodInHours", defaultValue = "12",
     name = "Maximum duration of code inspections, in hours",
     description = "Sonar has an embedded purge mechanism which is fairly powerful to avoid keeping useless data. This mechanism is using a minimum period during which a " +
index a1e4bd8aabbc4652da6401f3433c338dbcb96341..0dcca8e94248938b3711871b21c1d7791b3ba888 100644 (file)
@@ -24,10 +24,9 @@ public interface DbCleanerConstants {
   String PLUGIN_KEY = "dbcleaner";
   String PLUGIN_NAME = "DbCleaner";
   String PROPERTY_CLEAN_DIRECTORY = "sonar.dbcleaner.cleanDirectory";
-  String MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK = "sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByWeek";
-  String MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH = "sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByMonth";
-  String MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS = "sonar.dbcleaner.monthsBeforeDeletingAllSnapshots";
-  String ONE_MONTH = "1";
-  String ONE_YEAR = "12";
-  String FIVE_YEARS = "60";
+
+  String WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK = "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek";
+  String WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH = "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByMonth";
+  String WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS = "sonar.dbcleaner.weeksBeforeDeletingAllSnapshots";
+
 }
index f89db209adfcb0d04a3877fc58bdd98f7c666d69..3b59951386023067040889f609276f7beecedf60 100644 (file)
@@ -26,16 +26,15 @@ import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
 
 import java.util.Calendar;
 import java.util.Date;
-import java.util.GregorianCalendar;
 import java.util.List;
 
 class Filters {
   private final List<Filter> filters = Lists.newArrayList();
 
   Filters(Settings settings) {
-    Date dateToStartKeepingOneSnapshotByWeek = getDate(settings, DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK);
-    Date dateToStartKeepingOneSnapshotByMonth = getDate(settings, DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH);
-    Date dateToStartDeletingAllSnapshots = getDate(settings, DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS);
+    Date dateToStartKeepingOneSnapshotByWeek = getDate(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK);
+    Date dateToStartKeepingOneSnapshotByMonth = getDate(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH);
+    Date dateToStartDeletingAllSnapshots = getDate(settings, DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS);
 
     filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, new Date(), Calendar.DAY_OF_YEAR, "day"));
     filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByMonth, dateToStartKeepingOneSnapshotByWeek, Calendar.WEEK_OF_YEAR, "week"));
@@ -48,9 +47,7 @@ class Filters {
   }
 
   static Date getDate(Settings settings, String propertyKey) {
-    int months = settings.getInt(propertyKey);
-    GregorianCalendar calendar = new GregorianCalendar();
-    calendar.add(GregorianCalendar.MONTH, -months);
-    return calendar.getTime();
+    int weeks = settings.getInt(propertyKey);
+    return DateUtils.addWeeks(new Date(), -weeks);
   }
 }
index 4481fef39a76fb5d326d1e61df5b26b2dd983ff2..43055ee6f1510c550d113d0bd2253b060c252333 100644 (file)
@@ -34,7 +34,7 @@ public class SchemaMigration {
 
   public final static int VERSION_UNKNOWN = -1;
 
-  public static final int LAST_VERSION = 254;
+  public static final int LAST_VERSION = 255;
   public static final int VERSION_2_13 = 241;
 
   public final static String TABLE_NAME = "schema_migrations";
index fd4626b92678138c3c88accb81f8f71d614ab950..e5772797a153d8a394fd7d1a7ccff0c3ce6e53a0 100644 (file)
@@ -173,6 +173,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('251');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('252');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('253');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('254');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('255');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null);
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb
new file mode 100644 (file)
index 0000000..c1b6066
--- /dev/null
@@ -0,0 +1,42 @@
+#
+# Sonar, entreprise quality control tool.
+# Copyright (C) 2008-2012 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# Sonar 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.
+#
+# Sonar 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 Sonar; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+#
+
+#
+# Sonar 2.14
+#
+class RenameDbcleanerProperties < ActiveRecord::Migration
+
+  class Property < ActiveRecord::Base
+  end
+
+
+  def self.up
+    rename('sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByWeek', 'sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek')
+    rename('sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByMonth', 'sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByMonth')
+    rename('sonar.dbcleaner.monthsBeforeDeletingAllSnapshots', 'sonar.dbcleaner.weeksBeforeDeletingAllSnapshots')
+  end
+
+  private
+  def self.rename(month_key, week_key)
+    Property.find(:all, :conditions => ['prop_key=? and text_value is not null', month_key]).each do |month_property|
+      Property.create(:prop_key => week_key, :resource_id => month_property.resource_id, :text_value => (month_property.text_value.to_i * 4).to_s)
+    end
+  end
+end