bit.ly/data-migrations-in-rails
Clone git
repository:
git clone https://github.com/caedes/data-migrations-in-rails.git
Setup the app:
./bin/setup
gco data-migration-file
rake db:migrate
User.all.size
#=> ~50000
gco data-rake-task-file
rake -T
Some issues:
- Doesn't display task when running
rake -T
; - Rake goes through every single record;
- Invokes validations/callbacks;
- Uses condition to check if a user needs to be updated or not;
- Doesn't give us a indication that it is actually working.
namespace :adhoc do
namespace :users do
desc "Fill up users that have both first and last names"
task fill_up: :environment do
users = User.where "users.first_name IS NOT NULL AND users.last_name IS NOT NULL"
puts "Update #{users.count} users..."
ActiveRecord::Base.transaction do
users.update_all filled_at: Time.current
end
puts "done."
end
end
end