Skip to content

Commit 0a0d4f6

Browse files
committed
Finish implementation of delete field from schema
1 parent b0c4b8f commit 0a0d4f6

2 files changed

Lines changed: 66 additions & 8 deletions

File tree

spec/Schema.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,50 @@ describe('Schema', () => {
520520
});
521521
})
522522
});
523+
524+
it('can delete string fields and resave as number field', done => {
525+
var obj1 = hasAllPODobject();
526+
var obj2 = hasAllPODobject();
527+
var p = Parse.Object.saveAll([obj1, obj2])
528+
.then(() => config.database.loadSchema())
529+
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.db, 'test_'))
530+
.then(() => new Parse.Query('HasAllPOD').get(obj1.id))
531+
.then(obj1 => {
532+
expect(obj1.get('aString')).toEqual(undefined);
533+
obj1.set('aString', ['not a string', 'this time']);
534+
obj1.save()
535+
.then(obj1 => {
536+
expect(obj1.get('aString')).toEqual(['not a string', 'this time']);
537+
return new Parse.Query('HasAllPOD').get(obj2.id);
538+
})
539+
.then(obj2 => {
540+
expect(obj2.get('aString')).toEqual(undefined);
541+
done();
542+
});
543+
})
544+
});
545+
546+
it('can delete pointer fields and resave as string', done => {
547+
var obj1 = new Parse.Object('NewClass');
548+
obj1.save()
549+
.then(() => {
550+
obj1.set('aPointer', obj1);
551+
return obj1.save();
552+
})
553+
.then(obj1 => {
554+
expect(obj1.get('aPointer').id).toEqual(obj1.id);
555+
})
556+
.then(() => config.database.loadSchema())
557+
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database.db, 'test_'))
558+
.then(() => new Parse.Query('NewClass').get(obj1.id))
559+
.then(obj1 => {
560+
expect(obj1.get('aPointer')).toEqual(undefined);
561+
obj1.set('aPointer', 'Now a string');
562+
return obj1.save();
563+
})
564+
.then(obj1 => {
565+
expect(obj1.get('aPointer')).toEqual('Now a string');
566+
done();
567+
});
568+
});
523569
});

src/Schema.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,20 +458,32 @@ Schema.prototype.deleteField = function(fieldName, className, database, prefix)
458458
});
459459
}
460460

461-
let p = null;
462461
if (schema.data[className][fieldName].startsWith('relation')) {
463462
//For relations, drop the _Join table
464-
465-
p = database.dropCollection(prefix + '_Join:' + fieldName + ':' + className);
463+
return database.dropCollection(prefix + '_Join:' + fieldName + ':' + className)
464+
//Save the _SCHEMA object
465+
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
466466
} else {
467467
//for non-relations, remove all the data. This is necessary to ensure that the data is still gone
468468
//if they add the same field.
469-
p = Promise.resolve();
469+
return new Promise((resolve, reject) => {
470+
database.collection(prefix + className, (err, coll) => {
471+
if (err) {
472+
reject(err);
473+
} else {
474+
return coll.update({}, {
475+
"$unset": { [fieldName] : null },
476+
}, {
477+
multi: true,
478+
})
479+
//Save the _SCHEMA object
480+
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}))
481+
.then(resolve)
482+
.catch(reject);
483+
}
484+
});
485+
});
470486
}
471-
return p.then(() => {
472-
//Save the _SCHEMA object
473-
return Promise.resolve();
474-
});
475487
});
476488
});
477489
}

0 commit comments

Comments
 (0)