【问题标题】:Run database migration (mongodb) with node.js使用 node.js 运行数据库迁移 (mongodb)
【发布时间】:2014-03-05 22:49:48
【问题描述】:

我正在寻找一个节点模块来进行 mongo 数据库迁移。到目前为止,我找到了mongo-migrate,但还不够强大。 (总比没有好,但我需要更多,我习惯使用非常强大的 Ruby 迁移!)

几周前我发现了另一个,功能强大但不处理 mongoDb,只处理 MySQL、PostGre 等。

你知道一个模块或可以帮助我的东西吗?我的意思是,我不是第一个想要处理数据库迁移的人,你是如何管理的?我的项目很大,我需要控制。

这是我到目前为止所做的一个例子:

*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();
};

【问题讨论】:

  • 很多原因。首先,我们在项目中有几个,使用模式允许我们在不同的脚本上创建索引和数据库配置,所有这些都将在 git 合并后在我们所有的机器上运行。避免在 dev 中的计算机之间进行不同的配置。允许我们很容易地创建测试数据并拥有所有相同的测试数据。也是因为最好看看每个人添加了什么,并允许我们还原诸如错误索引之类的修改。我同意这对于非无模式数据库确实更有用,但它在 mongoDB 上并非没用。
  • 您是如何定义和维护架构的?这不像是可以添加和删除的列。
  • 是的,你是对的,我没有定义任何模式,而是数据和索引(我认为这就是我使用 mongo 迁移所能做的全部)。我现在将在主帖中添加一个示例。
  • 鉴于 MongoDb 的性质,我不希望有比您所拥有的更多的东西。
  • 嗯,是的。只是mongo-migrate 并不完美,我一直在寻找更好的东西。它还不是一个真正有名的模块。

标签: node.js mongodb database-migration


【解决方案1】:

看看https://github.com/emirotin/mongodb-migrations,它似乎功能更加丰富、成熟和维护。

【讨论】:

【解决方案2】:

我刚刚开发了这个:https://github.com/eberhara/mongration - 你也可以在 npm 上找到。

我们需要一个好的 mongodb 节点迁移框架,但找不到——所以我们构建了一个。

它比常规迁移框架有很多更好的功能:

  • 校验和(当先前运行的迁移与其旧版本不匹配时发出错误)
  • 保持迁移状态到 mongo(没有常规状态文件)
  • 完全支持副本集
  • 自动处理回滚(开发者必须指定回滚过程)
  • 能够同时运行多个迁移(同步或异步)
  • 能够同时针对不同的数据库运行迁移

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2021-04-27
    • 2017-08-10
    • 1970-01-01
    • 2014-06-21
    • 2019-11-15
    • 1970-01-01
    相关资源
    最近更新 更多