Reputation: 8782
I have appended a my_checks
method call to the rails db:migrate
invocation. The code below works great as long as db:migrate
does not throw an exception. However, if db:migrate
does throw an exception, the my_checks
method won't be executed.
Is there a way I can rewrite the code below to ensure that my_checks
gets executed no matter what happens in db:migrate
?
namespace :db do
def my_checks
...
end
task :migrate do
my_checks
end
end
Upvotes: 4
Views: 1312
Reputation: 7561
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
.
Upvotes: 5