Skip to content

Commit d3b7ee7

Browse files
Feature - Clean deleted users related versioning (#24)
* WIP clean versions * add delete data feature * Add arm64-darwin-22 support in Gemfile.lock * Refactor clean_deleted_users_data_job specs --------- Co-authored-by: quentinchampenois <[email protected]>
1 parent 8dc1441 commit d3b7ee7

File tree

6 files changed

+114
-3
lines changed

6 files changed

+114
-3
lines changed

Gemfile.lock

+1
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ GEM
760760

761761
PLATFORMS
762762
arm64-darwin-21
763+
arm64-darwin-22
763764
x86_64-darwin-21
764765
x86_64-linux
765766

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ bundle exec rails db:migrate
2525
You can then modify the default values of the cleaner in your .ENV with the following variables:
2626

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

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

34-
# Delay until an admin log is deleted (in days)
34+
# Delay until an admin log is deleted (in days, default: 365)
3535
DECIDIM_CLEANER_DELETE_ADMIN_LOGS=
36+
37+
# Delay until user's versions are deleted after the user deletion (in days, default: 30)
38+
DECIDIM_CLEANER_DELETE_DELETED_USERS_DATA=
39+
40+
# Delay until deleted authorization's versions are deleted after the authorization creation (in days, default: 30)
41+
DECIDIM_CLEANER_DELETE_DELETED_AUTHORIZATIONS_DATA=
3642
```
3743

3844
### Sidekiq Scheduler
@@ -52,6 +58,10 @@ You can then add to your 'config/sidekiq.yml' file:
5258
cron: "0 9 0 * * *"
5359
class: Decidim::Cleaner::CleanInactiveUsersJob
5460
queue: scheduled
61+
CleanDeletedUsersData:
62+
cron: "0 9 0 * * *"
63+
class: Decidim::Cleaner::CleanDeletedUsersDataJob
64+
queue: scheduled
5565
```
5666
5767
### Cronjob
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Cleaner
5+
class CleanDeletedUsersDataJob < ApplicationJob
6+
queue_as :scheduled
7+
8+
def perform
9+
remove_versions_for_deleted_users
10+
remove_versions_for_deleted_authorizations
11+
end
12+
13+
private
14+
15+
def remove_versions_for_deleted_users
16+
PaperTrail::Version.joins(
17+
<<~SQL.squish
18+
INNER JOIN decidim_users ON decidim_users.id = versions.item_id
19+
AND versions.item_type IN ('Decidim::User', 'Decidim::UserBaseEntity')
20+
SQL
21+
).where("deleted_at < ?", date_before_delete_user_versions).each do |version|
22+
version.destroy!
23+
Rails.logger.info "Version for user with id #{version.item_id} destroyed"
24+
end
25+
end
26+
27+
def remove_versions_for_deleted_authorizations
28+
PaperTrail::Version.joins(
29+
<<~SQL.squish
30+
LEFT JOIN decidim_authorizations ON decidim_authorizations.id = versions.item_id
31+
AND versions.item_type = 'Decidim::Authorization'
32+
SQL
33+
).where(item_type: "Decidim::Authorization", decidim_authorizations: { id: nil })
34+
.where("versions.created_at < ?", date_before_delete_authorization_versions)
35+
.each do |version|
36+
version.destroy!
37+
Rails.logger.info "Version for authorization with id #{version.item_id} destroyed"
38+
end
39+
end
40+
41+
def date_before_delete_user_versions
42+
Decidim::Cleaner.delete_deleted_users_data_after.days.ago
43+
end
44+
45+
def date_before_delete_authorization_versions
46+
Decidim::Cleaner.delete_deleted_authorizations_data_after.days.ago
47+
end
48+
end
49+
end
50+
end

lib/decidim/cleaner.rb

+8
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@ module Cleaner
2020
config_accessor :delete_inactive_users_email_after do
2121
ENV.fetch("DECIDIM_CLEANER_INACTIVE_USERS_MAIL", "365").to_i
2222
end
23+
24+
config_accessor :delete_deleted_users_data_after do
25+
ENV.fetch("DECIDIM_CLEANER_DELETE_DELETED_USERS_DATA", "30").to_i
26+
end
27+
28+
config_accessor :delete_deleted_authorizations_data_after do
29+
ENV.fetch("DECIDIM_CLEANER_DELETE_DELETED_AUTHORIZATIONS_DATA", "30").to_i
30+
end
2331
end
2432
end

lib/tasks/decidim_cleaner_tasks.rake

+5
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ namespace :decidim_cleaner do
1111
task clean_admin_logs: :environment do
1212
Decidim::Cleaner::CleanAdminLogsJob.perform_now
1313
end
14+
15+
desc "Deletes versions for deleted users and deleted authorizations"
16+
task clean_versions_for_deleted_data: :environment do
17+
Decidim::Cleaner::CleanDeletedUsersDataJob.perform_now
18+
end
1419
end
1520
# :nocov:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
module Decidim
6+
module Cleaner
7+
describe CleanDeletedUsersDataJob do
8+
subject { described_class }
9+
10+
context "when running the job" do
11+
let!(:deleted_user) { create(:user, :deleted, deleted_at: 2.years.ago) }
12+
let!(:user) { create(:user, :confirmed) }
13+
let!(:authorization) { create(:authorization, user:) }
14+
15+
before do
16+
PaperTrail::Version.create!(item: deleted_user, event: "destroy")
17+
PaperTrail::Version.create!(item: user, event: "create")
18+
PaperTrail::Version.create!(item_type: "Decidim::Authorization", item_id: -1, created_at: 2.years.ago, event: "destroy")
19+
PaperTrail::Version.create!(item: authorization, event: "create")
20+
end
21+
22+
it "enqueues job in queue 'cleaner'" do
23+
expect(subject.queue_name).to eq("scheduled")
24+
end
25+
26+
it "removes the versions that should be removed" do
27+
expect do
28+
subject.perform_now
29+
end.to change(PaperTrail::Version, :count).from(4).to(2)
30+
31+
expect(PaperTrail::Version.first.item).to eq(user)
32+
expect(PaperTrail::Version.last.item_type).to eq("Decidim::Authorization")
33+
end
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)