【发布时间】: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