This post originated from an RSS feed registered with Ruby Buzz
by Jamie Hill.
Original Post: Maintaining database column order with migrations
Feed Title: The Lucid
Feed URL: http://feeds.feedburner.com/thelucid
Feed Description: Lightweight ramblings and tips on Ruby and Rails.
If you’re anything like me, you like to keep database columns in a reasonably logical order. In my case I generally keep primary keys as the first column, then content columns, then special usage columns, then foreign keys e.g.
id
name
description
created_on
updated_on
account_id
Now, say I added a ‘slug’ column with a migration:
def self.upadd_column"projects","slug",:stringend
That’s fine, I have my new column but it’s after my foreign keys:
id
name
description
created_on
updated_on
account_id
slug
What’s it doing down there? I want it after the ‘name’ column! ...wouldn’t it be nice if you could specify an :after option for add_column.
We can make this possible by monkey patching the add_column method at the top of our migration file (I will make this into a plugin when I get time):