-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_diagrams.sh
executable file
·138 lines (120 loc) · 2.53 KB
/
generate_diagrams.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
schema="ez"
user="admin"
pass="publish"
host="localhost"
db="mysql"
out="output"
driver="lib/mysql-connector-java-5.1.13-bin.jar"
sp="lib/schemaSpy_5.0.0.jar"
meta="ez_dbmeta.xml"
fkgen="sql/fkeys_generated.sql"
fkman="sql/fkeys_manual.sql"
java=`which java`
mysql=""
sspy=""
sqlsetup()
{
mysql="mysql -p$pass -u $user --host=$host $schema"
}
cleanup()
{
sqlsetup
echo "Removing all foreign keys from db"
fksQueryResult=`echo \
"SELECT TABLE_NAME, CONSTRAINT_NAME" \
"FROM information_schema.TABLE_CONSTRAINTS" \
"WHERE CONSTRAINT_TYPE='FOREIGN KEY' AND TABLE_SCHEMA='$schema';"\
| $mysql`
declare -a fks
fks=($fksQueryResult)
for((i=2;i<${#fks[@]};i+=2)); do
iinc=i+1
tableName=${fks[$i]}
keyName=${fks[$iinc]}
echo " Dropping $tableName: $keyName"
echo "ALTER TABLE $tableName"\
"DROP FOREIGN KEY $keyName;"\
| $mysql
done
}
addkeys()
{
sqlsetup
echo "Adding foreign keys from $fkgen"
$mysql < $fkgen
if [ $? -ne 0 ] ; then exit 1 ; fi
echo "Adding foreign keys from $fkman"
$mysql < $fkman
if [ $? -ne 0 ] ; then exit 1 ; fi
}
diagrams()
{
sqlsetup
echo "Running $sp"
$java -jar $sp -host $host -t $db -db $schema -u $user -p $pass -o $out -dp $driver -meta $meta -hq -noads -norows $sspy
if [ $? -ne 0 ] ; then exit 1 ; fi
}
usage()
{
cat << EOF
usage: $0 options
Manage foreign keys and create diagrams with schemaSpy
Note that options depends on order, so for example
-x: before -d
-n: before -c
OPTIONS:
-h Show this message
-u Override default user name ($user)
-p Override default password ($pass)
-s Override default hostname ($host)
-n Override default database name/schema ($schema)
-x Additional options to schemaSpy in quoted string
-r Removes all foreign keys from db
-c Creates foreign keys to db
-d Create diagrams with schemaSpy
-a Run all: rcd
EOF
}
while getopts "hu:p:s:n:x:rcda" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
u)
user=$OPTARG
;;
p)
pass=$OPTARG
;;
s)
host=$OPTARG
;;
n)
schema=$OPTARG
;;
x)
sspy=$OPTARG
;;
r)
cleanup
;;
c)
addkeys
;;
d)
diagrams
;;
a)
cleanup
addkeys
diagrams
;;
?)
usage
exit
;;
esac
done