Module: SchemaPlus::ForeignKeys::Middleware::Dumper::Table

Defined in:
lib/schema_plus/foreign_keys/middleware/dumper.rb

Instance Method Summary collapse

Instance Method Details

#after(env) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/schema_plus/foreign_keys/middleware/dumper.rb', line 66

def after(env)
  dumped = {}
  env.table.columns.each do |column|
    if (foreign_key = env.dump.data.inline_fks[env.table.name].find(&its.column.to_s == column.name))
      column.options[:foreign_key] = {references: foreign_key.to_table}.merge foreign_key.options_for_dump
      dumped[foreign_key] = true
    end
    if (foreign_key = env.dump.data.backref_fks.values.flatten.find{|fk| fk.from_table.to_s == env.table.name && fk.column.to_s == column.name})
      column.comments << "foreign key references #{foreign_key.to_table.inspect} (below)"
    end
  end
  env.table.trailer += env.dump.data.inline_fks[env.table.name].map { |foreign_key|
    foreign_key.to_dump unless dumped[foreign_key] # just in case we missed any.  don't think it can happen
  }.compact.sort
  env.table.trailer += env.dump.data.backref_fks[env.table.name].map { |foreign_key|
    foreign_key.to_dump
  }.sort
end