5

Defining an existing rake task again appends to the original, but I'd like to prepend to the db:migrate task. I want to raise an error beforehand in some cases. Is there a good way to prepend to an existing rake task?

Qaz
  • 1,556
  • 2
  • 20
  • 34

2 Answers2

11

Try adding a db:custom task on 'db' namespace and invoke db:migrate with enhance method

# add your custom code on db:custom 
namespace 'db' do
  task 'custom' do
    puts "do custom db stuff"
  end
end

# invoke db:migrate 
Rake::Task['db:migrate'].enhance [:custom]
sa77
  • 3,563
  • 3
  • 24
  • 37
3

Might be better to define your own task and call db:migrate inside.

namespace :custom_db do
  desc 'migrate db if condition true'
  task :migrate do
    if true #your condition
      Rake::Task['db:migrate'].invoke
    else
      #process errors
    end
  end
end
EJAg
  • 3,210
  • 2
  • 14
  • 22
  • 2
    Unfortunately, I can't count on everyone who is or will be in my organization to know about `:custom_db` and remember to use it. For an individual or a small team, though, this is a good practical approach. – Qaz Dec 13 '17 at 02:05