Class: PGExaminer::Result::Constraint

Inherits:
Item
  • Object
show all
Defined in:
lib/pg_examiner/result/constraint.rb

Constant Summary collapse

FOREIGN_KEY_ACTIONS =
{
  "a" => "no action",
  "r" => "restrict",
  "c" => "cascade",
  "n" => "set null",
  "d" => "set default",
}.freeze
FOREIGN_KEY_MATCH_TYPES =
{
  "f" => "full",
  "p" => "partial",
  "s" => "simple",
}.freeze

Instance Attribute Summary

Attributes inherited from Item

#parent, #result, #row

Instance Method Summary collapse

Methods inherited from Item

#initialize, #inspect, #name, #oid

Methods inherited from Base

#==, #diff, #diffable_lists

Constructor Details

This class inherits a constructor from PGExaminer::Result::Item

Instance Method Details

#constrained_columnsObject



66
67
68
# File 'lib/pg_examiner/result/constraint.rb', line 66

def constrained_columns
  @constrained_columns ||= extract_array(row['conkey']).map{|n| parent.columns.find{|c| c.row['attnum'] == n}.name} if row['conkey']
end

#diffable_attrsObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pg_examiner/result/constraint.rb', line 6

def diffable_attrs
  {
    "name"          => "name",
    "contype"       => "constraint type",
    "condeferrable" => "constraint is deferrable",
    "condeferred"   => "constraint is initially deferred",
    "convalidated"  => "constraint is validated",
    "connoinherit"  => "constraint is not inheritable",
    "check_def"     => "check constraint definition",
  }
end

#diffable_methodsObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pg_examiner/result/constraint.rb', line 18

def diffable_methods
  {
    "type"                        => "type",
    "index"                       => "index",
    "foreign_table_name"          => "table referenced by foreign key",
    "constrained_columns"         => "local constrained columns",
    "foreign_constrained_columns" => "foreign constrained columns",
    "foreign_key_update_action"   => "foreign key on update action",
    "foreign_key_delete_action"   => "foreign key on delete action",
    "foreign_key_match_type"      => "foreign key match type",
  }
end

#foreign_constrained_columnsObject



70
71
72
# File 'lib/pg_examiner/result/constraint.rb', line 70

def foreign_constrained_columns
  @foreign_constrained_columns ||= extract_array(row['confkey']).map{|n| foreign_table.columns.find{|c| c.row['attnum'] == n}.name} if row['confkey']
end

#foreign_key_delete_actionObject



86
87
88
# File 'lib/pg_examiner/result/constraint.rb', line 86

def foreign_key_delete_action
  FOREIGN_KEY_ACTIONS.fetch(row['confdeltype']) if row['confdeltype'] != ' '
end

#foreign_key_match_typeObject



96
97
98
# File 'lib/pg_examiner/result/constraint.rb', line 96

def foreign_key_match_type
  FOREIGN_KEY_MATCH_TYPES.fetch(row['confmatchtype']) if row['confmatchtype'] != ' '
end

#foreign_key_update_actionObject



82
83
84
# File 'lib/pg_examiner/result/constraint.rb', line 82

def foreign_key_update_action
  FOREIGN_KEY_ACTIONS.fetch(row['confupdtype']) if row['confupdtype'] != ' '
end

#foreign_tableObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/pg_examiner/result/constraint.rb', line 55

def foreign_table
  if row['confrelid'] != '0'
    @foreign_table ||= begin
      # Look up the table, which may be outside our own schema.
      table_row = result.pg_class.find { |c| c['relkind'] == 'r' && c['oid'] == row['confrelid'] }
      schema = result.schemas.find { |s| s.oid == table_row['relnamespace'] }
      schema.tables.find { |t| t.name == table_row['name'] }
    end
  end
end

#foreign_table_nameObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pg_examiner/result/constraint.rb', line 39

def foreign_table_name
  if ft = foreign_table
    this_schema_name = parent.parent.name
    that_schema_name = ft.parent.name

    relative_schema =
      if this_schema_name == that_schema_name
        "(same schema)"
      else
        "#{that_schema_name} schema"
      end

    [relative_schema, ft.name]
  end
end

#indexObject



35
36
37
# File 'lib/pg_examiner/result/constraint.rb', line 35

def index
  @index ||= result.pg_index.find{|i| i['indexrelid'] == row['conindid']}['name'] if row['conindid'] != '0'
end

#typeObject



31
32
33
# File 'lib/pg_examiner/result/constraint.rb', line 31

def type
  @type ||= result.pg_type.find{|t| t['oid'] == row['contypid']}['name'] if row['contypid'] != '0'
end