Rails - 两个应用程序之间的共享数据库表 [英] Rails - Shared Database Tables between two apps

查看:148
本文介绍了Rails - 两个应用程序之间的共享数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们很快就会将一个伴随的Rails应用程序发布到我们现有的Rails应用程序中。我们将在相同的服务器上运行同伴应用程序和现有的应用程序。

We’ll be releasing shortly a companion Rails application to our existing Rails app. We will be running the companion app alongside our existing app on the same servers.

我的问题涉及数据库。我的托管提供商通常会为新应用程序配置第二个不同的数据库 - secondappname_production。但是,在应用程序之间有一系列共享表。这些共享表也由一系列cron作业维护。我想避免重复这些表(因此cron作业),如果可能的话。

My question concerns the databases. My hosting provider generally would configure a 2nd distinct database for the new application - secondappname_production. However, there are a series of shared tables between the applications. These shared tables are also maintained by a series of cron jobs. I would love to avoid duplicating these tables (and thus the cron jobs) if at all possible.

有一种方法,我可以把这些共享表在一个共享Rails应用程序可以利用的数据库?关于如何配置或文档指针的任何建议?

Is there a way that I can put these shared tables in perhaps a shared database that both Rails apps can leverage? Any suggestions as to how to configure that or documentation pointers?

非常感谢!

编辑:为什么我不想从同一个数据库运行这两个应用程序:两个应用程序都有相同的名称(但不同的属性的模型,等等)的模型,所以我宁愿不同时运行两个出同一个数据库。 ...

To clarify why I don't want to run both apps out of the same DB: Both apps have models of the same name (yet different attributes of the models, etc.), so I would prefer to not run both out of the same DB....

推荐答案

您可以在一个数据库中建立一些模型新的应用程序自己的数据库(所以他们不会与现有的应用程序碰撞)。

You can have some models in one database (the ones that you want to share), and others in the new app's own database (so they don't collide with the existing app).

要为特定模型指定不同的数据库,请尝试这样:

To specify a different database for a particular model, try something like this:

class SharedModelBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection(ActiveRecord::Base.configurations["shared_db_connection_#{RAILS_ENV}"])
end

现在,使用它作为共享模型的基类,你应该很好。

Now, use this as a base class for your shared models, and you should be good to go.

您的问题的一部分是最佳做法,因此还有其他几个选项。

Part of your question is best practices, so a couple of other options.

一个选项是甚至不直接尝试访问db,而是使用ActiveResource在应用程序之间构建集成。让原始应用程序为这些表提供RESTful接口,并在新应用程序中使用它,并且不共享数据库。我喜欢这个选项,但可能不是聪明的你的情况。

One option is to not even try to access to the db directly, but instead build an integration between the apps using ActiveResource. Have the original app provide a RESTful interface to these tables, and consume it in the new app, and don't share the db at all. I like this option, but may not be clever for your situation.

另一个选择是重构这些共享表到自己的数据库,并让rails应用程序访问db。你甚至可能最终写入这些共享数据的服务(例如restful接口)供这两个应用程序使用,然后你很好地解耦。

Another option is to refactor these shared tables into their own database, and have both the rails apps access that db. You could even end up writing services (e.g. restful interface) to this shared data to be used by both apps, and then you are nicely decoupled.

考虑此共享数据库结构更改时的复杂性。如果您直接共享表,则两个轨道应用程序可能必须同时更改以适应更改 - 您已经链接您的发布计划,这些应用程序现在耦合。如果在服务中包装对数据库的访问,则可以提供抽象,因为您可以通过在旧服务接口的同时部署新更新的服务来同时为旧结构和新结构提供服务。这一切都取决于你的应用程序,如果这样的复杂性是值得的。

Consider the complexities of when this shared db structure changes. If you are sharing the tables directly, both rails apps could have to be changed simultaneously to accommodate the change - you have linked your release schedule now, these apps are now coupled. If you wrap the access to the db in services, this can provide abstraction as you can serve both the old structure and new structure simultaneously by deploying the new updated service at the same time as the old service interface. It all depends on your app if such complexity is worth it.

这篇关于Rails - 两个应用程序之间的共享数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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