Skip to content

Commit

Permalink
Rake task to remove old notifications (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
fblupi authored Nov 21, 2024
1 parent 4508c1b commit c7be9fb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ docker-compose run --rm app bundle exec rake db:create db:schema:load db:seed
docker-compose up
```

## Available tasks

### Notifications removal

You can remove old notifications by running the `notifications:remove_old` task. You can specify the date until you want to preserve notifications and the date from which you will
remove them. By default it will delete all the notifications from 2017-01-01 older than 1 year if no params are provided.

For example, this one will remove all the notifications from 2023-11-12 to 2024-01-01:

```bash
bundle exec rake notifications:destroy_old["2024-01-01","2023-11-12"]
```

## License

Code published under AFFERO GPL v3 (see [LICENSE-AGPLv3.txt](LICENSE-AGPLv3.txt))
Expand Down
29 changes: 29 additions & 0 deletions lib/tasks/notifications.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

namespace :notifications do
desc "Destroy old notifications"
task :destroy_old, [:to, :from] => :environment do |_task, args|
args.with_defaults(
to: 1.year.ago.strftime("%Y-%m-%d"),
from: "2017-01-01"
)

current_date = Date.parse(args.from).beginning_of_month
end_date = Date.parse(args.to).beginning_of_month
dates = []
while current_date <= end_date
dates << current_date.strftime("%Y-%m-%d")
current_date = current_date.next_month
end

total = 0
dates.each do |date|
notifications = Decidim::Notification.where("created_at < ?", date)
total += notifications.count
puts "Destroying #{notifications.count} notifications older than #{date}..."
notifications.destroy_all
end

puts "You have successfully destroyed #{total} notifications."
end
end

0 comments on commit c7be9fb

Please sign in to comment.