@@ -341,12 +341,9 @@ class SchemaController {
341
341
}
342
342
343
343
updateClass ( className , submittedFields , classLevelPermissions , database ) {
344
- return this . hasClass ( className )
345
- . then ( hasClass => {
346
- if ( ! hasClass ) {
347
- throw new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , `Class ${ className } does not exist.` ) ;
348
- }
349
- let existingFields = Object . assign ( this . data [ className ] , { _id : className } ) ;
344
+ return this . getOneSchema ( className )
345
+ . then ( schema => {
346
+ let existingFields = schema . fields ;
350
347
Object . keys ( submittedFields ) . forEach ( name => {
351
348
let field = submittedFields [ name ] ;
352
349
if ( existingFields [ name ] && field . __op !== 'Delete' ) {
@@ -360,7 +357,7 @@ class SchemaController {
360
357
delete existingFields . _rperm ;
361
358
delete existingFields . _wperm ;
362
359
let newSchema = buildMergedSchemaObject ( existingFields , submittedFields ) ;
363
- let validationError = this . validateSchemaData ( className , newSchema , classLevelPermissions ) ;
360
+ let validationError = this . validateSchemaData ( className , newSchema , classLevelPermissions , Object . keys ( existingFields ) ) ;
364
361
if ( validationError ) {
365
362
throw new Parse . Error ( validationError . code , validationError . error ) ;
366
363
}
@@ -395,6 +392,13 @@ class SchemaController {
395
392
classLevelPermissions : this . perms [ className ]
396
393
} ) ) ;
397
394
} )
395
+ . catch ( error => {
396
+ if ( error === undefined ) {
397
+ throw new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , `Class ${ className } does not exist.` ) ;
398
+ } else {
399
+ throw error ;
400
+ }
401
+ } )
398
402
}
399
403
400
404
// Returns a promise that resolves successfully to the new schema
@@ -436,25 +440,27 @@ class SchemaController {
436
440
error : invalidClassNameMessage ( className ) ,
437
441
} ;
438
442
}
439
- return this . validateSchemaData ( className , fields , classLevelPermissions ) ;
443
+ return this . validateSchemaData ( className , fields , classLevelPermissions , [ ] ) ;
440
444
}
441
445
442
- validateSchemaData ( className , fields , classLevelPermissions ) {
446
+ validateSchemaData ( className , fields , classLevelPermissions , existingFieldNames ) {
443
447
for ( let fieldName in fields ) {
444
- if ( ! fieldNameIsValid ( fieldName ) ) {
445
- return {
446
- code : Parse . Error . INVALID_KEY_NAME ,
447
- error : 'invalid field name: ' + fieldName ,
448
- } ;
449
- }
450
- if ( ! fieldNameIsValidForClass ( fieldName , className ) ) {
451
- return {
452
- code : 136 ,
453
- error : 'field ' + fieldName + ' cannot be added' ,
454
- } ;
448
+ if ( ! existingFieldNames . includes ( fieldName ) ) {
449
+ if ( ! fieldNameIsValid ( fieldName ) ) {
450
+ return {
451
+ code : Parse . Error . INVALID_KEY_NAME ,
452
+ error : 'invalid field name: ' + fieldName ,
453
+ } ;
454
+ }
455
+ if ( ! fieldNameIsValidForClass ( fieldName , className ) ) {
456
+ return {
457
+ code : 136 ,
458
+ error : 'field ' + fieldName + ' cannot be added' ,
459
+ } ;
460
+ }
461
+ const error = fieldTypeIsInvalid ( fields [ fieldName ] ) ;
462
+ if ( error ) return { code : error . code , error : error . message } ;
455
463
}
456
- const error = fieldTypeIsInvalid ( fields [ fieldName ] ) ;
457
- if ( error ) return { code : error . code , error : error . message } ;
458
464
}
459
465
460
466
for ( let fieldName in defaultColumns [ className ] ) {
@@ -552,19 +558,19 @@ class SchemaController {
552
558
throw new Parse . Error ( 136 , `field ${ fieldName } cannot be changed` ) ;
553
559
}
554
560
555
- return this . reloadData ( )
556
- . then ( ( ) => this . hasClass ( className ) )
557
- . then ( hasClass => {
558
- if ( ! hasClass ) {
561
+ return this . getOneSchema ( className )
562
+ . catch ( error => {
563
+ if ( error === undefined ) {
559
564
throw new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , `Class ${ className } does not exist.` ) ;
560
- }
561
- if ( ! this . data [ className ] [ fieldName ] ) {
562
- throw new Parse . Error ( 255 , `Field ${ fieldName } does not exist, cannot delete.` ) ;
565
+ } else {
566
+ throw error ;
563
567
}
564
568
} )
565
- . then ( ( ) => this . getOneSchema ( className ) )
566
569
. then ( schema => {
567
- if ( this . data [ className ] [ fieldName ] . type == 'Relation' ) {
570
+ if ( ! schema . fields [ fieldName ] ) {
571
+ throw new Parse . Error ( 255 , `Field ${ fieldName } does not exist, cannot delete.` ) ;
572
+ }
573
+ if ( schema . fields [ fieldName ] . type == 'Relation' ) {
568
574
//For relations, drop the _Join table
569
575
return database . adapter . deleteFields ( className , schema , [ fieldName ] )
570
576
. then ( ( ) => database . adapter . deleteClass ( `_Join:${ fieldName } :${ className } ` ) ) ;
0 commit comments