diff --git a/app/jobs/decidim/papertrail_versions_job.rb b/app/jobs/decidim/papertrail_versions_job.rb index 2018642742..f32f0d3c05 100644 --- a/app/jobs/decidim/papertrail_versions_job.rb +++ b/app/jobs/decidim/papertrail_versions_job.rb @@ -6,33 +6,48 @@ class PapertrailVersionsJob < ApplicationJob include Decidim::Logging - def perform(retention = 6.months.ago) + def perform(ret = nil) + ret = retention(ret) + log! "Cleaning versions in database..." - elements = [ - "Decidim::Accountability::TimelineEntry", - "Decidim::Accountability::Result", - "Decidim::Attachment", - "Decidim::AttachmentCollection", - "Decidim::Blogs::Post", - "Decidim::Budgets::Project", - "Decidim::Comments::Comment", - "Decidim::Conferences::MediaLink", - "Decidim::Conferences::Partner", - "Decidim::Debates::Debate", - "Decidim::Categorization", - "Decidim::Categorization", - "Decidim::Forms::Questionnaire", - "Decidim::UserBaseEntity", - ] - log! "Cleaning item_types : #{elements.join(", ")}" + log! "Cleaning item_types : #{item_types.join(", ")}" total = 0 - PaperTrail::Version.where(item_type: elements).where("created_at <= ?", retention).in_batches do |versions| + PaperTrail::Version.where(item_type: item_types).where("created_at <= ?", ret).in_batches do |versions| total += versions.size versions.destroy_all end log! "#{total} versions have been removed" end + + private + + def retention(ret) + return ret if ret.present? && ret.is_a?(Time) + + ret = Rails.application.secrets.dig(:decidim, :database, :versions, :clean, :retention) + ret.months.ago + end + + # Exhaustive list of item_types to remove from versions table + def item_types + @item_types ||= %w( + Decidim::Accountability::TimelineEntry + Decidim::Accountability::Result + Decidim::Attachment + Decidim::AttachmentCollection + Decidim::Blogs::Post + Decidim::Budgets::Project + Decidim::Comments::Comment + Decidim::Conferences::MediaLink + Decidim::Conferences::Partner + Decidim::Debates::Debate + Decidim::Categorization + Decidim::Categorization + Decidim::Forms::Questionnaire + Decidim::UserBaseEntity + ) + end end end diff --git a/config/secrets.yml b/config/secrets.yml index 78f94c3731..f85084b758 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -23,6 +23,10 @@ default: &default authorizations: export_data_to_userdata_enabled_for: <%= ENV.fetch("AUTO_EXPORT_AUTHORIZATIONS_DATA_TO_USER_DATA_ENABLED_FOR", "") %> currency: <%= ENV["CURRENCY"] || "€" %> + database: + versions: + clean: + retention: <%= ENV.fetch("DECIDIM_DB_VERSIONS_RETENTION", "6")&.to_i %> half_signup: show_tos_page_after_signup: <%= ENV.fetch("DECIDIM_HALF_SIGNUP_SHOW_TOS_PAGE_AFTER_SIGNUP", "true") == "true" %> initiatives: diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index e52ab71e49..82a8c09d8b 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -42,7 +42,7 @@ namespace :decidim do desc "Clean versions" task clean: :environment do puts "(decidim:db:versions:clean) #{Time.current.strftime("%d-%m-%Y %H:%M:%S")}> Executing PapertrailVersionsJob..." - retention = ENV.fetch("DECIDIM_DB_VERSIONS_RETENTION", "6")&.to_i + retention = Rails.application.secrets.dig(:decidim, :database, :versions, :clean, :retention) retention = retention.months.ago puts "(decidim:db:versions:clean) #{Time.current.strftime("%d-%m-%Y %H:%M:%S")}> Clean versions created before #{retention.strftime("%d-%m-%Y %H:%M:%S")}..." Decidim::PapertrailVersionsJob.perform_later(retention) diff --git a/spec/jobs/decidim/papertrail_versions_job_spec.rb b/spec/jobs/decidim/papertrail_versions_job_spec.rb index e1fdf72926..b3c98adf68 100644 --- a/spec/jobs/decidim/papertrail_versions_job_spec.rb +++ b/spec/jobs/decidim/papertrail_versions_job_spec.rb @@ -30,5 +30,40 @@ module Decidim end.not_to change(PaperTrail::Version, :count) end end + + describe "#retention" do + subject { described_class.new.send(:retention, retention) } + let(:retention) { 8.months.ago } + + it "returns the defined retention" do + expect(subject).to eq(retention) + end + + context "when retention isn't a Date" do + let(:retention) { 8 } + + it "returns default value" do + expect(subject.strftime("%d-%m-%Y")).to eq(6.months.ago.strftime("%d-%m-%Y")) + end + end + + context "when retention is blank" do + let(:retention) { nil } + + it "returns default value" do + expect(subject.strftime("%d-%m-%Y")).to eq(6.months.ago.strftime("%d-%m-%Y")) + end + end + end + + describe "#item_types" do + subject { described_class.new.send(:item_types) } + + it "does not includes Proposals nor Initiatives" do + expect(subject).not_to include("Decidim::Proposals::Proposal") + expect(subject).not_to include("Decidim::Proposals::CollaborativeDraft") + expect(subject).not_to include("Decidim::Initiative") + end + end end end