Alphabetize schema.rb columns

I wrote previously about automated testing of database rollback scripts in rails. After running the rollback scripts, we verify our database schema by comparing the schema.rb file from before the upgrade with the one after rollback. The problem is that the columns for each table in schema.rb seem to appear in the order that they were created, not in alphabetical order. So, if a column is dropped and then re-added, it will move down to the bottom of the list. For example, we start with a table like:

  create_table "foo", :force => true do |t|
    t.string "first"
    t.string "second"
  end

Now, version 6 drops the “a” column, and the rollback adds it back. Then, the schema.rb file will look like:

  create_table "foo", :force => true do |t|
    t.string "second"
    t.string "first"
  end

In order to compare schema.rb files before and after upgrades, we wanted to alphabetize the column list. We did this by monkey patching the columns method on ActiveRecord::Base.connection. We only want to change the columns method when dumping the schema. We do not want to change the running application in any way. so we only run our monkey patch when calling the db:schema:dump task. Our solution looks like:

task :'db:schema:dump' => :'db:alphabetize_columns'

task :'db:alphabetize_columns' do
  class << ActiveRecord::Base.connection
    alias_method :old_columns, :columns unless self.instance_methods.include?("old_columns")

    def columns(*args)
      old_columns(*args).sort_by(&:name)
    end
  end
end