Created
October 25, 2018 17:13
-
-
Save briandunn/e3e061dc3b1b4e7a4fc64091c7ca8549 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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