ドキュメントのないアプリケーションを引き継ぐことが多く、最近はデータベースからER図を作ってドキュメントの整備を行なってます。
今回は、SchemaSpy を使うとその作業がとても楽になったためここにメモを残します。
SchemaSpy とは
DB から DB のドキュメント(ER図やテーブル/カラム一覧など)をHTML形式で出力する Java 製のツールです。
schemaspy.org
使い方
事前準備
java インストール
$ sudo apt install default-jdk
dot インストール
$ sudo apt-get install graphviz
SchemaSpy の作業
DB接続ドライバ取得
MySQLに接続する場合 % wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.20/mysql-connector-java-8.0.20.jar PostgreSQLに接続する場合 % wget https://jdbc.postgresql.org/download/postgresql-42.2.23.jar
chemaspy.properties 作成
SchemaSpy 実行
以下のコマンドを実行します。
$ sudo java -jar schemaspy-6.1.0.jar -dp mysql-connector-java-8.0.20.jar -s {{DB}} -rails ____ _ ____ / ___| ___| |__ ___ _ __ ___ __ _/ ___| _ __ _ _ \___ \ / __| '_ \ / _ \ '_ ` _ \ / _` \___ \| '_ \| | | | ___) | (__| | | | __/ | | | | | (_| |___) | |_) | |_| | |____/ \___|_| |_|\___|_| |_| |_|\__,_|____/| .__/ \__, | |_| |___/ 6.1.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.1.0 on {{server name}} with PID 426696 ({{path}}/schemaspy-6.1.0.jar started by root in {{path}}) INFO - The following profiles are active: default INFO - Found configuration file: schemaspy.properties INFO - Started Main in 1.311 seconds (JVM running for 1.769) INFO - Loaded configuration from schemaspy.properties INFO - Starting schema analysis INFO - Connected to MySQL - 8.0.29-0ubuntu0.20.04.3 INFO - Gathering schema details Gathering schema details...................................(0sec) Connecting relationships...................................(0sec) Writing/graphing summary.INFO - Gathered schema details in 0 seconds INFO - Writing/graphing summary INFO - Graphviz rendered set to '' ........(1sec) Writing/diagramming detailsINFO - Completed summary in 1 seconds INFO - Writing/diagramming details ................................(5sec) Wrote relationship details of 32 tables/views to directory 'analysis' in 8 seconds. View the results by opening analysis/index.html INFO - Wrote table details in 5 seconds INFO - Wrote relationship details of 32 tables/views to directory 'analysis' in 8 seconds. INFO - View the results by opening analysis/index.html
成果物
テーブル一覧
テーブル詳細
ER図