Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoMapper and migrations
    primarykey
    data
    text
    <p>I'm building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model:</p> <pre><code>class SomeModel include MongoMapper::Document key :some_key, String end </code></pre> <p>Later in version 2, I realize that I need a new required key on the model. So, in version 2, SomeModel now looks like this:</p> <pre><code>class SomeModel include MongoMapper::Document key :some_key, String key :some_new_key, String, :required =&gt; true end </code></pre> <p>How do I migrate all my existing data to include some_new_key? Assume that I know how to set a reasonable default value for all the existing documents. Taking this a step further, suppose that in version 3, I realize that I really don't need some_key at all. So, now the model looks like this</p> <pre><code>class SomeModel include MongoMapper::Document key :some_new_key, String, :required =&gt; true end </code></pre> <p>But all the existing records in my database have values set for some_key, and it's just wasting space at this point. How do I reclaim that space?</p> <p>With ActiveRecord, I would have just created migrations to add the initial values of some_new_key (in the version1 -> version2 migration) and to delete the values for some_key (in the version2 -> version3 migration).</p> <p>What's the appropriate way to do this with MongoDB/MongoMapper? It seems to me that some method of tracking which migrations have been run is still necessary. Does such a thing exist?</p> <p>EDITED: I think people are missing the point of my question. There are times where you want to be able to run a script on a database to change or restructure the data in it. I gave two examples above, one where a new required key was added and one where a key can be removed and space can be reclaimed. How do you manage running these scripts? ActiveRecord migrations give you an easy way to run these scripts and to determine what scripts have already been run and what scripts have not been run. I can obviously write a Mongo script that does any update on the database, but what I'm looking for is a framework like migrations that lets me track which upgrade scripts have already been run.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload