Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/clean versions #24

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ GEM

PLATFORMS
arm64-darwin-21
arm64-darwin-22
x86_64-darwin-21
x86_64-linux

Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ bundle exec rails db:migrate
You can then modify the default values of the cleaner in your .ENV with the following variables:

```bash
# Delay until a user is considered inactive and receive a warning email (in days)
# Delay until a user is considered inactive and receive a warning email (in days, default: 365)
DECIDIM_CLEANER_INACTIVE_USERS_MAIL=

# Delay until a user is deleted after receiving an email (in days)
# Delay until a user is deleted after receiving an email (in days, default: 30)
DECIDIM_CLEANER_DELETE_INACTIVE_USERS=

# Delay until an admin log is deleted (in days)
# Delay until an admin log is deleted (in days, default: 365)
DECIDIM_CLEANER_DELETE_ADMIN_LOGS=

# Delay until user's versions are deleted after the user deletion (in days, default: 30)
DECIDIM_CLEANER_DELETE_DELETED_USERS_DATA=

# Delay until deleted authorization's versions are deleted after the authorization creation (in days, default: 30)
DECIDIM_CLEANER_DELETE_DELETED_AUTHORIZATIONS_DATA=
```

### Sidekiq Scheduler
Expand All @@ -52,6 +58,10 @@ You can then add to your 'config/sidekiq.yml' file:
cron: "0 9 0 * * *"
class: Decidim::Cleaner::CleanInactiveUsersJob
queue: scheduled
CleanDeletedUsersData:
cron: "0 9 0 * * *"
class: Decidim::Cleaner::CleanDeletedUsersDataJob
queue: scheduled
```

### Cronjob
Expand Down
50 changes: 50 additions & 0 deletions app/jobs/decidim/cleaner/clean_deleted_users_data_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Decidim
module Cleaner
class CleanDeletedUsersDataJob < ApplicationJob
queue_as :scheduled

def perform
remove_versions_for_deleted_users
remove_versions_for_deleted_authorizations
end

private

def remove_versions_for_deleted_users
PaperTrail::Version.joins(
<<~SQL.squish
INNER JOIN decidim_users ON decidim_users.id = versions.item_id
AND versions.item_type IN ('Decidim::User', 'Decidim::UserBaseEntity')
SQL
).where("deleted_at < ?", date_before_delete_user_versions).each do |version|
version.destroy!
Rails.logger.info "Version for user with id #{version.item_id} destroyed"
end
end

def remove_versions_for_deleted_authorizations
PaperTrail::Version.joins(
<<~SQL.squish
LEFT JOIN decidim_authorizations ON decidim_authorizations.id = versions.item_id
AND versions.item_type = 'Decidim::Authorization'
SQL
).where(item_type: "Decidim::Authorization", decidim_authorizations: { id: nil })
.where("versions.created_at < ?", date_before_delete_authorization_versions)
.each do |version|
version.destroy!
Rails.logger.info "Version for authorization with id #{version.item_id} destroyed"
end
end

def date_before_delete_user_versions
Decidim::Cleaner.delete_deleted_users_data_after.days.ago
end

def date_before_delete_authorization_versions
Decidim::Cleaner.delete_deleted_authorizations_data_after.days.ago
end
end
end
end
8 changes: 8 additions & 0 deletions lib/decidim/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ module Cleaner
config_accessor :delete_inactive_users_email_after do
ENV.fetch("DECIDIM_CLEANER_INACTIVE_USERS_MAIL", "365").to_i
end

config_accessor :delete_deleted_users_data_after do
ENV.fetch("DECIDIM_CLEANER_DELETE_DELETED_USERS_DATA", "30").to_i
end

config_accessor :delete_deleted_authorizations_data_after do
ENV.fetch("DECIDIM_CLEANER_DELETE_DELETED_AUTHORIZATIONS_DATA", "30").to_i
end
end
end
5 changes: 5 additions & 0 deletions lib/tasks/decidim_cleaner_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ namespace :decidim_cleaner do
task clean_admin_logs: :environment do
Decidim::Cleaner::CleanAdminLogsJob.perform_now
end

desc "Deletes versions for deleted users and deleted authorizations"
task clean_versions_for_deleted_data: :environment do
Decidim::Cleaner::CleanDeletedUsersDataJob.perform_now
end
end
# :nocov:
37 changes: 37 additions & 0 deletions spec/jobs/decidim/cleaner/clean_deleted_users_data_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module Cleaner
describe CleanDeletedUsersDataJob do
subject { described_class }

context "when running the job" do
let!(:deleted_user) { create(:user, :deleted, deleted_at: 2.years.ago) }
let!(:user) { create(:user, :confirmed) }
let!(:authorization) { create(:authorization, user:) }

before do
PaperTrail::Version.create!(item: deleted_user, event: "destroy")
PaperTrail::Version.create!(item: user, event: "create")
PaperTrail::Version.create!(item_type: "Decidim::Authorization", item_id: -1, created_at: 2.years.ago, event: "destroy")
PaperTrail::Version.create!(item: authorization, event: "create")
end

it "enqueues job in queue 'cleaner'" do
expect(subject.queue_name).to eq("scheduled")
end

it "removes the versions that should be removed" do
expect do
subject.perform_now
end.to change(PaperTrail::Version, :count).from(4).to(2)

expect(PaperTrail::Version.first.item).to eq(user)
expect(PaperTrail::Version.last.item_type).to eq("Decidim::Authorization")
end
end
end
end
end