Class: ActiveRecord::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/change_column_null_migration_fix.rb

Class Method Summary collapse

Class Method Details

.change_column_null(table_name, column_name, column_type, null, default = nil) ⇒ Object

:nodoc:



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/change_column_null_migration_fix.rb', line 2

def self.change_column_null(table_name, column_name, column_type, null, default = nil) #:nodoc: 
  unless null || default.nil? 
    execute("UPDATE #{table_name} SET #{column_name}=#{quote(default)} WHERE #{column_name} IS NULL") 
  end
  if ActiveRecord::Base.connection.adapter_name.downcase == "postgresql"
    execute("ALTER TABLE #{table_name} ALTER #{column_name} #{null ? 'DROP' : 'SET'} NOT NULL") 
  elsif ActiveRecord::Base.connection.adapter_name.downcase == "mysql"
    execute("ALTER TABLE #{table_name} MODIFY COLUMN #{column_name} #{column_type} #{null ? 'NULL' : 'NOT NULL'}")
  else
    raise "'change_column_null' is not supported for connection adapter #{ActiveRecord::Base.connection.adapter_name}."
  end
end