From bc35388d1f2d98fc19a7fd2d899d39164c13a21c Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 14 Dec 2010 20:58:22 -0800 Subject: [PATCH 1/2] Use find_each --- lib/tasks/migrations.rake | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/tasks/migrations.rake b/lib/tasks/migrations.rake index 3fa9cbced..6c86bd113 100644 --- a/lib/tasks/migrations.rake +++ b/lib/tasks/migrations.rake @@ -30,11 +30,13 @@ namespace :migrations do task :contacts_as_requests do require File.join(Rails.root,"config/environment") puts "Migrating contacts..." - old_contacts = Contact.all(:pending => nil) - old_contacts.each{|contact| contact.pending = false; contact.save} + Contact.find_each(:pending => nil){|contact| + contact.pending = false; contact.save + } puts "Deleting stale requests..." - old_requests = Request.all(:sent => true) - old_requests.each{|request| request.delete} + Request.find_each(:sent => true){|request| + request.delete + } puts "Done!" end From 94fe0b4049d903b6f14caac4be34cbe33fa5f4a2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 14 Dec 2010 21:54:18 -0800 Subject: [PATCH 2/2] Do the update in JS --- lib/tasks/migrations.rake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tasks/migrations.rake b/lib/tasks/migrations.rake index 6c86bd113..4965c6793 100644 --- a/lib/tasks/migrations.rake +++ b/lib/tasks/migrations.rake @@ -30,9 +30,9 @@ namespace :migrations do task :contacts_as_requests do require File.join(Rails.root,"config/environment") puts "Migrating contacts..." - Contact.find_each(:pending => nil){|contact| - contact.pending = false; contact.save - } + MongoMapper.database.eval(' + db.contacts.find({pending : null}).forEach(function(contact){ + db.contacts.update({"_id" : contact["_id"]}, {"$set" : {"pending" : false}}); });') puts "Deleting stale requests..." Request.find_each(:sent => true){|request| request.delete