Rails: Output helpers for migrations

Updated . Posted . Visible to the public.

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.

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) ================================
Dominik Schöler
Last edit
Keywords
puts, print
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2013-08-21 16:18)