-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemaspy
executable file
·113 lines (98 loc) · 3.01 KB
/
schemaspy
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
#!/usr/bin/env bash
set -u
set -e
set -o noclobber
# 先ほどJARファイルを配置したディレクトリ
JAR_LIB_DIR=$HOME/local/lib
opts='h:u:p:d:t:'
function usage
{
echo "* Usage:"
echo " ${0} [-h DB_HOST] [-u DB_USER] [-p DB_PASSWORD] -t DB_TYPE -d DB_NAME"
echo "* DB_TYPE"
echo " - mysql"
echo " - pgsql"
echo " - ..."
exit 1
}
# ここでは "PostgreSQL" に特化させました
DB_TYPE=""
# 接続対象DBホスト
DB_HOST=localhost
# 接続対象DB名
DB_NAME=""
# 接続対象DBユーザ
DB_USER=$USER
# 接続対象DBパスワード
DB_PASS=""
while getopts ":${opts}" opt ; do
case "${opt}" in
h)
DB_HOST="${OPTARG}"
;;
u)
DB_USER="${OPTARG}"
;;
p)
DB_PASS="${OPTARG}"
;;
d)
DB_NAME="${OPTARG}"
;;
t)
DB_TYPE="${OPTARG}"
;;
:|\?)
usage
;;
esac
done
# 最後に名前無し引数があった場合はDB_NAMEとして扱う
shift $(($OPTIND -1))
DB_NAME=${1:-$DB_NAME}
if [ -z ${DB_TYPE} ] || [ -z ${DB_HOST} ] || [ -z ${DB_USER} ] || [ -z ${DB_NAME} ]; then
usage
exit 1
fi
# 関連ライブラリの有無をチェックし、必要ならインストールします
(which yum > /dev/null) && (
(which java > /dev/null) || sudo yum install -y java-1.8.0-openjdk-devel
(which dot > /dev/null) || sudo yum install -y 'graphviz*'
(which wget > /dev/null) || sudo yum install -y 'wget'
)
(
[ ! -d "${JAR_LIB_DIR}" ] && mkdir -p "${JAR_LIB_DIR}"; cd "${JAR_LIB_DIR}"
[ -f "${JAR_LIB_DIR}/schemaSpy_5.0.0.jar" ] || wget http://jaist.dl.sourceforge.net/project/schemaspy/schemaspy/SchemaSpy%205.0.0/schemaSpy_5.0.0.jar
case ${DB_TYPE} in
"pgsql")
[ -f "${JAR_LIB_DIR}/postgresql-9.4.1212.jar" ] || wget https://jdbc.postgresql.org/download/postgresql-9.4.1212.jar
;;
"mysql")
[ -f "${JAR_LIB_DIR}/mysql-connector-java-5.1.40-bin.jar" ] || wget http://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.40.tar.gz
tar xf mysql-connector-java-5.1.40.tar.gz; cp mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar mysql-connector-java-5.1.40-bin.jar
;;
"sqlite")
[ -f "${JAR_LIB_DIR}/sqlite-jdbc-3.15.1.jar" ] || wget https://bitbucket.org/xerial/sqlite-jdbc/downloads/sqlite-jdbc-3.15.1.jar
;;
esac
)
# ER図生成
[ ! -d "./${DB_NAME}" ] && mkdir -p "./${DB_NAME}"
JAR_LIB_PATH=${JAR_LIB_PATH:-$(ls ${JAR_LIB_DIR}/*.jar| grep -v schemaSpy| xargs echo| sed "s/ /:/g")}
echo ${JAR_LIB_PATH}
# -hq :ハイクオリティモード。出力結果がきれいになる!
# -noimplied :「外部キーがない既存テーブル間の関連を推測し、ER図に反映してくれる」機能をOFFにする
java \
-jar "${JAR_LIB_DIR}/schemaSpy_5.0.0.jar" \
-hq \
-noimplied \
-o "./${DB_NAME}" \
-charset utf-8 \
-dp "${JAR_LIB_PATH}" \
-t "${DB_TYPE}" \
-host "${DB_HOST}" \
-s "public" \
-db "${DB_NAME}" \
-u "${DB_USER}" \
-p "${DB_PASS}" \
;