Paladini
Paladini

Reputation: 4572

How do I execute a "rake task" after every db:reset

I'm working on a personal project and I have a strange doubt. At the localhost, I do rake db:reset almost every time and I always need to run another task named newsworker:create (in other words, I run rake newsworker:create).

My questions are:

  1. How can I execute a specific rake task after every rake db:reset?
  2. How can I generalize the above question to work with rake db:create and other tasks?

Thanks!

Upvotes: 2

Views: 562

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

In my opinion, the best way is to create your own rake task calling all the tasks needed (to avoid changing Rails/Rake basic tasks):

# lib/tasks/reset_and_create
namespace :database do
  desc 'Reset the database to a fresh and clean DB ready for use'
  task :reset_and_create do
    Rake::Task['db:reset'].invoke
    Rake::Task['newsworker:create'].invoke
    # if you need to pass arguments to your tasks, use:
    # Rake::Task['your_task'].invoke(your_arg, another_arg)
  end
end

And use it like this:

rake database:reset_and_create

Upvotes: 5

Related Questions