The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Maintaining database column order with migrations

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
Jamie Hill

Posts: 161
Nickname: jamie007
Registered: Nov, 2006

Jamie Hill is Managing Director of SonicIQ Limited in the UK specialising in XHTML, CSS and Rails.
Maintaining database column order with migrations Posted: Nov 21, 2006 9:38 PM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jamie Hill
Latest Posts From The Lucid

Advertisement

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

Read: Maintaining database column order with migrations

Topic: Accessing the non-perishable information on eigenclass.org Previous Topic   Next Topic Topic: El Ba��o -> /dev/null - III Jornadas de Ingenier��a en Computaci��n USB

Sponsored Links



Google
  Web Artima.com   

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