Ruby Flare

Your awesome Tagline

14 notes

Talkative migrations

Sometimes I’ll have a migration that needs to convert existing data. When run, it can look like the migration has stalled (especially if you didn’t write the migration and are simply migrating your database to the latest). I like to wrap these blocks with the say_with_time method to let the developer know what is happening.

The say_with_time method outputs text along with how long it took to run the block. If the block returns an integer it assumes it is the number of rows affected.

say_with_time "Converting correct responses... (go grab a cuppa)" do
  Question.all.each do |q|
    q.update_attribute(:correct_response, convert_to_letter(q.correct_response))
  end
end

(Source: guides.rubyonrails.org)

Filed under migrations

  1. rubyflare posted this