SchemaSpy でDB定義書とER図を生成する

2019/01/07   #DB 
このエントリーをはてなブックマークに追加

SchemaSpy はDB定義書/ER図生成ツールで、結果は html で出力してくれる。
公式のサンプル をみればイメージがつくと思う。

なお、Java 製であり、Java8 以上が必要です。

Install SchemaSpy

インストールといっても jar ファイルを落としてきてどこかに置くだけ。
jar ファイルは GitHub にある。 Releases · schemaspy/schemaspy

Install Graphvis

ER図を描画するのに Graphvis が必要。
バージョン 6.1.0 からは vis.js が組み込みで入るから Graphvis は不要になるっぽい が、現時点では 6.0.0 なのでインストールが必要になる。
サクッと Homebrew で入れてしまう。

$ brew install graphviz --with-librsvg --with-pango

--with-librsvg --with-pango が必要なので注意だ。

設定ファイルをつくる

schemaspy.jar への引数パラメータでいろいろ渡せるが、プロパティファイルに記述しておいてそれを読み込ませるという方法もある。
DB定義書/ER図をつくりたいシチュエーションにおいてはだいたい決まったデータベースに対して行いたいだろうから設定ファイルとして書いておくほうがよさそうなので書いておく。

以下は PostgreSQL の例。
いまいちデータベースの種類になんて書いたらいいかわからないのだが、
PostgreSQL の場合は pgsql と書けばよいみたいです。

# type of database. Run with -dbhelp for details
schemaspy.t=pgsql
# optional path to alternative jdbc drivers.
schemaspy.dp=postgresql-42.2.2.jar
# database properties: host, port number, name user, password
schemaspy.host=localhost
schemaspy.port=5432
schemaspy.db=hoge
schemaspy.u=foo
schemaspy.p=bar
# output dir to save generated files
schemaspy.o=path/to/output
# db scheme for which generate diagrams
schemaspy.s=public

schemaspy.properties として保存。

schemaspy を実行してDB定義書/ER図を出力

では、schemaspy を実行してDB定義書/ER図を出力してみる。

$ java -jar schemaspy-6.0.0.jar -configFile=schemaspy.properties
  ____       _                          ____
 / ___|  ___| |__   ___ _ __ ___   __ _/ ___| _ __  _   _
 \___ \ / __| '_ \ / _ \ '_ ` _ \ / _` \___ \| '_ \| | | |
  ___) | (__| | | |  __/ | | | | | (_| |___) | |_) | |_| |
 |____/ \___|_| |_|\___|_| |_| |_|\__,_|____/| .__/ \__, |
                                             |_|    |___/

                                              6.0.0

SchemaSpy generates an HTML representation of a database schema's relationships.
SchemaSpy comes with ABSOLUTELY NO WARRANTY.
SchemaSpy is free software and can be redistributed under the conditions of LGPL version 3 or later.
http://www.gnu.org/licenses/

INFO  - Starting Main v6.0.0 on hogehoge.local with PID 5740 (/Users/hogehoge/foo/schemaspy/schemaspy-6.0.0.jar started by hogehoge in /Users/hogehoge/foo/schemaspy)
INFO  - The following profiles are active: default
INFO  - Found configuration file: schemaspy.properties
INFO  - Started Main in 1.687 seconds (JVM running for 2.307)
INFO  - Starting schema analysis
INFO  - Connected to PostgreSQL - 9.6.8
INFO  - Gathering schema details
Gathering schema details................................................(2sec)
Connecting relationships................................................(1sec)
Writing/graphing summary.INFO  - Gathered schema details in 1 seconds
INFO  - Writing/graphing summary
..............(7sec)
Writing/diagramming detailsINFO  - Completed summary in 7 seconds
INFO  - Writing/diagramming details
.............................................(49sec)
Wrote relationship details of 45 tables/views to directory 'path/to/output' in 61 seconds.
View the results by opening path/to/output/index.html
INFO  - Wrote table details in 49 seconds
INFO  - Wrote relationship details of 45 tables/views to directory 'path/to/output' in 61 seconds.
INFO  - View the results by opening path/to/output/index.html

指定した出力先にいろいろファイルが出力されているので index.html を開いて結果をみてみましょう。

Docker イメージも用意されている

CircleCI の Artifacts に添付する例

  document:
    docker:
    - image: schemaspy/schemaspy:snapshot
      user: root
    - image: postgresのイメージなど...
    steps:
    - attach_workspace:
        at: .
    - run:
        name: install openssl psql
        command: |
          apk update
          apk add openssl postgresql-client
    - run:
        name: install dockerize
        command: |
          wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
        environment:
          DOCKERIZE_VERSION: v0.6.1
    - run: dockerize -wait tcp://localhost:5432 -timeout 1m
    - run:
        name: create table
        command: |
          psql -h 127.0.0.1 -U user -d pass -a -f /root/project/sql/ddl.sql
    - run: cd / && /entrypoint.sh -t pgsql -host 127.0.0.1 -port 5432 -s public -u user -p password -db dbname -hq
    - run: cd / && tar czf reports.tar.gz output && mv reports.tar.gz output
    - store_artifacts:
        path: /output
        destination: reports

ちなみに、上記のように毎回 schemaspy のイメージに psql やらを加えるものアレなので、 schemaspy + psql の Docker イメージを以下につくっている。
roundrop/docker-schemaspy-with-pgclient: https://hub.docker.com/r/roundrop/schemaspy-with-pgclient
image: roundrop/schemaspy-with-pgclient