]> git.openstreetmap.org Git - rails.git/blob - config/initializers/migrate.rb
Merge branch 'master' into feature/add-communities-page
[rails.git] / config / initializers / migrate.rb
1 if defined?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
2   module OpenStreetMap
3     module ActiveRecord
4       module PostgreSQLAdapter
5         def quote_column_name(name)
6           Array(name).map { |n| super(n) }.join(", ")
7         end
8
9         def add_primary_key(table_name, column_name, _options = {})
10           table_name = quote_table_name(table_name)
11           column_name = quote_column_name(column_name)
12
13           execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{column_name})"
14         end
15
16         def remove_primary_key(table_name)
17           table_name = quote_table_name(table_name)
18
19           execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
20         end
21
22         def alter_primary_key(table_name, new_columns)
23           constraint_name = quote_table_name("#{table_name}_pkey")
24           table_name = quote_table_name(table_name)
25           new_columns = quote_column_name(new_columns)
26
27           execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint_name}"
28           execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{new_columns})"
29         end
30
31         def create_enumeration(enumeration_name, values)
32           execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
33         end
34
35         def drop_enumeration(enumeration_name)
36           execute "DROP TYPE #{enumeration_name}"
37         end
38
39         def rename_enumeration(old_name, new_name)
40           old_name = quote_table_name(old_name)
41           new_name = quote_table_name(new_name)
42
43           execute "ALTER TYPE #{old_name} RENAME TO #{new_name}"
44         end
45       end
46     end
47   end
48
49   ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(OpenStreetMap::ActiveRecord::PostgreSQLAdapter)
50 end