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
40
41
42
|
# 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
schemas = query_module.table_schemas(in_format: :hash)
foreign_keys = query_module.foreign_keys(in_format: :hash)
tables.reduce([]) do |agg, table|
schema = schemas.select { |row| row.fetch("table_name") == table }
fk_columns = foreign_keys.select { |row| row.fetch("table_name") == table }
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.none? { |row| row.fetch("column_name") == column_name }
agg.push(
{
table: table,
column_name: column_name,
}
)
end
end
agg
end
end
|