2   module ConnectionAdapters
 
   3     module SchemaStatements
 
   4       def quote_column_names(column_name)
 
   5         Array(column_name).map { |e| quote_column_name(e) }.join(", ")
 
   8       def add_primary_key(table_name, column_name, options = {})
 
   9         column_names = Array(column_name)
 
  10         quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
 
  11         execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{quoted_column_names})"
 
  14       def remove_primary_key(table_name)
 
  15         execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
 
  18       def add_foreign_key(table_name, column_name, reftbl, refcol = nil)
 
  19         execute "ALTER TABLE #{table_name} ADD " +
 
  20           "FOREIGN KEY (#{quote_column_names(column_name)}) " +
 
  21           "REFERENCES #{reftbl} (#{quote_column_names(refcol || column_name)})"
 
  24       def remove_foreign_key(table_name, column_name, reftbl, refcol = nil)
 
  25         execute "ALTER TABLE #{table_name} DROP " +
 
  26           "CONSTRAINT #{table_name}_#{column_name[0]}_fkey"
 
  29       alias_method :old_options_include_default?, :options_include_default?
 
  31       def options_include_default?(options)
 
  32         return false if options[:options] =~ /AUTO_INCREMENT/i
 
  33         return old_options_include_default?(options)
 
  36       alias_method :old_add_column_options!, :add_column_options!
 
  38       def add_column_options!(sql, options)
 
  39         sql << " UNSIGNED" if options[:unsigned]
 
  40         old_add_column_options!(sql, options)
 
  41         sql << " #{options[:options]}"
 
  45     class PostgreSQLAdapter
 
  46       alias_method :old_native_database_types, :native_database_types
 
  48       def native_database_types
 
  49         types = old_native_database_types
 
  50         types[:double] = { :name => "double precision" }
 
  51         types[:integer_pk] = { :name => "serial PRIMARY KEY" }
 
  52         types[:bigint_pk] = { :name => "bigserial PRIMARY KEY" }
 
  53         types[:bigint_pk_64] = { :name => "bigserial PRIMARY KEY" }
 
  54         types[:bigint_auto_64] = { :name => "bigint" } #fixme: need autoincrement?
 
  55         types[:bigint_auto_11] = { :name => "bigint" } #fixme: need autoincrement?
 
  56         types[:bigint_auto_20] = { :name => "bigint" } #fixme: need autoincrement?
 
  57         types[:four_byte_unsigned] = { :name => "bigint" } # meh
 
  58         types[:inet] = { :name=> "inet" }
 
  60         enumerations.each_key do |e|
 
  61           types[e.to_sym]= { :name => e }
 
  68         return { :id => false, :force => true, :options => ""}
 
  72         return { :id => false, :force => true, :options => ""}
 
  79       def change_engine (table_name, engine)
 
  82       def add_fulltext_index (table_name, column)
 
  83         execute "CREATE INDEX #{table_name}_#{column}_idx on #{table_name} (#{column})"
 
  87         @enumerations ||= Hash.new
 
  90       def create_enumeration(enumeration_name, values)
 
  91         enumerations[enumeration_name] = values
 
  92         execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
 
  95       def drop_enumeration(enumeration_name)
 
  96         execute "DROP TYPE #{enumeration_name}"
 
  97         enumerations.delete(enumeration_name)
 
 100       def rename_enumeration(old_name, new_name)
 
 101         execute "ALTER TYPE #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
 
 104       def alter_primary_key(table_name, new_columns)
 
 105         execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{table_name}_pkey"
 
 106         execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{new_columns.join(',')})"
 
 109       def interval_constant(interval)
 
 110         "'#{interval}'::interval"
 
 113       def add_index(table_name, column_name, options = {})
 
 114         column_names = Array(column_name)
 
 115         index_name   = index_name(table_name, :column => column_names)
 
 117         if Hash === options # legacy support, since this param was a string
 
 118           index_type = options[:unique] ? "UNIQUE" : ""
 
 119           index_name = options[:name] || index_name
 
 120           index_method = options[:method] || "BTREE"
 
 125         quoted_column_names = column_names.map { |e| quote_column_name(e) }
 
 126         if Hash === options and options[:lowercase]
 
 127           quoted_column_names = quoted_column_names.map { |e| "LOWER(#{e})" }
 
 129         if Hash === options and options[:columns]
 
 130           quoted_column_names = quoted_column_names + Array[options[:columns]]
 
 132         quoted_column_names = quoted_column_names.join(", ")
 
 134         execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} USING #{index_method} (#{quoted_column_names})"
 
 137       def rename_index(table_name, old_name, new_name)
 
 138         execute "ALTER INDEX #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}"