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)
23 alias_method :old_native_database_types, :native_database_types
25 def native_database_types
26 types = old_native_database_types
27 types[:bigint] = { :name => "bigint", :limit => 20 }
28 types[:double] = { :name => "double" }
32 def change_column(table_name, column_name, type, options = {})
33 unless options_include_default?(options)
34 options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
36 unless type == :string or type == :text
37 options.delete(:default) if options[:default] = "";
41 change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
42 add_column_options!(change_column_sql, options)
43 change_column_sql << " #{options[:options]}"
44 execute(change_column_sql)
48 return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
52 return { :id => false, :force => true, :options => "ENGINE=InnoDB" }