@@ -685,7 +685,7 @@ module.exports = function() {
685
685
var collection = global . mongoClient . db ( appId ) . collection ( "_Schema" ) ;
686
686
687
687
//get previous schema object of current table if old table
688
-
688
+ var renameColumnObject = { } ;
689
689
if ( ! isNewTable )
690
690
collection . findOne ( { name :tableName } , function ( err , doc ) {
691
691
if ( err )
@@ -701,11 +701,12 @@ module.exports = function() {
701
701
if ( newColumnObj . _id === oldColumnObj . _id ) {
702
702
if ( newColumnObj . name != oldColumnObj . name ) {
703
703
//column name is updated update previous records.
704
- updateColumnNameOfOldRecordsPromises . push ( _updateColumnNameOfOldRecords ( tableName , appId , newColumnObj . name , oldColumnObj . name ) ) ;
704
+ renameColumnObject [ oldColumnObj . name ] = newColumnObj . name ;
705
705
}
706
706
}
707
707
} )
708
708
} )
709
+ updateColumnNameOfOldRecordsPromises . push ( _updateColumnNameOfOldRecords ( tableName , appId , renameColumnObject ) ) ;
709
710
710
711
}
711
712
} )
@@ -773,8 +774,14 @@ module.exports = function() {
773
774
promises . push ( global . mongoUtil . collection . deleteAndCreateTextIndexes ( appId , tableName , cloneOldColumns , schema ) ) ;
774
775
//updateColumnNameOfOldRecordsPromises stores the promises for updating previous records.
775
776
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 ) {
778
785
//TODO : Rollback.
779
786
deferred . resolve ( table ) ;
780
787
} ) ;
@@ -1103,34 +1110,17 @@ module.exports = function() {
1103
1110
} ;
1104
1111
} ;
1105
1112
1106
- function _updateColumnNameOfOldRecords ( tableName , appId , newColumnName , oldColumnName ) {
1113
+ function _updateColumnNameOfOldRecords ( tableName , appId , renameColumnObject ) {
1107
1114
1108
1115
var deferred = q . defer ( ) ;
1109
1116
1110
1117
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
+ } ) ;
1134
1124
1135
1125
return deferred . promise ;
1136
1126
0 commit comments