Commit 483042fcbef580dcfc32296c625a9f9872985637

Authored by Sergey Kuznetsov
1 parent ccdd2c52
Exists in master and in 1 other branch production

notices_delete rake task

Showing 1 changed file with 21 additions and 3 deletions   Show diff stats
lib/tasks/errbit/database.rake
1 namespace :errbit do 1 namespace :errbit do
2 namespace :db do 2 namespace :db do
3 - 3 +
4 desc "Updates cached attributes on Problem" 4 desc "Updates cached attributes on Problem"
5 task :update_problem_attrs => :environment do 5 task :update_problem_attrs => :environment do
6 puts "Updating problems" 6 puts "Updating problems"
7 Problem.all.each(&:cache_notice_attributes) 7 Problem.all.each(&:cache_notice_attributes)
8 end 8 end
9 - 9 +
10 desc "Updates Problem#notices_count" 10 desc "Updates Problem#notices_count"
11 task :update_notices_count => :environment do 11 task :update_notices_count => :environment do
12 puts "Updating problem.notices_count" 12 puts "Updating problem.notices_count"
@@ -14,12 +14,30 @@ namespace :errbit do @@ -14,12 +14,30 @@ namespace :errbit do
14 p.update_attributes(:notices_count => p.notices.count) 14 p.update_attributes(:notices_count => p.notices.count)
15 end 15 end
16 end 16 end
17 - 17 +
18 desc "Delete resolved errors from the database. (Useful for limited heroku databases)" 18 desc "Delete resolved errors from the database. (Useful for limited heroku databases)"
19 task :clear_resolved => :environment do 19 task :clear_resolved => :environment do
20 count = Problem.resolved.count 20 count = Problem.resolved.count
21 Problem.resolved.each {|problem| problem.destroy } 21 Problem.resolved.each {|problem| problem.destroy }
22 puts "=== Cleared #{count} resolved errors from the database." if count > 0 22 puts "=== Cleared #{count} resolved errors from the database." if count > 0
23 end 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 end 42 end
25 end 43 end