#!/bin/bash query(){ # psql-gridware -A -F$'\t' -t -P pager=off -c "$@" # psql-gridware -A -F$'\t' -P pager=off -P title=off -c "$@" psql-gridware -A -F$'\t' -P pager=off -c "$@" } exec {in}<&0 pforkey(){ echo '>]' open_table=0 data=forkey } forkey(){ # ${constraint name} FOREIGN KEY (${Column}) REFERENCES ${table}(${Column}) read -ra line <<<"$Column" this_column=${line[3]%)} this_column=${this_column#(} other_table=${line[5]%%(*} other_column=${line[5]##*\(} other_column=${other_column%)} printf '%s:%s_out -> %s:%s_in\n' "$table" "$this_column" "$other_table" "$other_column" } record(){ if [[ $Nullable = 'not null' ]] then echo "$Column$Type" else echo "$Column$Type" fi } echo 'strict digraph db {' echo 'node[shape=plain]' query '\d' -t | cut -f 2,3 | grep table\$ | cut -f1 | while read -r table do printf '\e[1m%s\e[0m\n' "$table" >&2 data=record open_table=1 echo echo "$table [label=<" echo '
' while IFS=$'\t' read -r Column Type Collation Nullable Default do case $Column in Foreign-key\ constraints:) echo >&2 'foreign keys' data=pforkey ;; *:) echo >&2 '… skipping '"$Column" data=: esac $data done < <(query '\d '"$table" <&${in} | tail -n+3 | tr -d $'\r') [[ $open_table = 1 ]] && echo '
\\N
>]' done echo '}'