How do you deal with breaking changes in a Rails migration?

Posted by Adam Lassek on Stack Overflow See other posts from Stack Overflow or by Adam Lassek
Published on 2011-01-13T00:42:06Z Indexed on 2011/01/13 0:54 UTC
Read the original article Hit count: 126

Let's say I'm starting out with this model:

class Location < ActiveRecord::Base
  attr_accessible :company_name, :location_name
end

Now I want to refactor one of the values into an associated model.

class CreateCompanies < ActiveRecord::Migration
  def self.up
    create_table :companies do |t|
      t.string :name, :null => false
      t.timestamps
    end

    add_column :locations, :company_id, :integer, :null => false
  end

  def self.down
    drop_table :companies
    remove_column :locations, :company_id
  end
end

class Location < ActiveRecord::Base
  attr_accessible :location_name
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :locations
end

This all works fine during development, since I'm doing everything a step at a time; but if I try deploying this to my staging environment, I run into trouble.

The problem is that since my code has already changed to reflect the migration, it causes the environment to crash when it attempts to run the migration.

Has anyone else dealt with this problem? Am I resigned to splitting my deployment up into multiple steps?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about migration