2 module ConnectionAdapters
3 class PostgreSQLAdapter
5 alias_method :old_add_column_options!, :add_column_options!
7 def add_column_options!(sql, options)
8 sql << " UNSIGNED" if options[:unsigned]
9 old_add_column_options!(sql, options)
10 sql << " #{options[:options]}"
14 module SchemaStatements
15 def quote_column_names(column_name)
16 Array(column_name).map { |e| quote_column_name(e) }.join(", ")
19 def add_primary_key(table_name, column_name, options = {})
20 column_names = Array(column_name)
21 quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
22 execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{quoted_column_names})"
25 def remove_primary_key(table_name)
26 execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
29 def add_foreign_key(table_name, column_name, reftbl, refcol = nil)
30 execute "ALTER TABLE #{table_name} ADD " +
31 "FOREIGN KEY (#{quote_column_names(column_name)}) " +
32 "REFERENCES #{reftbl} (#{quote_column_names(refcol || column_name)})"
35 def remove_foreign_key(table_name, column_name, reftbl, refcol = nil)
36 execute "ALTER TABLE #{table_name} DROP " +
37 "CONSTRAINT #{table_name}_#{column_name[0]}_fkey"
40 # alias_method :old_options_include_default?, :options_include_default?
42 # def options_include_default?(options)
43 # return false if options[:options] =~ /AUTO_INCREMENT/i
44 # return old_options_include_default?(options)
48 alias_method :old_native_database_types, :native_database_types
50 def native_database_types
51 types = old_native_database_types
52 types[:double] = { :name => "double precision" }
53 types[:integer_pk] = { :name => "serial PRIMARY KEY" }
54 types[:bigint_pk] = { :name => "bigserial PRIMARY KEY" }
55 types[:bigint_pk_64] = { :name => "bigserial PRIMARY KEY" }
56 types[:bigint_auto_64] = { :name => "bigint" } #fixme: need autoincrement?
57 types[:bigint_auto_11] = { :name => "bigint" } #fixme: need autoincrement?
58 types[:bigint_auto_20] = { :name => "bigint" } #fixme: need autoincrement?
59 types[:four_byte_unsigned] = { :name => "bigint" } # meh
60 types[:inet] = { :name=> "inet" }
62 enumerations.each_key do |e|
63 types[e.to_sym]= { :name => e }
70 return { :id => false, :force => true, :options => ""}
74 return { :id => false, :force => true, :options => ""}
81 def change_engine (table_name, engine)
84 def add_fulltext_index (table_name, column)
85 execute "CREATE INDEX #{table_name}_#{column}_idx on #{table_name} (#{column})"
89 @enumerations ||= Hash.new
92 def create_enumeration(enumeration_name, values)
93 enumerations[enumeration_name] = values
94 execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
97 def drop_enumeration(enumeration_name)
98 execute "DROP TYPE #{enumeration_name}"
99 enumerations.delete(enumeration_name)
102 def rename_enumeration(old_name, new_name)
103 execute "ALTER TYPE #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
106 def alter_primary_key(table_name, new_columns)
107 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{table_name}_pkey"
108 execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{new_columns.join(',')})"
111 def interval_constant(interval)
112 "'#{interval}'::interval"
115 def add_index(table_name, column_name, options = {})
116 column_names = Array(column_name)
117 index_name = index_name(table_name, :column => column_names)
119 if Hash === options # legacy support, since this param was a string
120 index_type = options[:unique] ? "UNIQUE" : ""
121 index_name = options[:name] || index_name
122 index_method = options[:method] || "BTREE"
127 quoted_column_names = column_names.map { |e| quote_column_name(e) }
128 if Hash === options and options[:lowercase]
129 quoted_column_names = quoted_column_names.map { |e| "LOWER(#{e})" }
131 if Hash === options and options[:columns]
132 quoted_column_names = quoted_column_names + Array[options[:columns]]
134 quoted_column_names = quoted_column_names.join(", ")
136 execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} USING #{index_method} (#{quoted_column_names})"
139 def rename_index(table_name, old_name, new_name)
140 execute "ALTER INDEX #{quote_table_name(old_name)} RENAME TO #{quote_table_name(new_name)}"