Skip to content

Commit 9a7b7c5

Browse files
committed
added multiple column name change support
1 parent 50779ea commit 9a7b7c5

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

services/app.js

+18-28
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ module.exports = function() {
685685
var collection = global.mongoClient.db(appId).collection("_Schema");
686686

687687
//get previous schema object of current table if old table
688-
688+
var renameColumnObject={};
689689
if(!isNewTable)
690690
collection.findOne({name:tableName},function(err,doc){
691691
if(err)
@@ -701,11 +701,12 @@ module.exports = function() {
701701
if(newColumnObj._id===oldColumnObj._id){
702702
if(newColumnObj.name!=oldColumnObj.name){
703703
//column name is updated update previous records.
704-
updateColumnNameOfOldRecordsPromises.push(_updateColumnNameOfOldRecords(tableName,appId,newColumnObj.name,oldColumnObj.name));
704+
renameColumnObject[oldColumnObj.name]=newColumnObj.name;
705705
}
706706
}
707707
})
708708
})
709+
updateColumnNameOfOldRecordsPromises.push(_updateColumnNameOfOldRecords(tableName,appId,renameColumnObject));
709710

710711
}
711712
})
@@ -773,8 +774,14 @@ module.exports = function() {
773774
promises.push(global.mongoUtil.collection.deleteAndCreateTextIndexes(appId, tableName, cloneOldColumns, schema));
774775
//updateColumnNameOfOldRecordsPromises stores the promises for updating previous records.
775776
q.all(promises.concat(updateColumnNameOfOldRecordsPromises)).then(function(res) {
776-
deferred.resolve(table);
777-
}, function(error) {
777+
//confirm all colums are updated
778+
q.all(updateColumnNameOfOldRecordsPromises).then(function(res) {
779+
deferred.resolve(table);
780+
}, function(error) {
781+
//TODO : Rollback.
782+
deferred.resolve(table);
783+
});
784+
}, function(error) {
778785
//TODO : Rollback.
779786
deferred.resolve(table);
780787
});
@@ -1103,34 +1110,17 @@ module.exports = function() {
11031110
};
11041111
};
11051112

1106-
function _updateColumnNameOfOldRecords(tableName,appId,newColumnName,oldColumnName){
1113+
function _updateColumnNameOfOldRecords(tableName,appId,renameColumnObject){
11071114

11081115
var deferred = q.defer();
11091116

11101117
var collection = global.mongoClient.db(appId).collection(tableName);
1111-
var findQuery = collection.find({});
1112-
var updatedDocs=[]
1113-
findQuery.toArray(function(err, docs) {
1114-
if (err) {
1115-
global.winston.log('error', err);
1116-
deferred.reject(err);
1117-
} else if (!docs || docs.length == 0) {
1118-
console.log('No records existed');
1119-
deferred.resolve('No records existed');
1120-
} else if (docs.length > 0) {
1121-
docs.forEach(function(doc){
1122-
if(doc[oldColumnName])
1123-
//set new column data
1124-
doc[newColumnName]=doc[oldColumnName];
1125-
//delete old column
1126-
delete doc[oldColumnName];
1127-
updatedDocs.push(doc);
1128-
collection.save(doc);
1129-
});
1130-
deferred.resolve(updatedDocs.length+"docs updated");
1131-
}
1132-
});
1133-
1118+
collection.updateMany( {}, { $rename: renameColumnObject },function(err,doc){
1119+
if(err)
1120+
deferred.reject()
1121+
else
1122+
deferred.resolve();
1123+
} );
11341124

11351125
return deferred.promise;
11361126

0 commit comments

Comments
 (0)