From 0a0d4f65efe86ae14fa0a401bacdf83f9cb53868 Mon Sep 17 00:00:00 2001 From: Drew Gross Date: Wed, 10 Feb 2016 14:11:42 -0800 Subject: [PATCH] Finish implementation of delete field from schema --- spec/Schema.spec.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ src/Schema.js | 28 +++++++++++++++++++-------- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index 9524ea1b..b8222e5f 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -520,4 +520,50 @@ describe('Schema', () => { }); }) }); + + it('can delete string fields and resave as number field', done => { + var obj1 = hasAllPODobject(); + var obj2 = hasAllPODobject(); + var p = Parse.Object.saveAll([obj1, obj2]) + .then(() => config.database.loadSchema()) + .then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.db, 'test_')) + .then(() => new Parse.Query('HasAllPOD').get(obj1.id)) + .then(obj1 => { + expect(obj1.get('aString')).toEqual(undefined); + obj1.set('aString', ['not a string', 'this time']); + obj1.save() + .then(obj1 => { + expect(obj1.get('aString')).toEqual(['not a string', 'this time']); + return new Parse.Query('HasAllPOD').get(obj2.id); + }) + .then(obj2 => { + expect(obj2.get('aString')).toEqual(undefined); + done(); + }); + }) + }); + + it('can delete pointer fields and resave as string', done => { + var obj1 = new Parse.Object('NewClass'); + obj1.save() + .then(() => { + obj1.set('aPointer', obj1); + return obj1.save(); + }) + .then(obj1 => { + expect(obj1.get('aPointer').id).toEqual(obj1.id); + }) + .then(() => config.database.loadSchema()) + .then(schema => schema.deleteField('aPointer', 'NewClass', config.database.db, 'test_')) + .then(() => new Parse.Query('NewClass').get(obj1.id)) + .then(obj1 => { + expect(obj1.get('aPointer')).toEqual(undefined); + obj1.set('aPointer', 'Now a string'); + return obj1.save(); + }) + .then(obj1 => { + expect(obj1.get('aPointer')).toEqual('Now a string'); + done(); + }); + }); }); diff --git a/src/Schema.js b/src/Schema.js index 33dedb99..ecf1a5b3 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -458,20 +458,32 @@ Schema.prototype.deleteField = function(fieldName, className, database, prefix) }); } - let p = null; if (schema.data[className][fieldName].startsWith('relation')) { //For relations, drop the _Join table - - p = database.dropCollection(prefix + '_Join:' + fieldName + ':' + className); + return database.dropCollection(prefix + '_Join:' + fieldName + ':' + className) + //Save the _SCHEMA object + .then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }})); } else { //for non-relations, remove all the data. This is necessary to ensure that the data is still gone //if they add the same field. - p = Promise.resolve(); + return new Promise((resolve, reject) => { + database.collection(prefix + className, (err, coll) => { + if (err) { + reject(err); + } else { + return coll.update({}, { + "$unset": { [fieldName] : null }, + }, { + multi: true, + }) + //Save the _SCHEMA object + .then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }})) + .then(resolve) + .catch(reject); + } + }); + }); } - return p.then(() => { - //Save the _SCHEMA object - return Promise.resolve(); - }); }); }); }