Class: RubyPgExtras::MissingFkConstraints

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pg_extras/missing_fk_constraints.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(table_name) ⇒ Object



5
6
7
# File 'lib/ruby_pg_extras/missing_fk_constraints.rb', line 5

def self.call(table_name)
  new.call(table_name)
end

Instance Method Details

#call(table_name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_pg_extras/missing_fk_constraints.rb', line 9

def call(table_name)
  tables = if table_name
      [table_name]
    else
      all_tables
    end

  tables.reduce([]) do |agg, table|
    foreign_keys_info = query_module.table_foreign_keys(args: { table_name: table }, in_format: :hash)
    schema = query_module.table_schema(args: { table_name: table }, in_format: :hash)

    fk_columns = schema.filter_map do |row|
      if DetectFkColumn.call(row.fetch("column_name"), all_tables)
        row.fetch("column_name")
      end
    end

    fk_columns.each do |column_name|
      if foreign_keys_info.none? { |row| row.fetch("column_name") == column_name }
        agg.push(
          {
            table: table,
            column_name: column_name,
          }
        )
      end
    end

    agg
  end
end