I have a Rails 2.2 project in which I want to override the functionality of the rake db:test:prepare
task. I thought this would work, but it doesn't:
#lib/tasks/db.rake
namespace :db do
namespace :test do
desc "Overridden version of rails' standard db:test:prepare task since the schema dump used in that can't handle DB enums"
task :prepare => [:environment] do
puts "doing db:structure:dump"
Rake::Task['db:structure:dump'].invoke
puts "doing db:test:clone_structure"
Rake::Task['db:test:clone_structure'].invoke
end
end
end
I get the standard task's behaviour. If I change the name of the task to :prepare2
and then do rake db:test:prepare2
, then it works fine. The natural conclusion I draw from this is that my rake tasks are being defined before the built-in Rails ones, so mine is overridden by the standard :prepare
task.
Can anyone see how I can fix this? I'd rather override it than have to use a new task. Thanks, max
i was trying to do the same thing and came across this at codingspree.net http://j.mp/1ICZrm5
Create a new project.rake file at lib/tasks/, and paster below code into it.
inspired by Krasimir Angelov's blog
If you define a rake task that already exists, its execution gets appended to the original task's execution; both tasks will be executed.
If you want to redefine a task you need to clear the original task first:
It's also useful to note that once a task has been executed in rake, it won't execute again even if you call it again. This is by design but you can call
.reset
on a task to allow it to be run again.You have to remove the default task before adding your own:
A fairly popular idiom is to create a convenience method called
remove_task
like so:(Source:
drnic/newgem
)