]> git.openstreetmap.org Git - rails.git/blob - lib/migrate.rb
Merge remote-tracking branch 'upstream/pull/3002'
[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         table_name = quote_table_name(table_name)
18         column_name = quote_column_name(column_name)
19
20         execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{column_name})"
21       end
22
23       def remove_primary_key(table_name)
24         table_name = quote_table_name(table_name)
25
26         execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
27       end
28
29       def alter_primary_key(table_name, new_columns)
30         constraint_name = quote_table_name("#{table_name}_pkey")
31         table_name = quote_table_name(table_name)
32         new_columns = quote_column_name(new_columns)
33
34         execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint_name}"
35         execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{new_columns})"
36       end
37
38       def create_enumeration(enumeration_name, values)
39         execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
40       end
41
42       def drop_enumeration(enumeration_name)
43         execute "DROP TYPE #{enumeration_name}"
44       end
45
46       def rename_enumeration(old_name, new_name)
47         old_name = quote_table_name(old_name)
48         new_name = quote_table_name(new_name)
49
50         execute "ALTER TYPE #{old_name} RENAME TO #{new_name}"
51       end
52     end
53   end
54 end
55
56 ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(OpenStreetMap::ActiveRecord::AbstractAdapter)
57 ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(OpenStreetMap::ActiveRecord::PostgreSQLAdapter)