如何从另一个插件中的 Rails 3 插件安装数据库模式 [英] How to install database schema from a Rails 3 plugin in another plugin

查看:44
本文介绍了如何从另一个插件中的 Rails 3 插件安装数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Rails 3 插件,它使用了我最近编写的另一个 Rails 3 插件.让我们称它们为七月和八月.所以在 8 月的 gemspec 中,我添加了以下行:

I'm writing a Rails 3 plugin that uses another Rails 3 plugin that I recently wrote. Let's call them July and August. So in August's gemspec, I add the line:

s.add_dependency "july", "~> 0.0.1"

然后我运行bundle install.然后我创建了一些模型和装置.接下来我需要迁移数据库:

and I run bundle install. Then I create some models and fixtures. Next I need to migrate the database:

$ cd test/dummy
$ rake august:install:migrations
$ rake db:migrate

现在,August(我正在创建的插件)表在开发和测试数据库中,但 July 表不在.但是我的 August 表有我的 July 表的外键,所以在我可以运行任何测试之前,我需要创建 July 表并编写适当的装置.我希望运行 rake -T 并看到

Now, the August (the plugin I am creating) tables are in the development and test databases, but the July tables are not. But my August tables have foreign keys to my July tables, so before I can run any tests, I need to create the July tables and write appropriate fixtures. I would expect to run rake -T and see

rake august:install:migrations
rake july:install:migrations

但我看到的只是august rake 任务.那么我该如何创建 7 月份的数据库表(除了创建一个新的迁移,因为我已经在我的 7 月份代码库中这样做了,这会违反 DRY)?

but all I see is the august rake task. So how do I create the July database tables (other than creating a new migration, which would violate DRY since I've already done that in my July codebase)?

推荐答案

Dummy App 必须直接依赖于july"才能加载july"的任务.所以我需要将它包含在 Gemfile 中,而不是 gempspec.但是,将august"放入 Gemfile/gemspec 后,当我 bundle install 时,将它放入 Gemfile 不会强制安装真实(非虚拟)应用程序.

The Dummy App has to be directly dependent on "july" for it to load "july"'s rake tasks. So I need to include it in the Gemfile, not the gempspec. However, putting it in Gemfile won't force real (non-dummy) apps to install july when I bundle install after putting "august" in their Gemfile/gemspec.

所以我需要在两个地方都包含它——在 rake 任务的 Gemfile 中(这可以在 :development 组中)和 gemspec(使用 add_dependencyadd_runtime_dependency) 强制安装依赖项.这就是解决方案,但我不明白为什么.

So I need to include it in both places--in the Gemfile for the rake task (and this can be in a :development group), and in the gemspec (using add_dependency or add_runtime_dependency) to force installation of the dependency. That's the solution, but I don't understand why.

我尝试在此处的评论中解释我对此的看法但无法真正确定任何解释这种设计方法的逻辑.有人能解释一下真正的原因吗?

I tried explaining my thoughts on this in my comment here but wasn't really able to pinpoint any logic that explains this design methodology. Could someone please explain the true reason?

这篇关于如何从另一个插件中的 Rails 3 插件安装数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆