Just wrap it in a begin/rescue/ensure/end
statement:
namespace :db do
def my_checks
...
end
task :migrate do
begin
# something that may raise error
rescue
# what to do if error, you can omit this if you don't care about error
ensure
my_checks
end
end
end
EDIT
I would do it like this:
namespace :db do
def my_checks
...
end
task :my_migrate do
begin
Rake::Task['db:migrate'].invoke
rescue
# what to do if error, you can omit this if you don't care about error
ensure
my_checks
end
end
end
EDIT 2
Okay, try:
def alias_task(name, old_name)
# from https://gist.github.com/raggi/232966
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end
alias_task 'db:old_migrate', 'db:migrate'
namespace :db do
def my_checks
puts 'ok'
end
task :migrate do
begin
Rake::Task["db:old_migrate"].execute
ensure
my_checks
end
end
end
EDIT 3
Okay, this should work, and is much more simple:
namespace :db do
def my_checks
puts 'ok'
end
task :other do
at_exit { my_checks }
end
end
Rake::Task['db:migrate'].enhance(['db:other'])
Use of at_exit
from a suggestion by the late Jim Weirich at https://www.ruby-forum.com/topic/61010.
See http://ruby-doc.org/stdlib-1.9.3/libdoc/rake/rdoc/Rake/Task.html for more about enhance
.