Finish implementation of delete field from schema

This commit is contained in:
Drew Gross
2016-02-10 14:11:42 -08:00
parent b0c4b8f6ce
commit 0a0d4f65ef
2 changed files with 66 additions and 8 deletions

View File

@@ -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();
});
});
});

View File

@@ -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();
});
});
});
}