我编写了一个自定义数据库适配器,它在 Rails 服务器运行时可以正确有效地工作.我现在想添加用于创建、删除和迁移数据库的常用 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 打包在一起,以便它们覆盖使用 gem 的任何人的默认定义?
How do I package these definitions with my gem so that they override the default ones for anyone who uses the gem?
我查看了其他适配器的源代码,但所有 rake 任务逻辑似乎都融入了 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.
推荐答案这可以通过:
# somewhere in your gem's tasks Rake::Task['db:create'].clear # then re-define namespace 'db' do task 'create' do # ... end end当 Take::Task#[] 无法解析任务时 它将失败.如果您的任务有时存在,您可能想要:
When Take::Task#[] can't resolve a task it will fail. If your tasks sometimes exists, you might want to:
task_exists = Rake.application.tasks.any? { |t| t.name == 'db:create' } Rake::Task['db:create'].clear if task_exists如果您想将任务添加到现有的 rake 任务,请使用 enhance.
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更多推荐
如何覆盖自定义数据库适配器的 rake 任务?
发布评论