source: subversion/sites/rails_port/lib/migrate.rb @ 4156

Last change on this file since 4156 was 3326, checked in by tomhughes, 13 years ago

Enhacements to the migration library code.

File size: 1.9 KB
RevLine 
[3323]1module 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(", ")
[3324]7        execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{quoted_column_names})"
[3323]8      end
9
[3326]10      def remove_primary_key(table_name)
11        execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
[3323]12      end
13
14      alias_method :old_options_include_default?, :options_include_default?
15
16      def options_include_default?(options)
[3326]17        return false if options[:options] =~ /AUTO_INCREMENT/i
18        return old_options_include_default?(options)
[3323]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 }
[3326]28        types[:double] = { :name => "double" }
[3323]29        types
30      end
[3326]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
[3323]54    end
55  end
56end
Note: See TracBrowser for help on using the repository browser.