Maintaining database column order with migrations

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.up
  add_column "projects", "slug", :string
end

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):

module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module SchemaStatements
      def add_column(table_name, column_name, type, options = {})
        add_column_sql = "ALTER TABLE #{table_name} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
        after_column = options.delete(:after)
        add_column_options!(add_column_sql, options)
        add_column_sql << " AFTER #{after_column}" if after_column
        execute(add_column_sql)
      end
    end
  end
end

This lets us do the following:

def self.up
  add_column "projects", "slug", :string, :after => 'name'
end

Which gives us:

id
name
slug
description
created_on
updated_on
account_id

Much better… this may seem a little petty, however as your tables get more and more columns it makes things much easier to follow.

Please note: I have only tested this with MySQL

h4. Update: this is now a plugin and can be “downloaded here”:http://svn.soniciq.com/public/rails/plugins/iq_migration_extensions/

Published by

Jamie

Hi, I am the Managing Director of SonicIQ Limited in the UK. I have been working in the web development industry since 1999 and have been running SonicIQ since 2001. Currently Ruby On Rails is my preferred development platform. I am experienced in designing with web standards, HTML5, CSS3 and Javascript.

2 thoughts on “Maintaining database column order with migrations”

  1. Brilliant… try writing this across database types and write a few tests and get it committed to Rails… I think is a great functionality to add, I know I’m a bit of a stickler for column order, but have more or less given up with migrations and just bite my tongue.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>