]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
bb8ffa6e8f4a4d1507d1f2625fa9b51985d0b49f
[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       if MysqlAdapter.public_instance_methods(false).include?('native_database_types')
42         alias_method :old_native_database_types, :native_database_types
43       end
44
45       def native_database_types
46         types = old_native_database_types
47         types[:bigint] = { :name => "bigint", :limit => 20 }
48         types[:double] = { :name => "double" }
49         types[:integer_pk] = { :name => "integer DEFAULT NULL auto_increment PRIMARY KEY" }
50         types[:bigint_pk] = { :name => "bigint(20) DEFAULT NULL auto_increment PRIMARY KEY" }
51         types[:bigint_pk_64] = { :name => "bigint(64) DEFAULT NULL auto_increment PRIMARY KEY" }
52         types[:bigint_auto_64] = { :name => "bigint(64) DEFAULT NULL auto_increment" }
53         types[:bigint_auto_11] = { :name => "bigint(11) DEFAULT NULL auto_increment" }
54         types[:bigint_auto_20] = { :name => "bigint(20) DEFAULT NULL auto_increment" }
55         types[:four_byte_unsigned] = { :name=> "integer unsigned NOT NULL" }
56         types
57       end
58
59       def change_column(table_name, column_name, type, options = {})
60         unless options_include_default?(options)
61           options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
62
63           unless type == :string or type == :text
64             options.delete(:default) if options[:default] = "";
65           end
66         end
67
68         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
69         add_column_options!(change_column_sql, options)
70         execute(change_column_sql) 
71       end
72
73       def myisam_table
74         return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
75       end
76
77       def innodb_table
78         return { :id => false, :force => true, :options => "ENGINE=InnoDB" }
79       end
80
81       def innodb_option
82         return "ENGINE=InnoDB"
83       end
84
85       def change_engine (table_name, engine)
86         execute "ALTER TABLE #{table_name} ENGINE = #{engine}"
87       end
88
89       def add_fulltext_index (table_name, column)
90         execute "CREATE FULLTEXT INDEX `#{table_name}_#{column}_idx` ON `#{table_name}` (`#{column}`)"
91       end
92
93       def alter_column_nwr_enum (table_name, column)
94         execute "alter table #{table_name} change column #{column} #{column} enum('node','way','relation');"
95       end
96
97       def alter_primary_key(table_name, new_columns)
98         execute("alter table #{table_name} drop primary key, add primary key (#{new_columns.join(',')})")
99       end
100     end
101
102     class PostgreSQLAdapter
103       if PostgreSQLAdapter.public_instance_methods(false).include?('native_database_types')
104         alias_method :old_native_database_types, :native_database_types
105       end
106
107       def native_database_types
108         types = old_native_database_types
109         types[:double] = { :name => "double precision" }
110         types[:integer_pk] = { :name => "serial PRIMARY KEY" }
111         types[:bigint_pk] = { :name => "bigserial PRIMARY KEY" }
112         types[:bigint_pk_64] = { :name => "bigserial PRIMARY KEY" }
113         types[:bigint_auto_64] = { :name => "bigint" } #fixme: need autoincrement?
114         types[:bigint_auto_11] = { :name => "bigint" } #fixme: need autoincrement?
115         types[:bigint_auto_20] = { :name => "bigint" } #fixme: need autoincrement?
116         types[:four_byte_unsigned] = { :name => "bigint" } # meh
117         types
118       end
119
120       def myisam_table
121         return { :id => false, :force => true, :options => ""}
122       end
123
124       def innodb_table
125         return { :id => false, :force => true, :options => ""}
126       end
127
128       def innodb_option
129         return ""
130       end
131  
132       def change_engine (table_name, engine)
133       end
134
135       def add_fulltext_index (table_name, column)
136         execute "CREATE INDEX #{table_name}_#{column}_idx on #{table_name} (#{column})"
137       end
138
139       def alter_column_nwr_enum (table_name, column)
140         response = select_one("select count(*) as count from pg_type where typname = 'nwr_enum'")
141         if response['count'] == "0" #yep, as a string
142           execute "create type nwr_enum as ENUM ('node', 'way', 'relation')"
143         end
144         execute "alter table #{table_name} drop #{column}"
145         execute "alter table #{table_name} add #{column} nwr_enum"
146       end
147
148       def alter_primary_key(table_name, new_columns)
149         execute "alter table #{table_name} drop constraint #{table_name}_pkey; alter table #{table_name} add primary key (#{new_columns.join(',')})"
150       end
151     end
152   end
153 end