]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
Add a capabilities API call. Fixes #410.
[rails.git] / lib / migrate.rb
1 module ActiveRecord
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})"
8       end
9
10       def remove_primary_key(table_name)
11         execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
12       end
13
14       alias_method :old_options_include_default?, :options_include_default?
15
16       def options_include_default?(options)
17         return false if options[:options] =~ /AUTO_INCREMENT/i
18         return old_options_include_default?(options)
19       end
20     end
21
22     class MysqlAdapter
23       alias_method :old_native_database_types, :native_database_types
24
25       def native_database_types
26         types = old_native_database_types
27         types[:bigint] = { :name => "bigint", :limit => 20 }
28         types[:double] = { :name => "double" }
29         types
30       end
31
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"]
35
36           unless type == :string or type == :text
37             options.delete(:default) if options[:default] = "";
38           end
39         end
40
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) 
45       end
46
47       def myisam_table
48         return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
49       end
50
51       def innodb_table
52         return { :id => false, :force => true, :options => "ENGINE=InnoDB" }
53       end
54     end
55   end
56 end