11

I'm looking for a node module to make mongo database migrations. So far I found mongo-migrate, but not really powerful enough. (Better than nothing but I need more, I'm used to use the Ruby migration which was really powerful!)

I found another one few weeks ago, powerful but doesn't deal with mongoDb, only with MySQL, PostGre and so on.

Do you know a module or something that could help me? I mean, I'm not the first person to want to deal with DB migrations, how do you manage that? My project will be big and I need control.

Here an example of what I did so far:

*0010-init_category_table.js*

var mongodb = require('mongodb');

exports.up = function(db, next){

    var documentName = 'category';
    var collection = mongodb.Collection(db, documentName);
    var index;
    var indexOptions;

    /**
     * Create indexes.
     */
    index = { "code": 1 };
    indexOptions = { unique: true };
    collection.ensureIndex( index, {unique: true, w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [ensureIndex] ' + JSON.stringify(index) + JSON.stringify(indexOptions));
    });

    index = { "name": 1 };
    indexOptions = { unique: true };
    collection.ensureIndex( index, {unique: true, w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [ensureIndex] ' + JSON.stringify(index) + JSON.stringify(indexOptions));
    });

    /**
     * Create basic data.
     */
    collection.insert({
        code: 'a',
        name: 'languageStatus'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });
    collection.insert({
        code: 'b',
        name: 'accessName'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });
    collection.insert({
        code: 'c',
        name: 'roleName'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });
    collection.insert({
        code: 'd',
        name: 'translationStatus'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });

    /**
     * Display index information.
     */
    collection.indexInformation(function(error, data){
        console.log(error ? error : documentName + ': [indexes] ' + JSON.stringify(data));
    });

    next();
};

exports.down = function(db, next){
    var documentName = 'category';
    var document = mongodb.Collection(db, documentName);

    var query = {
        $or: [
            {name: 'languageStatus'},
            {name: 'accessName'},
            {name: 'roleName'},
            {name: 'translationStatus'}
        ]
    };
    document.find(query, function(error, data){
        data.each(function(error, data){
            document.remove(data, {w: 1}, function(error, number){
                console.log(error ? error : documentName + ': [remove] (' + number + ') ' + JSON.stringify(data));
            })
        });
    });

    next();
};
9
  • 2
    Many reasons. First we are several on the project, using schema alllow us to create index and database configuration on different scripts that all will be run in all our machines after a git merge. Avoid different configurations between computers in dev. Allow us to create test data quite easily and have all same test data. But also because it's better to see what each people add, and allow us to revert modification such as bad index and so on. I agree that is really more useful for none schemaless DB, but it's not useless on mongoDB. Commented Feb 8, 2014 at 13:59
  • 1
    How are you defining and maintaining the schema though? It's not like there are columns you can add and remove. Commented Feb 8, 2014 at 14:03
  • 1
    Yeah, you're right, I don't define any schema but data and indexes (I think that's all I can do using mongo migration). I'll add an example in the main post right now. Commented Feb 8, 2014 at 14:07
  • 1
    Given the nature of MongoDb, I wouldn't expect something much more than what you've got. Commented Feb 8, 2014 at 14:27
  • 1
    Hum, yeah. That's just that the mongo-migrate is not perfect and I was looking for something better. It's not really a famous module yet. Commented Feb 8, 2014 at 14:29

2 Answers 2

6

I just developed this one: https://github.com/eberhara/mongration - you can also find on npm.

We needed a good node migration framework for mongodb, but could not find any - so we built one.

It has lots of better features than the regular migration frameworks:

  • Checksum (issues an error when a previosuly ran migration does not match its old version)
  • Persists migration state to mongo (there is no regular state file)
  • Full support to replica sets
  • Automatic handle rollbacks (developers must specify the rollback procedures)
  • Ability to run multiple migrations (sync or async) at the same time
  • Ability to run migrations against different databases at the same time
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at https://github.com/emirotin/mongodb-migrations it seems to be more feature rich, mature and maintained.

1 Comment

This repo was better out of all available on the shelf but sounds like this repo is not getting maintained anymore and going to be dead very soon as deprecated warnings are started coming github.com/emirotin/mongodb-migrations/issues/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.