Regenerate ERD after rake db:migrate

403 Views Asked by At

I am using http://rails-erd.rubyforge.org/ to generate an ERD - the output is a very nice diagram of my project's object model. There is also a rake task to generate the ERD, generate_erd, that I would like to have invoked automatically after I run rake db:migrate. How do I do that?

1

There are 1 best solutions below

0
On

The given link by @MaxWilliams is a helpful but I don't think any of those answers quite do what you want. I found this article on Rake Task Overwriting. It's from 2008, but I tried this out and it worked.

I created another .rake file (for organization) and just happened to call mine migrate_and_generate_erb.rake but name it whatever you want.

Inside I just had this:

namespace :db do
  task :migrate do
    Rake::Task["erd"].invoke
  end
end

Basically, according to the article, Rake just keeps appending code implementation to the task if it's already defined.

Now running rake db:migrate also generated me my ERD.

Careful: You'll also want to do the same for db:rollback so that rolling back a migration also updates your ERD.

One last note: consider also just aliasing this (shell command), just in case you'd ever want to run the migrate without generate the ERD, or use environment variables along with your new Rake task.