module Fakey::PostgreSQLAdapter

Public Instance Methods

add_column_with_foreign_key(table_name, column_name, type, options = {}) click to toggle source

Adds a new column to the named table. See TableDefinition#column for details of the options you can use.

# File lib/fakey/postgresql_adapter.rb, line 13
def add_column_with_foreign_key(table_name, column_name, type, options = {})
  default = options[:default]
  notnull = options[:null] == false
  foreign_key = "REFERENCES #{quote_table_name(options[:references])}"
  foreign_key +="(#{quote_column_name(options[:referenced_column] || options[:primary_key])})" if options[:referenced_column] || options[:primary_key]

  # Add the column.
  execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])} #{foreign_key}")

  change_column_default(table_name, column_name, default) if options_include_default?(options)
  change_column_null(table_name, column_name, false, default) if notnull
end
add_foreign_key(to_table, column, options={}) click to toggle source
# File lib/fakey/postgresql_adapter.rb, line 3
def add_foreign_key(to_table, column, options={})
  to_table = options[:references] if options[:references]
  res = "FOREIGN KEY(#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}"
  referenced_column = options[:referenced_column] || options[:primary_key]
  res += "(#{referenced_column})" if referenced_column
  res
end