Azuki'sLog

都内で働くエンジニアのブログ!RubyとかPHPとか

PlantUMLでER図を書いてみる

プロジェクトでPlantUMLで図を書いている人が居るんです。
これが自分が過去感じて来た課題を解決してくれる物に見えたので自分でも書いてみる。

解決してくれそうと思っている課題

  • ER図やシーケンス図のバージョン管理
  • 他の仕組みとの連携
    • ER図からRailsのmigrationファイルを自動生成とか、その逆とか

Macで環境を作る

brew install graphviz
# brew cask install java
brew install plantuml

エラーになって 以下のメッセージが出たので、自分は上の brew cask install java を挟む事になった。

% brew install plantuml
Updating Homebrew...
plantuml: Java is required to install this formula.
JavaRequirement unsatisfied!
You can install with Homebrew Cask:
 brew cask install java
You can download from:
https://www.oracle.com/technetwork/java/javase/downloads/index.html
Error: An unsatisfied requirement failed this build.

とりあえず作ってみる

1. ディレクトリ作る

mkdir work/test_plant_uml

2. ソースを書いてみる

vi work/test_plant_uml/recipes.uml
--------
@startuml

package "cock" as cock {

  entity "recipes" {
    + id [PK]
    ==
    name:string
  }

  entity "recipe_ingredients" {
    + id [PK]
    ==
    #recipe_id:references
    #ingredient_id:references
    quantity:integer
  }

  entity "ingredients" {
    + id [PK]
    ==
    name:string
  }
}

recipes --{ recipe_ingredients
ingredients --{ recipe_ingredients

@enduml

3. 図を生成してみる plantuml コマンドに引数でファイルを渡すとpngを生成してくれる。

% plantuml work/test_plant_uml/recipes.uml
% ls work/test_plant_uml/        
recipes.png  recipes.uml

4. 出来た図
それっぽいのがとりあえずできた!

f:id:nyantech0207:20181224215457p:plain
recipes.png

デザインも変えられる

デザインも変えられる。 (参考: http://plantuml.com/skinparam

例えば以下のコードをumlファイルに追加して、図を生成し直す。

skinparam roundcorner 20
skinparam class {
  BackgroundColor PaleGreen
  ArrowColor SeaGreen
  BorderColor SpringGreen
}
skinparam stereotypeCBackgroundColor YellowGreen

f:id:nyantech0207:20181224215526p:plain
recipes_green.png

素敵!・・・とはこれはならなそうだけど、いい感じにできたら良さそう。