Skip to content

caedes/data-migrations-in-rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Data Migrations in Rails - Example Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published