Alter Column the Missing Migration Method for ActiveRecord

Changing many columns is not so easy, because change_column forces you to enter all attributes you want (default,limit,null…) again.
So to make less errors we just alter the attributes we want to change and leave the rest alone.

Install

#Inside your migration
def self.alter_column(table,column,text)
  execute("ALTER TABLE #{quote_table_name(table)} ALTER #{quote_column_name(column)} #{text}")
end

Usage

# to set the default to nil, without remembering all the other 
# stuff change_column needs
alter_column(:users,:name,"DROP DEFAULT")

So thats very basic, but i hope it helps anyway 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s