]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
Merge remote-tracking branch 'upstream/pull/2037'
[rails.git] / lib / migrate.rb
1 module OpenStreetMap
2   module ActiveRecord
3     module AbstractAdapter
4       def add_index_options(table_name, column_name, options = {})
5         columns = options.delete(:columns)
6         index_name, index_type, index_columns, index_options, algorithm, using = super(table_name, column_name, options)
7         [index_name, index_type, columns || index_columns, index_options, algorithm, using]
8       end
9     end
10
11     module PostgreSQLAdapter
12       def quote_column_name(name)
13         Array(name).map { |n| super(n) }.join(", ")
14       end
15
16       def add_primary_key(table_name, column_name, _options = {})
17         execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(column_name)})"
18       end
19
20       def remove_primary_key(table_name)
21         execute "ALTER TABLE #{quote_table_name(table_name)} DROP PRIMARY KEY"
22       end
23
24       def alter_primary_key(table_name, new_columns)
25         execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_table_name(table_name + '_pkey')}"
26         execute "ALTER TABLE #{quote_table_name(table_name)} ADD PRIMARY KEY (#{quote_column_name(new_columns)})"
27       end
28
29       def create_enumeration(enumeration_name, values)
30         execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
31       end
32
33       def drop_enumeration(enumeration_name)
34         execute "DROP TYPE #{enumeration_name}"
35       end
36
37       def rename_enumeration(old_name, new_name)
38         execute "ALTER TYPE #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
39       end
40     end
41   end
42 end
43
44 ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(OpenStreetMap::ActiveRecord::AbstractAdapter)
45 ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(OpenStreetMap::ActiveRecord::PostgreSQLAdapter)