How do I check the Database type in a Rails Migration?

Shaun F picture Shaun F · Oct 27, 2009 · Viewed 16.1k times · Source

I have the following migration and I want to be able to check if the current database related to the environment is a mysql database. If it's mysql then I want to execute the SQL that is specific to the database.

How do I go about this?

class AddUsersFb < ActiveRecord::Migration

  def self.up
    add_column :users, :fb_user_id, :integer
    add_column :users, :email_hash, :string
    #if mysql
    #execute("alter table users modify fb_user_id bigint")
  end

  def self.down
    remove_column :users, :fb_user_id
    remove_column :users, :email_hash
  end

end

Answer

stasl picture stasl · Mar 29, 2010

Even more shorter call

ActiveRecord::Base.connection.adapter_name == 'MySQL'