Read more

Rails: Output helpers for migrations

Dominik Schöler
August 21, 2013Software engineer at makandra GmbH

When you're writing migrations that do more than changing tables (like, modify many records) you may want some output. In Rails > 3.1 you have two methods at hand: announce and say_with_time.

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

In the migration:

class AddUserToken < ActiveRecord::Migration

  class User < ActiveRecod::Base; end

  def up
    add_column :users, :token, :string
    
    announce "now generating tokens"
    User.find_in_batches do |users|
      say_with_time "For users ##{users.first.id} to ##{users.last.id}" do
        users.each do |user|
          user.update_attribute :token, # ... generate token
        end
      end
    end
  end

end

Output:

==  AddUserToken: migrating =========================================
# ...
==  AddUserToken: now generating tokens =============================
-- For users #1 to #500
   -> 1.542s
   -> 1 rows
==  AddUserToken: migrated (1.0003s) ================================
Posted by Dominik Schöler to makandra dev (2013-08-21 18:18)