From 483042fcbef580dcfc32296c625a9f9872985637 Mon Sep 17 00:00:00 2001 From: Sergey Kuznetsov Date: Mon, 3 Sep 2012 02:44:45 +0400 Subject: [PATCH] notices_delete rake task --- lib/tasks/errbit/database.rake | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/tasks/errbit/database.rake b/lib/tasks/errbit/database.rake index 8f0b287..8d63443 100644 --- a/lib/tasks/errbit/database.rake +++ b/lib/tasks/errbit/database.rake @@ -1,12 +1,12 @@ namespace :errbit do namespace :db do - + desc "Updates cached attributes on Problem" task :update_problem_attrs => :environment do puts "Updating problems" Problem.all.each(&:cache_notice_attributes) end - + desc "Updates Problem#notices_count" task :update_notices_count => :environment do puts "Updating problem.notices_count" @@ -14,12 +14,30 @@ namespace :errbit do p.update_attributes(:notices_count => p.notices.count) end end - + desc "Delete resolved errors from the database. (Useful for limited heroku databases)" task :clear_resolved => :environment do count = Problem.resolved.count Problem.resolved.each {|problem| problem.destroy } puts "=== Cleared #{count} resolved errors from the database." if count > 0 end + + desc "Remove notices in batch" + task :notices_delete, [ :problem_id ] => [ :environment ] do + BATCH_SIZE = 1000 + if args[:problem_id] + item_count = Problem.find(args[:problem_id]).notices.count + removed_count = 0 + puts "Notices to remove: #{item_count}" + while item_count > 0 + Problem.find(args[:problem_id]).notices.limit(BATCH_SIZE).each do |notice| + notice.remove + removed_count += 1 + end + item_count -= BATCH_SIZE + puts "Removed #{removed_count} notices" + end + end + end end end -- libgit2 0.21.2