覆盖 rails 的默认 rake 任务 [英] Overriding rails' default rake tasks
问题描述
我有一个 Rails 2.2 项目,我想在其中覆盖 rake db:test:prepare
任务的功能.我认为这会起作用,但它不起作用:
#lib/tasks/db.rake命名空间 :db 做命名空间:测试做descrails 的标准 db:test:prepare 任务的覆盖版本,因为其中使用的模式转储无法处理数据库枚举"任务:准备=>[:环境] 做把做数据库:结构:转储"Rake::Task['db:structure:dump'].invoke把做 db:test:clone_structure"Rake::Task['db:test:clone_structure'].invoke结尾结尾结尾
我得到了标准任务的行为.如果我将任务名称更改为 :prepare2
,然后执行 rake db:test:prepare2
,那么它就可以正常工作.我从中得出的自然结论是,我的 rake 任务是在内置 Rails 任务之前定义的,所以我的任务被标准的 :prepare
任务覆盖了.>
谁能看到我如何解决这个问题?我宁愿覆盖它也不必使用新任务.谢谢,最大
如果你定义了一个已经存在的 rake 任务,它的执行会附加到原始任务的执行中;两个任务都会被执行.
如果你想重新定义一个任务,你需要先清除原来的任务:
Rake::Task["db:test:prepare"].clear
注意,一旦任务在 rake 中执行,即使您再次调用它也不会再次执行,这也很有用.这是设计使然,但您可以对任务调用 .reset
以允许它再次运行.
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
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:
Rake::Task["db:test:prepare"].clear
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.
这篇关于覆盖 rails 的默认 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!