Skip to content

Instantly share code, notes, and snippets.

@eggplants
Last active May 24, 2022 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eggplants/496f79eb260af7dc40acf34898913564 to your computer and use it in GitHub Desktop.
Save eggplants/496f79eb260af7dc40acf34898913564 to your computer and use it in GitHub Desktop.
Convert mermaid definition of models into PosgresSQL schema
#!/usr/bin/env bash
if [[ "$@" =~ -h ]]; then
echo "Convert mermaid definition of models into PosgresSQL schema"
echo "Usage: $0 <mermaid model definition>"
exit 0
fi
if ! command -v awk sed &>/dev/null; then
echo "Install: awk, sed" >&2
exit 1
fi
if ! [ -f "$1" ]; then
echo "$0: '$1' is not found">&2
exit 1
fi
awk '
/{/{
print "CREATE TABLE "tolower($1)"("
}
$2!="{"&&NF==2{
switch($1){
case "string":
$1="text"
break
}
print " "tolower($2)" "$1($2=="id" ? " PRIMARY KEY" : $2~"_id" ? " NOT NULL" : "")","
}
NF==1{
print");"
}
' "$1" | sed -z 's/,\n)/\n)/g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment