]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
Ooops, forgot to commit the routes for the changeset query and include calls.
[rails.git] / lib / migrate.rb
1 module ActiveRecord
2   module ConnectionAdapters
3     module SchemaStatements
4       def quote_column_names(column_name)
5         Array(column_name).map { |e| quote_column_name(e) }.join(", ")
6       end
7
8       def add_primary_key(table_name, column_name, options = {})
9         column_names = Array(column_name)
10         quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
11         execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{quoted_column_names})"
12       end
13
14       def remove_primary_key(table_name)
15         execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
16       end
17
18       def add_foreign_key(table_name, column_name, reftbl, refcol = nil)
19         execute "ALTER TABLE #{table_name} ADD " +
20           "FOREIGN KEY (#{quote_column_names(column_name)}) " +
21           "REFERENCES #{reftbl} (#{quote_column_names(refcol || column_name)})"
22       end
23
24       alias_method :old_options_include_default?, :options_include_default?
25
26       def options_include_default?(options)
27         return false if options[:options] =~ /AUTO_INCREMENT/i
28         return old_options_include_default?(options)
29       end
30
31       alias_method :old_add_column_options!, :add_column_options!
32
33       def add_column_options!(sql, options)
34         sql << " UNSIGNED" if options[:unsigned]
35         old_add_column_options!(sql, options)
36         sql << " #{options[:options]}"
37       end
38     end
39
40     class MysqlAdapter
41       alias_method :old_native_database_types, :native_database_types
42
43       def native_database_types
44         types = old_native_database_types
45         types[:bigint] = { :name => "bigint", :limit => 20 }
46         types[:double] = { :name => "double" }
47         types[:bigint_pk] = { :name => "bigint(20) DEFAULT NULL auto_increment PRIMARY KEY" }
48         types[:bigint_pk_64] = { :name => "bigint(64) DEFAULT NULL auto_increment PRIMARY KEY" }
49         types[:bigint_auto_64] = { :name => "bigint(64) DEFAULT NULL auto_increment" }
50         types[:bigint_auto_11] = { :name => "bigint(11) DEFAULT NULL auto_increment" }
51         types[:bigint_auto_20] = { :name => "bigint(20) DEFAULT NULL auto_increment" }
52         types[:four_byte_unsigned] = { :name=> "integer unsigned NOT NULL" }
53         types
54       end
55
56       def change_column(table_name, column_name, type, options = {})
57         unless options_include_default?(options)
58           options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
59
60           unless type == :string or type == :text
61             options.delete(:default) if options[:default] = "";
62           end
63         end
64
65         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
66         add_column_options!(change_column_sql, options)
67         execute(change_column_sql) 
68       end
69
70       def myisam_table
71         return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
72       end
73
74       def innodb_table
75         return { :id => false, :force => true, :options => "ENGINE=InnoDB" }
76       end
77
78       def innodb_option
79         return "ENGINE=InnoDB"
80       end
81  
82       def change_engine (table_name, engine)
83         execute "ALTER TABLE #{table_name} ENGINE = #{engine}"
84       end
85
86       def add_fulltext_index (table_name, column)
87         execute "CREATE FULLTEXT INDEX `#{table_name}_#{column}_idx` ON `#{table_name}` (`#{column}`)"
88       end
89
90       def alter_column_nwr_enum (table_name, column)
91         execute "alter table #{table_name} change column #{column} #{column} enum('node','way','relation');"
92       end
93     end
94   end
95 end