2 module ConnectionAdapters
3 module SchemaStatements
4 def add_primary_key(table_name, column_name, options = {})
5 column_names = Array(column_name)
6 quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
7 execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{quoted_column_names})"
10 def remove_primary_key(table_name)
11 execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
14 alias_method :old_options_include_default?, :options_include_default?
16 def options_include_default?(options)
17 return false if options[:options] =~ /AUTO_INCREMENT/i
18 return old_options_include_default?(options)
21 alias_method :old_add_column_options!, :add_column_options!
23 def add_column_options!(sql, options)
24 sql << " UNSIGNED" if options[:unsigned]
25 old_add_column_options!(sql, options)
26 sql << " #{options[:options]}"
31 alias_method :old_native_database_types, :native_database_types
33 def native_database_types
34 types = old_native_database_types
35 types[:bigint] = { :name => "bigint", :limit => 20 }
36 types[:double] = { :name => "double" }
40 def change_column(table_name, column_name, type, options = {})
41 unless options_include_default?(options)
42 options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
44 unless type == :string or type == :text
45 options.delete(:default) if options[:default] = "";
49 change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
50 add_column_options!(change_column_sql, options)
51 execute(change_column_sql)
55 return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
59 return { :id => false, :force => true, :options => "ENGINE=InnoDB" }