将多个旧数据库迁移到一个 Rails 应用程序中
我有几个(旧的)Drupal 站点需要用一个新的 Rails 应用程序替换。该新站点应包含所有旧的 Drupal 内容。旧的 Drupal 内容已部分损坏(由于近 7 年的糟糕升级、停产的模块等)。它是 Drupal 的事实几乎无关紧要,只是它会导致一些不一致、奇怪的命名和糟糕的规范化表。
需要导入 Rails 应用程序的内容很简单:内容(博客条目)、附加文件(图像)和评论。我有两个数据库“过时”(不在生产中),另外两个数据库在生产中,但允许关闭/锁定一段时间(几小时,几天)。因此,迁移不需要针对速度进行优化,或者完全保存(意味着:我可以在运行迁移时释放发布的评论)
Rails(3) 应用程序大部分已完成,并且仅使用 Active-record 约定。
由于限制(损坏的、不一致的数据库、需要合并的多个数据库),我更喜欢为此编写迁移,而不是将我的新 Rails 应用程序连接到丑陋的、不一致的遗留数据库。
我的问题是:
- 是否有任何环境、gems 或工具可以使 Rails 中的导入变得更容易:例如,允许在某些 DSL 中从旧到新进行简单映射的东西。
- 或者完全用 SQL 编写迁移是否更容易:将旧数据转换为适合 Rails 应用程序的结构的 SQL 查询?迁移是从MySQL->MySQL。
- 或者我应该将 Activerecord 连接到旧数据库,循环每一行/结果并运行 Object.save!在我的 Rails 应用程序中?
I have several (old) Drupal-sites that need to be replaced with one single new Rails app. That new site should hold all old Drupal-content. That old Drupal-content is partly broken (due to nearly 7 years of fugly upgrades, discontinued modules and so on). The fact that it is Drupal is hardly relevant, just the fact that it causes some inconsistencies, weird naming, and badly normalised tables.
The content needing to be imported into the Rails app is simple: content (blog-entries), attached files (images) and comments. I have the luxury of two databases being "stale" (not in production) and two more being in production, but allowed to go down/locked for a while (hours, days). So, the migrations need not be optimized for speed, or be entirely save (meaning: I can afford to loose a comment being posted while running the migration)
The Rails(3) app is mostly done, and using Active-record conventions only.
Due to the contraints (broken, inconsistent database, several databases needing merging) I prefer to write migrations for this, instead of connecting my new Rails app to an ugly, inconsistent legacy databse.
My questions are:
- Are there any environments, gems or tools that make importing in Rails easier: e.g. something that allows simple mapping from old-new in some DSL.
- Or is it easier to write my migrations entirely in SQL: SQL queries that will turn the old data into the structure that fits the Rails app? Migrating is from MySQL->MySQL.
- Or should I just connect Activerecord to the old databases, loop over each row/result and run an Object.save! in my rails app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的案例,我在当前的项目中面临同样的问题。
我认为你应该考虑第二点!
编写 SQL 迁移以将数据从旧数据库导入到新数据库,然后使用新架构!我想您有一个转储 SQL 文件,其中包含每个旧数据库的 INSERT 查询列表。
因此,这一策略可能是:
首先,我要向您介绍 legacy_data gem,它基本上使用适当的方式生成所有模型层验证/关联,但鉴于您已经在 Rails 应用程序中设置了所有必要的模型,因此在您的情况下它是无用的。
Interesting case, I am facing the same issue on my current project.
I think you should consider the second point!
Write the SQL migrations to import the data from your old database to the new one, then work with the new schema! I suppose you have a dump SQL file with a list of INSERT queries for each old-database.
So the strategy for this could be:
At first, I was going to tell you about the legacy_data gem which basically generates all the model layer with the appropriate validations/associations, but it is useless in your case given that you already have all the necessary models set up in your Rails application.