Skip to content

Commit

Permalink
refactor: Use Rails secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentinchampenois committed Dec 18, 2024
1 parent f1aee47 commit 9652f6b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
53 changes: 34 additions & 19 deletions app/jobs/decidim/papertrail_versions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions spec/jobs/decidim/papertrail_versions_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9652f6b

Please sign in to comment.