Skip to content

Commit

Permalink
Update tag script
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Jan 20, 2020
1 parent 59150a6 commit b412425
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions scripts/blog/fix/tag-ghosts.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
var Tags = require("../../../app/models/tags");
var Entry = require("../../../app/models/entry");
var async = require('async');
var async = require("async");
var get = require("../../get/blog");
var client = require('client');
var client = require("client");

if (require.main === module) {
get(process.argv[2], function(err, user, blog) {
if (err) throw err;
main(blog, function(err) {
if (err) {
console.error(colors.red("Error:", err.message));
return process.exit(1);
}
if (err) throw err;
process.exit();
});
});
Expand All @@ -20,32 +17,36 @@ if (require.main === module) {
function main(blog, callback) {
console.log("Blog", blog.id, "Fixing tags");
Tags.list(blog.id, function(err, tags) {
async.eachSeries(tags, function(tag, next) {
Tags.get(blog.id, tag.slug, function(err, entryIDs) {
async.each(
entryIDs,
function(entryID, next) {
Entry.get(blog.id, entryID, function(entry) {
if (entry.id === entryID) return next();
console.log("MISMATCH", entryID, entry.id);
var multi = client.multi();
var entryKeyForIncorrectID = Tags.key.entry(blog.id, entryID)
var entryKeyForCorrectID = Tags.key.entry(blog.id, entry.id)
var tagKey = Tags.key.tag(blog.id, tag.slug)
async.eachSeries(
tags,
function(tag, next) {
Tags.get(blog.id, tag.slug, function(err, entryIDs) {
async.each(
entryIDs,
function(entryID, next) {
Entry.get(blog.id, entryID, function(entry) {
if (entry.id === entryID) return next();
console.log("MISMATCH", entryID, entry.id);
var multi = client.multi();
var entryKeyForIncorrectID = Tags.key.entry(blog.id, entryID);
var entryKeyForCorrectID = Tags.key.entry(blog.id, entry.id);
var tagKey = Tags.key.tag(blog.id, tag.slug);

multi.rename(entryKeyForIncorrectID, entryKeyForCorrectID)
multi.srem(tagKey, entryID)
multi.sadd(tagKey, entry.id)
multi.exec(function(err){
if (err) return next(err);
Entry.set(blog.id, entry.id, entry, next);
multi.rename(entryKeyForIncorrectID, entryKeyForCorrectID);
multi.srem(tagKey, entryID);
multi.sadd(tagKey, entry.id);
multi.exec(function(err) {
if (err) return next(err);
Entry.set(blog.id, entry.id, entry, next);
});
});
});
},
next
);
});
}, callback)
},
next
);
});
},
callback
);
});
}
module.exports = main;
module.exports = main;

0 comments on commit b412425

Please sign in to comment.