Skip to content

Instantly share code, notes, and snippets.

@briandunn
Created October 25, 2018 17:13
Show Gist options
  • Save briandunn/e3e061dc3b1b4e7a4fc64091c7ca8549 to your computer and use it in GitHub Desktop.
Save briandunn/e3e061dc3b1b4e7a4fc64091c7ca8549 to your computer and use it in GitHub Desktop.
task graph: :environment do
result = ActiveRecord::Base.connection.execute <<~SQL
select source.relname as source, dest.relname as dest
from pg_catalog.pg_constraint as fk
join pg_catalog.pg_class as source on source.oid = fk.conrelid
join pg_catalog.pg_class as dest on dest.oid = fk.confrelid
where contype = 'f'::char
SQL
relations = result.map(&:symbolize_keys).reduce({}) do |h, source:, dest:|
h.merge(source => [dest]) { |_, a, b| a + b }
end
puts <<~DOT
digraph fks {
\tlayout=neato;
\tnodesep=1;
\tnode [shape=box];
#{relations.map { |table, refs| "\t#{table} -> {#{refs.join ' '}}" }.join ";\n"}
}
DOT
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment