Wednesday, May 09, 2007

A "Best Migration Practice"

So today, I discovered a "good idea" for migrations.

Put your database schema changes in one migration, then your data changes in the next. This becomes self-evident when you mix the two and watch as part way through your migration, the data change fails and suddenly your tables have been altered.


jnunemaker said...

You can also use the ModelName.reset_column_information information method. ie:

def self.up
  add_column :product, :cost, :integer
  Product.find(:all).each { |p| # do stuff to p }

Jeremy said...

However, in my case, Product.find(:all).each {|p| p.do_it!} threw an exception.

Then, to run the migration again (because it didn't complete), it tried to add the column again.