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

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

Make the tile ID column unsigned as intended.

File size: 2.1 KB
Line 
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(", ")
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
21      alias_method :old_add_column_options!, :add_column_options!
22
23      def add_column_options!(sql, options)
24        sql << " UNSIGNED" if options[:unsigned]
25        old_add_column_options!(sql, options)
26        sql << " #{options[:options]}"
27      end
28    end
29
30    class MysqlAdapter
31      alias_method :old_native_database_types, :native_database_types
32
33      def native_database_types
34        types = old_native_database_types
35        types[:bigint] = { :name => "bigint", :limit => 20 }
36        types[:double] = { :name => "double" }
37        types
38      end
39
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"]
43
44          unless type == :string or type == :text
45            options.delete(:default) if options[:default] = "";
46          end
47        end
48
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) 
52      end
53
54      def myisam_table
55        return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
56      end
57
58      def innodb_table
59        return { :id => false, :force => true, :options => "ENGINE=InnoDB" }
60      end
61    end
62  end
63end
Note: See TracBrowser for help on using the repository browser.