简体   繁体   English

Rails:将数据库从Postgres迁移到MongoDB

[英]Rails: Migrating database From Postgres to MongoDB

My project was created using PostgresSQL but we really intend to use MongoDB, what should I change in order for that to happen? 我的项目是使用PostgresSQL创建的,但是我们确实打算使用MongoDB,为了使它发生,我应该更改什么?

A lot of scaffolding already took place, so we are trying to savage any of the work already done... 已经发生了很多脚手架,因此我们正在尝试破坏已经完成的任何工作...

  1. Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.) 从您的Gemfile中删除数据库适配器gem(mysql2,sqlite3等)

  2. Change your config/application.rb 更改您的config/application.rb

Remove require 'rails/all' line and require frameworks you want to use, for example: 删除要求的'rails/all'行和您要使用的要求框架,例如:

require "action_controller/railtie"
require "action_view/railtie"
require "action_mailer/railtie"
require "active_job/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie" 

NOTE: You should be using the require snippet from the rails/all.rb file that is current with your version of Rails without the active_record railtie. 注意:您应该使用rails/all.rb文件中的require片段,该片段是您的Rails版本的最新版本,不带active_record轨道。 Here is a link to rails/all.rb on the Rails master branch . 这是Rails master分支上的rails/all.rb的链接。

  1. Delete your config/database.yml file, db/schema.rb and migrations (if any) 删除config/database.yml文件, db/schema.rb和迁移(如果有)

  2. Delete migration check in test/test_helper.rb test/test_helper.rb删除迁移检查

  3. Delete any ActiveRecord configuration from your config/environments files 从您的config / environments文件中删除任何ActiveRecord配置

The same situation I also I have to face . 同样的情况我也不得不面对。 I am adding some additional points to above answer . 我在上面的答案中添加了一些其他要点。

1) Create a file in initializers and put this code 1)在初始化程序中创建一个文件,并将此代码放入

Mongoid.load!(Rails.root.join("config/mongoid.yml"))

2) You have to remove from every model which are inheriting from ApplicationRecord and delete the application_record file. 2)您必须从继承自ApplicationRecord的每个模型中删除并删除application_record文件。

3) If you have installed devise so you have to change 3)如果您已经安装了devise,则必须进行更改
**From ** **来自**

 require 'devise/orm/active_record'

to

require 'devise/orm/mongoid'

4) If you are using carrierwave so in gem file you have to replace 4)如果您使用的是载波,那么在gem文件中必须替换

From

gem 'carrierwave', github: 'carrierwaveuploader/carrierwave' 

To

gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

Better late than never! 迟到总比不到好! This should come in handy to somebody, someday!!!! 有一天,这应该对某人有用!

The following python-based migration framework does the job. 以下基于python的迁移框架可以完成这项工作。

https://github.com/datawrangl3r/pg2mongo https://github.com/datawrangl3r/pg2mongo

Coming to the performance, the migration of each JSON object is dynamic and there shouldn't be any memory lock issues when you use the above framework. 为了提高性能,每个JSON对象的迁移都是动态的,并且在使用上述框架时不应该存在任何内存锁定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM