Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using migrations with Rails/Mongoid/MongoDB make sense?

Should I create AR migrations as I'm changing my models? I am using Mongoid and MongoDB so I do not see any benefits. The only benefit that I can think of is renaming a field - but that I can also do with small script. Would that even work? My gut is telling me that I do not need migrations, but I'd like to hear from someone with more experience.

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

like image 210
xx77aBs Avatar asked Jul 04 '14 18:07

xx77aBs


People also ask

Does MongoDB need migrations?

This is why you need a migration tool for MongoDB And as software evolves, we may need to fix mistakes on the data schema or adapt existing data as requirements change.

Can we use MongoDB with rails?

Mongoid 7.0. 5 or higher is required to use Rails 6.0. This generator will create the config/mongoid. yml configuration file, which is used to configure the connection to the MongoDB deployment.

Does Ruby on Rails support MongoDB?

The answer is yes: it makes a lot of sense. Nevertheless, Rails wasn't originally built to use a document database so you must use a separate gem in place of Active Record. MongoMapper and Mongoid are the two leading gems that make it possible use MongoDB as a datastore with Rails.

What does DB Migrate do rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


1 Answers

Since MongoDB does not (as at 2.6) provide any server-side schema enforcement, data migration scripts are not strictly required. This can be particularly helpful for speed of development.

However, it may still make sense for you to create migrations for your production data if you want to practice good "data hygiene" and ensure consistency across different deployments.

For example:

  • removing unused fields
  • adding new required fields
  • setting default values
  • renaming fields
  • loading required data/fixtures
  • ensuring required indexes

You certainly have the choice of doing any of the above as one-off scripts or handling exception cases in your application code. For example, you can lazily add missing fields or defaults as documents are loaded from the database for editing.

For Mongoid in particular you may want to try the mongoid_rails_migrations gem.

like image 65
Stennie Avatar answered Oct 21 '22 05:10

Stennie