将使用 Active Record 的 Rails 应用程序迁移到 MongoDB

问题描述 投票:0回答:1

我编写了一个 Rails (5.2) 应用程序,它使用默认的 Active Record ORM 和 Devise 来验证用户身份。它还有一些其他模型和关联:

  • 用户有_一张卡
  • 用户有_很多订阅
  • 订阅有_许多发票
  • ....

现在,我必须切换到 MongoDB,我在网上找到的所有文章都是关于从头开始使用 MongoDB 而不是关系数据库,当然,我不想从头开始重写所有内容。

根据我的理解,为了从 Active Record 迁移到 MongoDB,我必须遵循以下步骤,例如使用 mongoid gem:

  1. 删除所有
    < ActiveRecord::Base
    继承
  2. 在模型类中包含
    Mongoid::Document
  3. db/schema.rb
    的每一行翻译成molde类中的一行,例如:

    t.string "encrypted_password", default: "", null: false
    db/schema.rb

    field :encrypted_password, type: String, default: "", null: false
    MyModel

  4. 配置 Rspec 以使用 mongoid

    config.include Mongoid::Matchers

  5. 配置

    database_cleaner
    以使用 mongoid
    DatabaseCleaner.orm = 'mongoid'

  6. 运行

    rails generate mongoid:config

  7. require 'devise/orm/active_record'
     中的 
    require 'devise/orm/mongoid'
    替换为

    config/initializers/devise.rb

仍有一些悬而未决的问题,例如:

  • 我还需要翻译
    created_at
    updated_at
    吗? (我的猜测是“是”)
  • 模型验证仍然有效吗?还是我必须适应它们?
  • 还有其他步骤我忘记了吗?
  • 有现成的解决方案吗?
mongodb mongoid rails-activerecord database-migration ruby-on-rails-5.2
1个回答
0
投票

自 Ruby 3.3 和 Rails 7.x 开始 - MongoDB 现在如何使用官方教程这里

© www.soinside.com 2019 - 2024. All rights reserved.