Does it make sense to use migration with Rails / Mongoid / MongoDB?

Should I create AR migrations as I change my models? I use Mongoid and MongoDB, so I do not see any advantages. The only advantage I can think of is renaming the field, but I can also do with a little script. Will this work? My gut tells me that I do not need migrations, but I would like to hear from someone who has more experience.

What is the best practice? Should I use migrations with MongoDB?

+6
source share
2 answers

Since MongoDB does not perform (as in clause 2.6) any forced execution of the server-side scheme, data transfer scripts are not strictly required. This can be especially useful for development speed.

However, it might make sense to create a migration for your production data if you want to practice “data hygiene” and ensure consistency between different deployments.

For instance:

  • delete unused fields
  • add new required fields
  • setting defaults
  • rename fields
  • Download required data / fixtures
  • providing required indices

Of course, you have the choice to make any of the above as one-time scripts or to handle exceptions in the application code. For example, you can lazily add missing fields or default values ​​as documents are loaded from the database for editing.

For Mongoid in particular, you can try the mongoid_rails_migrations gem.

+8
source

Mongo DB documents do not have a schema, so migrations are not needed. You can simply define the fields in your mongoid models, for example:

field :name, type: String

See the mongoid documentation for supported fields.

+1
source

Source: https://habr.com/ru/post/971777/


All Articles