Commit 483042fcbef580dcfc32296c625a9f9872985637
1 parent
ccdd2c52
Exists in
master
and in
1 other branch
notices_delete rake task
Showing
1 changed file
with
21 additions
and
3 deletions
Show diff stats
lib/tasks/errbit/database.rake
1 | 1 | namespace :errbit do |
2 | 2 | namespace :db do |
3 | - | |
3 | + | |
4 | 4 | desc "Updates cached attributes on Problem" |
5 | 5 | task :update_problem_attrs => :environment do |
6 | 6 | puts "Updating problems" |
7 | 7 | Problem.all.each(&:cache_notice_attributes) |
8 | 8 | end |
9 | - | |
9 | + | |
10 | 10 | desc "Updates Problem#notices_count" |
11 | 11 | task :update_notices_count => :environment do |
12 | 12 | puts "Updating problem.notices_count" |
... | ... | @@ -14,12 +14,30 @@ namespace :errbit do |
14 | 14 | p.update_attributes(:notices_count => p.notices.count) |
15 | 15 | end |
16 | 16 | end |
17 | - | |
17 | + | |
18 | 18 | desc "Delete resolved errors from the database. (Useful for limited heroku databases)" |
19 | 19 | task :clear_resolved => :environment do |
20 | 20 | count = Problem.resolved.count |
21 | 21 | Problem.resolved.each {|problem| problem.destroy } |
22 | 22 | puts "=== Cleared #{count} resolved errors from the database." if count > 0 |
23 | 23 | end |
24 | + | |
25 | + desc "Remove notices in batch" | |
26 | + task :notices_delete, [ :problem_id ] => [ :environment ] do | |
27 | + BATCH_SIZE = 1000 | |
28 | + if args[:problem_id] | |
29 | + item_count = Problem.find(args[:problem_id]).notices.count | |
30 | + removed_count = 0 | |
31 | + puts "Notices to remove: #{item_count}" | |
32 | + while item_count > 0 | |
33 | + Problem.find(args[:problem_id]).notices.limit(BATCH_SIZE).each do |notice| | |
34 | + notice.remove | |
35 | + removed_count += 1 | |
36 | + end | |
37 | + item_count -= BATCH_SIZE | |
38 | + puts "Removed #{removed_count} notices" | |
39 | + end | |
40 | + end | |
41 | + end | |
24 | 42 | end |
25 | 43 | end | ... | ... |