From cf630ba462aafe1eafd299548d670b5392f19492 Mon Sep 17 00:00:00 2001 From: Anthony Mosca Date: Tue, 19 Sep 2017 20:42:40 +0930 Subject: [PATCH] Updating with two GeoPoints fails correctly. (#4162) --- spec/ParseGeoPoint.spec.js | 21 +++++++++++++++++-- .../Storage/Mongo/MongoSchemaCollection.js | 6 +++++- src/Controllers/SchemaController.js | 8 ++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/spec/ParseGeoPoint.spec.js b/spec/ParseGeoPoint.spec.js index f2f8201e..b7f38fc5 100644 --- a/spec/ParseGeoPoint.spec.js +++ b/spec/ParseGeoPoint.spec.js @@ -68,8 +68,7 @@ describe('Parse.GeoPoint testing', () => { }) }); - - it('geo point exception two fields', (done) => { + it('creating geo point exception two fields', (done) => { var point = new Parse.GeoPoint(20, 20); var obj = new TestObject(); obj.set('locationOne', point); @@ -82,6 +81,24 @@ describe('Parse.GeoPoint testing', () => { }); }); + // TODO: This should also have support in postgres, or higher level database agnostic support. + it_exclude_dbs(['postgres'])('updating geo point exception two fields', (done) => { + var point = new Parse.GeoPoint(20, 20); + var obj = new TestObject(); + obj.set('locationOne', point); + obj.save(null, { + success: (obj) => { + obj.set('locationTwo', point); + obj.save().then(() => { + fail('expected error'); + }, (err) => { + equal(err.code, Parse.Error.INCORRECT_TYPE); + done(); + }) + } + }); + }); + it('geo line', (done) => { var line = []; for (var i = 0; i < 10; ++i) { diff --git a/src/Adapters/Storage/Mongo/MongoSchemaCollection.js b/src/Adapters/Storage/Mongo/MongoSchemaCollection.js index 6e9e0ddd..051bac65 100644 --- a/src/Adapters/Storage/Mongo/MongoSchemaCollection.js +++ b/src/Adapters/Storage/Mongo/MongoSchemaCollection.js @@ -152,7 +152,11 @@ class MongoSchemaCollection { addFieldIfNotExists(className: string, fieldName: string, type: string) { return this._fetchOneSchemaFrom_SCHEMA(className) .then(schema => { - // The schema exists. Check for existing GeoPoints. + // If a field with this name already exists, it will be handled elsewhere. + if (schema.fields[fieldName] != undefined) { + return; + } + // The schema exists. Check for existing GeoPoints. if (type.type === 'GeoPoint') { // Make sure there are not other geopoint fields if (Object.keys(schema.fields).some(existingField => schema.fields[existingField].type === 'GeoPoint')) { diff --git a/src/Controllers/SchemaController.js b/src/Controllers/SchemaController.js index 507e155d..6c60575b 100644 --- a/src/Controllers/SchemaController.js +++ b/src/Controllers/SchemaController.js @@ -661,9 +661,11 @@ export default class SchemaController { return this._dbAdapter.addFieldIfNotExists(className, fieldName, type).then(() => { // The update succeeded. Reload the schema return this.reloadData({ clearCache: true }); - }, () => { - //TODO: introspect the error and only reload if the error is one for which is makes sense to reload - + }, (error) => { + if (error.code == Parse.Error.INCORRECT_TYPE) { + // Make sure that we throw errors when it is appropriate to do so. + throw error; + } // The update failed. This can be okay - it might have been a race // condition where another client updated the schema in the same // way that we wanted to. So, just reload the schema