]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
Reject any AMF request which is not a POST request.
[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" }
56         types[:inet] = { :name=> "integer unsigned" }
57         types
58       end
59
60       def change_column(table_name, column_name, type, options = {})
61         unless options_include_default?(options)
62           options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
63
64           unless type == :string or type == :text
65             options.delete(:default) if options[:default] = "";
66           end
67         end
68
69         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
70         add_column_options!(change_column_sql, options)
71         execute(change_column_sql) 
72       end
73
74       def myisam_table
75         return { :id => false, :force => true, :options => "ENGINE=MyIsam" }
76       end
77
78       def innodb_table
79         return { :id => false, :force => true, :options => "ENGINE=InnoDB" }
80       end
81
82       def innodb_option
83         return "ENGINE=InnoDB"
84       end
85
86       def change_engine (table_name, engine)
87         execute "ALTER TABLE #{table_name} ENGINE = #{engine}"
88       end
89
90       def add_fulltext_index (table_name, column)
91         execute "CREATE FULLTEXT INDEX `#{table_name}_#{column}_idx` ON `#{table_name}` (`#{column}`)"
92       end
93
94       def alter_column_nwr_enum (table_name, column)
95         execute "alter table #{table_name} change column #{column} #{column} enum('Node','Way','Relation');"
96       end
97
98       def alter_primary_key(table_name, new_columns)
99         execute("alter table #{table_name} drop primary key, add primary key (#{new_columns.join(',')})")
100       end
101
102       def interval_constant(interval)
103         "'#{interval}'"
104       end
105     end
106
107     class PostgreSQLAdapter
108       if PostgreSQLAdapter.public_instance_methods(false).include?('native_database_types')
109         alias_method :old_native_database_types, :native_database_types
110       end
111
112       def native_database_types
113         types = old_native_database_types
114         types[:double] = { :name => "double precision" }
115         types[:integer_pk] = { :name => "serial PRIMARY KEY" }
116         types[:bigint_pk] = { :name => "bigserial PRIMARY KEY" }
117         types[:bigint_pk_64] = { :name => "bigserial PRIMARY KEY" }
118         types[:bigint_auto_64] = { :name => "bigint" } #fixme: need autoincrement?
119         types[:bigint_auto_11] = { :name => "bigint" } #fixme: need autoincrement?
120         types[:bigint_auto_20] = { :name => "bigint" } #fixme: need autoincrement?
121         types[:four_byte_unsigned] = { :name => "bigint" } # meh
122         types[:inet] = { :name=> "inet" }
123         types
124       end
125
126       def myisam_table
127         return { :id => false, :force => true, :options => ""}
128       end
129
130       def innodb_table
131         return { :id => false, :force => true, :options => ""}
132       end
133
134       def innodb_option
135         return ""
136       end
137  
138       def change_engine (table_name, engine)
139       end
140
141       def add_fulltext_index (table_name, column)
142         execute "CREATE INDEX #{table_name}_#{column}_idx on #{table_name} (#{column})"
143       end
144
145       def alter_column_nwr_enum (table_name, column)
146         response = select_one("select count(*) as count from pg_type where typname = 'nwr_enum'")
147         if response['count'] == "0" #yep, as a string
148           execute "create type nwr_enum as ENUM ('Node', 'Way', 'Relation')"
149         end
150         execute "alter table #{table_name} drop #{column}"
151         execute "alter table #{table_name} add #{column} nwr_enum"
152       end
153
154       def alter_primary_key(table_name, new_columns)
155         execute "alter table #{table_name} drop constraint #{table_name}_pkey; alter table #{table_name} add primary key (#{new_columns.join(',')})"
156       end
157
158       def interval_constant(interval)
159         "'#{interval}'::interval"
160       end
161
162       def add_index(table_name, column_name, options = {})
163         column_names = Array(column_name)
164         index_name   = index_name(table_name, :column => column_names)
165
166         if Hash === options # legacy support, since this param was a string
167           index_type = options[:unique] ? "UNIQUE" : ""
168           index_name = options[:name] || index_name
169           index_method = options[:method] || "BTREE"
170         else
171           index_type = options
172         end
173         quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
174         execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} USING #{index_method} (#{quoted_column_names})"
175       end
176     end
177   end
178 end