Finish implementation of delete field from schema
This commit is contained in:
@@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -458,20 +458,32 @@ Schema.prototype.deleteField = function(fieldName, className, database, prefix)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let p = null;
|
|
||||||
if (schema.data[className][fieldName].startsWith('relation')) {
|
if (schema.data[className][fieldName].startsWith('relation')) {
|
||||||
//For relations, drop the _Join table
|
//For relations, drop the _Join table
|
||||||
|
return database.dropCollection(prefix + '_Join:' + fieldName + ':' + className)
|
||||||
p = database.dropCollection(prefix + '_Join:' + fieldName + ':' + className);
|
//Save the _SCHEMA object
|
||||||
|
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
|
||||||
} else {
|
} else {
|
||||||
//for non-relations, remove all the data. This is necessary to ensure that the data is still gone
|
//for non-relations, remove all the data. This is necessary to ensure that the data is still gone
|
||||||
//if they add the same field.
|
//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();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user