@@ -561,6 +561,7 @@ module.exports = function() {
561
561
tableProps = tableProps || {
562
562
isEditableByClientKey : false
563
563
}
564
+ var updateColumnNameOfOldRecordsPromises = [ ]
564
565
565
566
try {
566
567
@@ -683,6 +684,33 @@ module.exports = function() {
683
684
684
685
var collection = global . mongoClient . db ( appId ) . collection ( "_Schema" ) ;
685
686
687
+ //get previous schema object of current table if old table
688
+ var renameColumnObject = { } ;
689
+ if ( ! isNewTable )
690
+ collection . findOne ( { name :tableName } , function ( err , doc ) {
691
+ if ( err )
692
+ deferred . reject ( "Error : Failed to get the table. " ) ;
693
+ else if ( ! doc )
694
+ deferred . reject ( "Error : Failed to get the table. " ) ;
695
+ else {
696
+
697
+ doc . columns . forEach ( function ( oldColumnObj , i ) {
698
+ //check column id
699
+ schema . forEach ( function ( newColumnObj , i ) {
700
+ //match column id of each columns
701
+ if ( newColumnObj . _id === oldColumnObj . _id ) {
702
+ if ( newColumnObj . name != oldColumnObj . name ) {
703
+ //column name is updated update previous records.
704
+ renameColumnObject [ oldColumnObj . name ] = newColumnObj . name ;
705
+ }
706
+ }
707
+ } )
708
+ } )
709
+ updateColumnNameOfOldRecordsPromises . push ( _updateColumnNameOfOldRecords ( tableName , appId , renameColumnObject ) ) ;
710
+
711
+ }
712
+ } )
713
+
686
714
console . log ( 'Collection Object Created.' ) ;
687
715
688
716
collection . findOneAndUpdate ( {
@@ -744,10 +772,16 @@ module.exports = function() {
744
772
745
773
//Index all text fields
746
774
promises . push ( global . mongoUtil . collection . deleteAndCreateTextIndexes ( appId , tableName , cloneOldColumns , schema ) ) ;
747
-
748
- q . all ( promises ) . then ( function ( res ) {
749
- deferred . resolve ( table ) ;
750
- } , function ( error ) {
775
+ //updateColumnNameOfOldRecordsPromises stores the promises for updating previous records.
776
+ q . all ( promises . concat ( updateColumnNameOfOldRecordsPromises ) ) . then ( function ( res ) {
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 ) {
751
785
//TODO : Rollback.
752
786
deferred . resolve ( table ) ;
753
787
} ) ;
@@ -1076,6 +1110,22 @@ module.exports = function() {
1076
1110
} ;
1077
1111
} ;
1078
1112
1113
+ function _updateColumnNameOfOldRecords ( tableName , appId , renameColumnObject ) {
1114
+
1115
+ var deferred = q . defer ( ) ;
1116
+
1117
+ var collection = global . mongoClient . db ( appId ) . collection ( tableName ) ;
1118
+ collection . updateMany ( { } , { $rename : renameColumnObject } , function ( err , doc ) {
1119
+ if ( err )
1120
+ deferred . reject ( )
1121
+ else
1122
+ deferred . resolve ( ) ;
1123
+ } ) ;
1124
+
1125
+ return deferred . promise ;
1126
+
1127
+ }
1128
+
1079
1129
function _isBasicDataType ( dataType ) {
1080
1130
try {
1081
1131
var types = global . cloudBoostHelper . getBasicDataTypes ( ) ;
@@ -1284,12 +1334,7 @@ function _checkValidDataType(columns, deafultDataType, tableType) {
1284
1334
if ( columns [ index ] . relationType != null || columns [ index ] . required != false || columns [ index ] . unique != false || columns [ index ] . dataType != 'Object' )
1285
1335
return false ;
1286
1336
}
1287
- // //name for file table
1288
- // if (key === 'fileName') {
1289
- // if (columns[index].relationType != null || columns[index].required != true || columns[index].unique != false || columns[index].dataType != 'Text')
1290
- // return false;
1291
- // }
1292
- // //size for file table
1337
+
1293
1338
if ( key === 'size' ) {
1294
1339
if ( columns [ index ] . relationType != null || columns [ index ] . required != true || columns [ index ] . unique != false || columns [ index ] . dataType != 'Number' )
1295
1340
return false ;
@@ -1312,7 +1357,7 @@ function _checkValidDataType(columns, deafultDataType, tableType) {
1312
1357
1313
1358
//user for event table
1314
1359
if ( key === 'user' ) {
1315
- if ( columns [ index ] . relationType != null || columns [ index ] . required != true || columns [ index ] . unique != false || columns [ index ] . dataType != 'Relation' )
1360
+ if ( columns [ index ] . relationType != null || columns [ index ] . required != false || columns [ index ] . unique != false || columns [ index ] . dataType != 'Relation' )
1316
1361
return false ;
1317
1362
}
1318
1363
0 commit comments