Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 1.18 KB

README.md

File metadata and controls

66 lines (48 loc) · 1.18 KB

Data Migrations in Rails - Example Project

bit.ly/data-migrations-in-rails

Getting Started

Clone git repository:

git clone https://github.com/caedes/data-migrations-in-rails.git

Setup the app:

./bin/setup

Demonstration

Data Modification in Migration

gco data-migration-file
rake db:migrate
User.all.size
#=> ~50000

Not So Smart Rake Task

gco data-rake-task-file
rake -T

Some issues:

  1. Doesn't display task when running rake -T;
  2. Rake goes through every single record;
  3. Invokes validations/callbacks;
  4. Uses condition to check if a user needs to be updated or not;
  5. Doesn't give us a indication that it is actually working.

Smart One

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