The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Alphabetize schema.rb columns

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Paul Gross

Posts: 152
Nickname: pgross
Registered: Sep, 2007

Paul Gross is a software developer for ThoughtWorks.
Alphabetize schema.rb columns Posted: Mar 12, 2008 6:03 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Paul Gross.
Original Post: Alphabetize schema.rb columns
Feed Title: Paul Gross's Blog - Home
Feed URL: http://feeds.feedburner.com/pgrs
Feed Description: Posts mainly about ruby on rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Paul Gross
Latest Posts From Paul Gross's Blog - Home

Advertisement

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

Read: Alphabetize schema.rb columns

Topic: .au Sprint Previous Topic   Next Topic Topic: Reason #437 why Phil Hagelburg rocks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use