Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepend to existing rake task

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?

like image 567
Qaz Avatar asked Dec 13 '17 00:12

Qaz


2 Answers

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]
like image 119
sa77 Avatar answered Sep 19 '22 20:09

sa77


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
like image 34
EJAg Avatar answered Sep 20 '22 20:09

EJAg