1 if defined?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
5 def quote_column_name(name)
6 Array(name).map { |n| super(n) }.join(", ")
10 module SchemaStatements
11 def add_primary_key(table_name, column_name, options = {})
12 constraint_name = "#{table_name}_pkey"
14 options = options.merge(:name => constraint_name, :unique => true)
16 add_index(table_name, column_name, **options)
17 set_primary_key table_name, constraint_name
20 def remove_primary_key(table_name)
21 constraint_name = quote_table_name("#{table_name}_pkey")
22 table_name = quote_table_name(table_name)
24 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint_name}"
27 def alter_primary_key(table_name, column_name, options = {})
28 constraint_name = "#{table_name}_pkey"
29 tmp_constraint_name = "#{table_name}_pkey_tmp"
31 options = options.merge(:name => tmp_constraint_name, :unique => true)
33 add_index(table_name, column_name, **options)
34 remove_primary_key table_name
35 set_primary_key table_name, tmp_constraint_name
36 rename_index table_name, tmp_constraint_name, constraint_name
39 def set_primary_key(table_name, constraint_name)
40 constraint_name = quote_table_name(constraint_name)
41 table_name = quote_table_name(table_name)
43 execute "ALTER TABLE #{table_name} ADD PRIMARY KEY USING INDEX #{constraint_name}"
46 def create_enumeration(enumeration_name, values)
47 execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
50 def drop_enumeration(enumeration_name)
51 execute "DROP TYPE #{enumeration_name}"
54 def rename_enumeration(old_name, new_name)
55 old_name = quote_table_name(old_name)
56 new_name = quote_table_name(new_name)
58 execute "ALTER TYPE #{old_name} RENAME TO #{new_name}"
64 ActiveRecord::ConnectionAdapters::PostgreSQL::Quoting.prepend(OpenStreetMap::PostgreSQL::Quoting)
65 ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.prepend(OpenStreetMap::PostgreSQL::SchemaStatements)