1 if defined?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
4 module PostgreSQLAdapter
5 def quote_column_name(name)
6 Array(name).map { |n| super(n) }.join(", ")
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)
13 execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{column_name})"
16 def remove_primary_key(table_name)
17 table_name = quote_table_name(table_name)
19 execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
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)
27 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint_name}"
28 execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{new_columns})"
31 def create_enumeration(enumeration_name, values)
32 execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
35 def drop_enumeration(enumeration_name)
36 execute "DROP TYPE #{enumeration_name}"
39 def rename_enumeration(old_name, new_name)
40 old_name = quote_table_name(old_name)
41 new_name = quote_table_name(new_name)
43 execute "ALTER TYPE #{old_name} RENAME TO #{new_name}"
49 ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(OpenStreetMap::ActiveRecord::PostgreSQLAdapter)