codecombat/scripts/mongodb/migrations/2016-09-16-fix-patch-originals.js
Scott Erickson be1c1323ac Patch fixes
* Partially revert GET /db/:collection/:handle/patches, as it returned limited results for versioned docs
* Fix POST /db/:collection/:handle/patch to:
   * normalize diffs based on latest doc version
   * handle empty deltas
   * not swallow thrown errors due to _.curry
   * Set 'target.original' correctly for versioned collections
2016-09-16 16:02:56 -07:00

24 lines
834 B
JavaScript

// Usage: Paste into mongodb client
VERSIONED_COLLECTIONS = {
'article': db.articles,
'level': db.levels,
'level_component': db.level.components,
'level_system': db.level.systems,
'thang_type': db.thang.types
};
db.patches.find({created:{$gt: '2016-09-08'}}).forEach(function(patch) {
if (!VERSIONED_COLLECTIONS[patch.target.collection]) {
print('skip', patch.target.collection);
return;
}
if (patch.target.original && patch.target.id && patch.target.original.equals(patch.target.id)) {
target = VERSIONED_COLLECTIONS[patch.target.collection].findOne({_id: patch.target.original});
print('found original', target, target._id, target.original);
db.patches.update({_id: patch._id}, {$set: {'target.original': target.original}});
}
else {
print('They are different, they are fine');
}
});