Last active
May 24, 2022 08:42
-
-
Save eggplants/496f79eb260af7dc40acf34898913564 to your computer and use it in GitHub Desktop.
Convert mermaid definition of models into PosgresSQL schema
This file contains hidden or 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
#!/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