每天将部分数据库转储导入 Rails 应用程序的最佳实践?
iTunes 企业合作伙伴 Feed 是“来自 iTunes 和 App Store 的完整元数据集的数据源”并且“以两种不同的格式提供 - 要么作为构建关系数据库所需的文件,要么作为依赖于国家/地区和媒体的独立平面文件。”
我需要使用此提要中的数据(本质上导出到平面文件中)并允许将我自己的模型对象(用户、活动等)链接到提要(应用程序、开发人员等)提供的数据。提供每周完整导出和每日增量导出。
我有两种实现此方法的想法:
- 在我的 Rails 应用程序中创建所有模型并编写我自己的导入程序,该导入程序将使用我创建的模型(应用程序、开发人员等)每天通过 cron 将记录直接插入/更新到我的应用程序的数据库中.)
- 保持这个数据库完全独立,并开放我自己的应用程序将使用的 REST API。
我用 #1 来保留 Rails 应用程序中的所有内容的天真方法是基于能够观察到变化的需要我从 EPF 获得的数据。例如,如果应用程序的描述被更新,我希望能够通过观察者创建一个 Activity 对象来跟踪该更新。
一方面,#2 感觉是一种更好的方法,因为它为数据创建了一个独立的 API,可以从我创建的多个不同应用程序中使用这些数据。另一方面,我只是不确定如何在不直接在我自己的模型上使用观察者的情况下完成数据更改通知。或者,我什至如何以面向对象的方式使用数据,该方式仍然可以与我自己的模型一起使用。感觉就像是大量重复的工作,必须查询 API 来获取应用程序的数据,为其创建一个适当的 Active Record 对象,然后保存它,以便它可以链接到我自己的模型之一。
有没有一种我完全缺少的标准方法可以做到这一点?有什么指点吗?
编辑: Rails 引擎听起来很有趣,但这意味着每个应用程序仍然需要单独使用和插入数据。听起来不那么干。听起来越来越像 REST API 是正确的选择。我只是不知道如何弥合 API 与 Active Record 模型之间的差距。
The iTunes Enterprise Partner Feed is "a data feed of the complete set of metadata from iTunes and the App Store" and "is available in two different formats - either as the files necessary to build a relational database or as stand-alone flat files that are country and media dependent."
I need to consume the data from this feed (which is essentially exported into flat files) and allow linking of my own Model objects (User, Activity, etc.) to data provided by the feed (App, Developer, etc.) The data is provided as a weekly full export and a daily incremental export.
I have two ideas for ways to implement this:
- Create all of the models in my rails app and write my own importer that will insert/update records directly into my app's database daily via cron using models I've created (App, Developer, etc.)
- Keep this database entirely separate and open up REST API that my own app will consume
My naive approach with #1 to keep everything in the Rails app is based on the need to be able to observe changes in the data I'm getting from the EPF. For example, if an App's description is updated, I want to be able to create an Activity object via an observer to track that update.
On one hand #2 feels like a better approach because it creates a standalone API into the data that can be consumed from several different apps I create. On the other hand, I'm just not sure how I'd accomplish the data change notifications without using observers directly on my own models. Or, how I would even consume the data in an object oriented way that could still be used with my own models. It feels like a lot of duplicate work to have to query the API for, say, an App's data, create a proper Active Record object for it and then save it just so it can be linked to one of my own models.
Is there a standard way to do this that I'm totally missing? Any pointers?
EDIT: Rails engines sound interesting but it would mean that each app would still need to consume and insert the data separately. That doesn't sound so DRY. It sounds more and more like a REST API is the way to go. I just don't know how to bridge the gap from API to Active Record model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails Engines 可能很适合这个。您可以创建 Rails Engine gem 并添加所有模型和 rake 任务来使用数据。然后,您可以将这个 gem 包含在任何使用它的应用程序中,并创建一个包含该 gem 的 API 应用程序。您应该能够在与 gem 交互的其他应用程序中创建观察者。
我有很多相互交互的应用程序,这种方法对我来说很有效。我有一个中央应用程序,其中包含使用数据的所有引擎,并且我从该应用程序运行所有 cronjobs。我使用
use_db
插件,它允许我的应用程序与不同的数据库进行通信。每个引擎都有use_db
作为依赖项,我将数据库配置保留在 gem 中。一个示例:我没有在包含我的引擎的应用程序中使用观察者,但我认为它没有理由不起作用。使用引擎,模型的工作方式就像位于您的 app/models 目录中一样。希望这有帮助!
Modest Rubyist 有一个很好的关于 Rails 3 插件的 4 部分教程,其中包括引擎:
http: //www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
Rails Engines might be a good fit for this. You can create a Rails Engine gem and add all of your models and rake tasks to consume the data. Then you can include this gem in any app that uses it and also create an API app which includes the gem. You should be able to create observers in your other apps that interact with the gem.
I have quite a few apps that interact with each other and this approach works well for me. I have one central app that includes all the engines that consume data and I run all of my cronjobs from this app. I use the
use_db
plugin which allows my app to communicate with different databases. Each engine hasuse_db
as a dependency and I keep the database configuration inside the gem. One example:I have not used observers inside an app that includes my engines, but I see no reason why it would not work. With the engine the models work as if they are in your app/models directory. Hope this helps!
Modest Rubyist has a good 4 part tutorial on Rails 3 plugins that includes Engines:
http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/