我如何重写耙任务自定义数据库适配器? [英] How do I override rake tasks for a custom database adapter?
问题描述
我写了一个自定义数据库适配器,当一个护栏服务器正在运行工作正常,有效。现在我想补充平时rake任务定义,创建,删除和迁移的数据库。
I've written a custom database adapter that works correctly and effectively when a rails server is running. I would now like to add the usual rake task definitions for creating, dropping and migrating the database.
我想实现:
db:[drop|create|migrate]
我如何用我的gem包这些定义,让他们替代默认的值的人谁使用的宝石?
How do I package these definitions with my gem so that they override the default ones for anyone who uses the gem?
我看了看,通过其它适配器,但所有的耙任务逻辑的来源似乎是烤成active_record本身,每个任务只接通适配器名称。
I looked through the source of other adapters but all the rake task logic appears to be baked into active_record itself, each task just switches on the adapter name.
推荐答案
这样做的正确的方法是使用一个新的rake任务和更新您的通话code!
The correct way of doing this is to use a new rake task and update your calling code!
猴补丁默认任务是一个可怕的概念,希望有一个更好的挂钩点的地方自定义的适配器。这里有一个方法来覆盖rake任务,如果你没有选择,只能低着头这条道路(你可能做到这意思吗!):
Monkey-patching default tasks is a scary concept, hopefully there's a nicer hook point somewhere for custom adapters. Here's a way to override a rake task if you have no choice (which you probably do! just sayin') but to head down that path:
# somewhere in your gem's tasks
tasks = Rake.application.instance_variable_get '@tasks'
tasks.delete 'db:create'
namespace 'db' do
task 'create' do
# ...
end
end
此技术也可以修改,以包裹耙任务。
This technique can also be modified to wrap rake tasks.
如果你想的添加的任务,以现有的rake任务,使用增强
。
If you want to add tasks to an existing rake task, use enhance
.
Rake::Task['db:create'].enhance do
Rake::Task['db:after_create'].invoke
end
这篇关于我如何重写耙任务自定义数据库适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!