Module: Foreigner::ConnectionAdapters::PostgreSQLAdapter

Includes:
Sql2003
Defined in:
lib/foreigner/connection_adapters/postgresql_adapter.rb

Instance Method Summary collapse

Methods included from Sql2003

#add_foreign_key, #supports_foreign_keys?

Instance Method Details

#foreign_keys(table_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/foreigner/connection_adapters/postgresql_adapter.rb', line 16

def foreign_keys(table_name)
  fk_info = select_all %{
    SELECT tc.constraint_name as name
          ,ccu.table_name as to_table
          ,ccu.column_name as primary_key
          ,kcu.column_name as column
          ,rc.delete_rule as dependency
    FROM information_schema.table_constraints tc
    JOIN information_schema.key_column_usage kcu
    USING (constraint_catalog, constraint_schema, constraint_name)
    JOIN information_schema.referential_constraints rc
    USING (constraint_catalog, constraint_schema, constraint_name)
    JOIN information_schema.constraint_column_usage ccu
    USING (constraint_catalog, constraint_schema, constraint_name)
    WHERE tc.constraint_type = 'FOREIGN KEY'
      AND tc.constraint_catalog = '#{@config[:database]}'
      AND tc.table_name = '#{table_name}'
  }
  
  fk_info.map do |row|
    options = {:column => row['column'], :name => row['name'], :primary_key => row['primary_key']}

    if row['dependency'] == 'CASCADE'
      options[:dependent] = :delete
    elsif row['dependency'] == 'SET NULL'
      options[:dependent] = :nullify
    end
    ForeignKeyDefinition.new(table_name, row['to_table'], options)
  end
end

#remove_foreign_key(table, options) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/foreigner/connection_adapters/postgresql_adapter.rb', line 6

def remove_foreign_key(table, options)
  if Hash === options
    foreign_key_name = foreign_key_name(table, options[:column], options)
  else
    foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
  end

  execute "ALTER TABLE #{quote_table_name(table)} DROP CONSTRAINT #{quote_column_name(foreign_key_name)}"
end