How to automatically generate ER diagram when migrating with Rails6

Introduction

We have summarized how to automatically generate ER diagram in Rails6. In the case of Rails6, Zeitwerk is used by default for autoloading files, but due to this, ER diagram could not be generated well, so the workaround is also described.

Development environment

Rails: 6.0.3 Ruby:2.7.1 Macbook Pro Graphviz GraphQL (This was due to the automatically generated file.)

ER diagram settings

Install Gem.

Gemfile


group :development do
  gem 'rails-erd'
end

Set to gitignore if necessary.

.gitignore


*.dot

You can generate an ER diagram with the following command.

$ bundle exec erd

Auto-generated settings

To automatically generate ER diagram at the time of migration, set in Rakefile. If you don't need automatic generation, you don't need to mention it here.

Rakefile


#Hook the task of migrate
Rake::Task['db:migrate'].enhance do
  if Rails.env.development?
    Rake::Task[:after_migrate].invoke
  end
end

#Tasks after migrate
task after_migrate: :environment do
  Rake::Task[:create_erd].invoke
end

#Create ER diagram
task create_erd: :environment do
  # attributes=foreign_keys,primary_keys,timestamps (Attributes display primary key, foreign key, timestamp)
  # sort=false (Do not sort column names alphabetically)
  # filename=loof-api-server-erd (file name)
  # filetype=dot (File extension)
  sh 'bundle exec erd --attributes=foreign_keys,primary_keys,content,timestamps --sort=false --filename=hogehoge --filetype=dot'
end

Error countermeasures

Not recommended measures

When I stopped using zeitwerk, which autoloads files, and changed it to classic, I was able to automatically generate it, but Rspec did not pass.

config/application.rb


config.autoloader = :classic

About zeitwerk uninitialized constant XXX (NameError) error [Rails Guide 2 Enable Zeitwerk Mode](https://railsguides.jp/autoloading_and_reloading_constants.html#zeitwerk%E3%83%A2%E3%83%BC%E3%83%89%E3%82%92% E6% 9C% 89% E5% 8A% B9% E3% 81% AB% E3% 81% 99% E3% 82% 8B) [When Rails Guide 11 Zeitwerk is not used](https://railsguides.jp/autoloading_and_reloading_constants.html#zeitwerk%E3%82%92%E4%BD%BF%E3%82%8F%E3%81%AA%E3 % 81% 84% E5% A0% B4% E5% 90% 88)

Measures taken

When I generated the ER diagram, I got the following error.

Terminal


$ bundle exec erd
Failed: Zeitwerk::NameError: expected file /app/graphql/interface_types/base_interface.rb to define constant Types::BaseInterface, but didn't

Apparently the module definition method is not appropriate. How to break Zeitwerk

The cause was the file automatically generated by GraphQL, so I changed it as follows.

base_interface.rb


module Types
  module BaseInterface
    include GraphQL::Schema::Interface
    field_class Types::BaseField 
  end 
end

After measures ↓

base_interface.rb


module Types::BaseInterface
  include GraphQL::Schema::Interface
  field_class Types::BaseField
end

Change other files as well.

base_scalar.rb


module Types
  class BaseScalar < GraphQL::Schema::Scalar
  end
end

After measures ↓

base_scalar.rb


class Types::BaseScalar < GraphQL::Schema::Scalar
end

When I generated the ER diagram again, the following error appeared this time.

Terminal


$ bundle exec erd
Failed: RuntimeError: Saving diagram failed!
Verify that Graphviz is installed and in your path, or use filetype=dot.

It is necessary to specify file as dot.

Terminal(Specify file)


$ bundle exec erd --filetype=dot

You have successfully generated an ER diagram!

At the end

Since Rails6 x GraphQL was used, the auroload could not be performed correctly, and the error occurred.

Recommended Posts

How to automatically generate ER diagram when migrating with Rails6
How to manually generate a JWT with Rails Knock
How to set environment variables when using Payjp with Rails
How to specify db when creating an app with rails
How to get along with Rails
[Rails] How to use rails console with docker
How to resolve errors when installing Rails 5.1.3
How to build Rails 6 environment with Docker
[Rails] How to write when making a subquery
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
How to log in automatically when Ubuntu restarts
How to automatically generate a constructor in Eclipse
How to write Rails
How to uninstall Rails
How to make batch processing with Rails + Heroku configuration
[Rails] How to search by multiple values ​​with LIKE
How to push an app developed with Rails to Github
How to delete a new_record object built with Rails
How to make an almost static page with rails
Automatically deploy to WildFly with Jenkins when SVN commits
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
How to query Array in jsonb with Rails + postgres
How to get started with creating a Rails app
[Docker + Rails] How to deal with Rails server startup failure
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
[rails] How to post images
[Rails] How to use enum
What to do when you launch an application with rails
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
[Easy] How to automatically format Ruby erb file with vsCode
[With back tricks] How to introduce React to the simplest Rails
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
How to write and notes when migrating from VB to JAVA
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
Things to keep in mind when using Sidekiq with Rails
[Rails] How to use Scope
[Rails / RSpec] How to deal with element has zero size error
Is it possible to automatically generate Getters / Setters with Java Interface?
[Rails] How to introduce kaminari with Slim and change the design
How to install Pry after building Rails development environment with Docker
[Rails] How to avoid "Use hash rockets syntax" when executing Rubocop
What I was addicted to when implementing google authentication with rails