LoginSignup
4
3

More than 5 years have passed since last update.

Railsでmigrate時にER図を自動生成する設定

Last updated at Posted at 2018-10-06

目的

  • ドキュメントとシステムが乖離する要因を消す。
  • 視覚的に把握する。
  • 共有の手間をなくす。

手順

  1. graphvizをインストールする
  2. gemを指定する
  3. タスクを定義する

1. graphvizをインストールする

# ディストリビューションに合わせて
yum install graphviz
 or
apt-get install graphviz

2. gemを指定する

Gemfile

group :development do
  gem 'rails-erd'
end

3. タスクを定義する

Rakefile
require_relative 'config/application'

Rails.application.load_tasks

# migrateのタスクをフックする
Rake::Task['db:migrate'].enhance do
  if Rails.env.development?
    Rake::Task[:after_migrate].invoke
  end
end

# migrateの後のタスク
task :after_migrate do
  Rake::Task[:create_erd].invoke
end

# ER図を作成
task :create_erd do
  # attributes=foreign_keys,primary_keys,timestamp (属性は主キー、外部キー、タイムスタンプを表示)
  # sort=false (カラム名をアルファベット順にしない)
  # filename=hage-erd (ファイル名)
  # filetype=png (ファイル拡張子)
  sh 'bin/rake erd attributes=foreign_keys,primary_keys,content,timestamp sort=false filename=hage-erd filetype=png'
end

実行する

rails db:migrate
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3