2014-07-03 09:30:10 +09:00
|
|
|
// TODO: switch over to Tom's migration package.
|
|
|
|
|
2013-04-06 12:38:28 +09:00
|
|
|
// database migrations
|
|
|
|
// http://stackoverflow.com/questions/10365496/meteor-how-to-perform-database-migrations
|
|
|
|
Migrations = new Meteor.Collection('migrations');
|
|
|
|
|
|
|
|
Meteor.startup(function () {
|
2014-06-23 14:28:37 +09:00
|
|
|
allMigrations = Object.keys(migrationsList);
|
|
|
|
_.each(allMigrations, function(migrationName){
|
|
|
|
runMigration(migrationName);
|
|
|
|
});
|
|
|
|
});
|
2013-04-06 12:38:28 +09:00
|
|
|
|
2014-06-23 14:28:37 +09:00
|
|
|
// wrapper function for all migrations
|
|
|
|
var runMigration = function (migrationName) {
|
|
|
|
// migration updatePostStatus: make sure posts have a status
|
|
|
|
if (!Migrations.findOne({name: migrationName})) {
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log("//----------------------------------------------------------------------//");
|
2014-06-23 14:28:37 +09:00
|
|
|
console.log("//------------// Starting "+migrationName+" Migration //-----------//");
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log("//----------------------------------------------------------------------//");
|
2014-06-23 17:16:04 +09:00
|
|
|
Migrations.insert({name: migrationName, startedAt: new Date(), completed: false});
|
2013-04-06 14:27:08 +09:00
|
|
|
|
2014-06-23 14:28:37 +09:00
|
|
|
// execute migration function
|
2014-06-23 17:16:04 +09:00
|
|
|
var itemsAffected = migrationsList[migrationName]() || 0;
|
2014-06-23 14:28:37 +09:00
|
|
|
|
2014-06-23 17:16:04 +09:00
|
|
|
Migrations.update({name: migrationName}, {$set: {finishedAt: new Date(), completed: true, itemsAffected: itemsAffected}});
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log("//----------------------------------------------------------------------//");
|
2014-06-23 14:28:37 +09:00
|
|
|
console.log("//------------// Ending "+migrationName+" Migration //-----------//");
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log("//----------------------------------------------------------------------//");
|
2013-04-06 14:27:08 +09:00
|
|
|
}
|
2014-06-23 14:28:37 +09:00
|
|
|
}
|
2013-04-06 14:27:08 +09:00
|
|
|
|
2014-06-23 14:28:37 +09:00
|
|
|
var migrationsList = {
|
|
|
|
updatePostStatus: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-06-23 14:28:37 +09:00
|
|
|
Posts.find({status: {$exists : false}}).forEach(function (post) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2014-06-23 14:28:37 +09:00
|
|
|
Posts.update(post._id, {$set: {status: 2}});
|
|
|
|
console.log("---------------------");
|
|
|
|
console.log("Post: "+post.title);
|
|
|
|
console.log("Updating status to approved");
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
updateCategories: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
|
|
|
Categories.find({slug: {$exists : false}}).forEach(function (category) {
|
|
|
|
i++;
|
2013-04-06 12:38:28 +09:00
|
|
|
var slug = slugify(category.name);
|
|
|
|
Categories.update(category._id, {$set: {slug: slug}});
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log("---------------------");
|
2013-04-06 12:38:28 +09:00
|
|
|
console.log("Category: "+category.name);
|
|
|
|
console.log("Updating category with new slug: "+slug);
|
2014-06-23 17:08:25 +09:00
|
|
|
|
2013-04-06 12:38:28 +09:00
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
updatePostCategories: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2013-04-06 12:38:28 +09:00
|
|
|
Posts.find().forEach(function (post) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2013-04-06 12:38:28 +09:00
|
|
|
var oldCategories = post.categories;
|
|
|
|
var newCategories = [];
|
|
|
|
var category = {};
|
|
|
|
var updating = false; // by default, assume we're not going to do anything
|
|
|
|
|
|
|
|
// iterate over the post.categories array
|
|
|
|
// if the post has no categories then nothing will happen
|
|
|
|
_.each(oldCategories, function(value, key, list){
|
|
|
|
// make sure the categories are strings
|
|
|
|
if((typeof value === "string") && (category = Categories.findOne({name: value}))){
|
|
|
|
// if value is a string, then look for the matching category object
|
|
|
|
// and if it exists push it to the newCategories array
|
|
|
|
updating = true; // we're updating at least one category for this post
|
|
|
|
newCategories.push(category);
|
|
|
|
}else{
|
|
|
|
// if category A) is already an object, or B) it's a string but a matching category object doesn't exist
|
|
|
|
// just keep the current value
|
|
|
|
newCategories.push(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(updating){
|
|
|
|
// update categories property on post
|
|
|
|
Posts.update(post._id, {$set: {categories: newCategories}});
|
|
|
|
}
|
|
|
|
|
|
|
|
// START CONSOLE LOGS
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log("---------------------");
|
2014-05-16 09:19:35 +09:00
|
|
|
console.log("Post: "+post.title);
|
2013-04-06 12:38:28 +09:00
|
|
|
if(updating){
|
2014-05-06 20:15:48 -07:00
|
|
|
console.log(oldCategories.length+" categories: "+oldCategories);
|
2013-04-06 12:38:28 +09:00
|
|
|
console.log("Updating categories array to: ");
|
|
|
|
console.log(newCategories);
|
|
|
|
}else{
|
|
|
|
console.log("No updates");
|
|
|
|
}
|
|
|
|
// END CONSOLE LOGS
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
updateUserProfiles: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2013-11-16 13:13:21 +09:00
|
|
|
var allUsers = Meteor.users.find();
|
|
|
|
console.log('> Found '+allUsers.count()+' users.\n');
|
|
|
|
|
|
|
|
allUsers.forEach(function(user){
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2013-11-16 13:13:21 +09:00
|
|
|
console.log('> Updating user '+user._id+' ('+user.username+')');
|
|
|
|
|
|
|
|
// update user slug
|
|
|
|
if(getUserName(user))
|
|
|
|
Meteor.users.update(user._id, {$set:{slug: slugify(getUserName(user))}});
|
|
|
|
|
|
|
|
// update user isAdmin flag
|
|
|
|
if(typeof user.isAdmin === 'undefined')
|
|
|
|
Meteor.users.update(user._id, {$set: {isAdmin: false}});
|
2013-04-06 12:38:28 +09:00
|
|
|
|
2013-11-16 13:13:21 +09:00
|
|
|
// update postCount
|
|
|
|
var postsByUser = Posts.find({userId: user._id});
|
|
|
|
Meteor.users.update(user._id, {$set: {postCount: postsByUser.count()}});
|
|
|
|
|
|
|
|
// update commentCount
|
|
|
|
var commentsByUser = Comments.find({userId: user._id});
|
|
|
|
Meteor.users.update(user._id, {$set: {commentCount: commentsByUser.count()}});
|
|
|
|
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
resetUpvotesDownvotes: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-05-15 15:56:23 +09:00
|
|
|
Posts.find().forEach(function (post) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2014-05-15 15:56:23 +09:00
|
|
|
var upvotes = 0,
|
|
|
|
downvotes = 0;
|
2014-05-16 09:19:35 +09:00
|
|
|
console.log("Post: "+post.title);
|
2014-05-15 15:56:23 +09:00
|
|
|
if(post.upvoters){
|
|
|
|
upvotes = post.upvoters.length;
|
|
|
|
console.log("Found "+upvotes+" upvotes.")
|
|
|
|
}
|
|
|
|
if(post.downvoters){
|
|
|
|
downvotes = post.downvoters.length;
|
|
|
|
console.log("Found "+downvotes+" downvotes.")
|
|
|
|
}
|
|
|
|
Posts.update(post._id, {$set: {upvotes: upvotes, downvotes: downvotes}});
|
|
|
|
console.log("---------------------");
|
2014-06-23 17:08:25 +09:00
|
|
|
});
|
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
resetCommentsUpvotesDownvotes: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-05-15 15:56:23 +09:00
|
|
|
Comments.find().forEach(function (comment) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2014-05-15 15:56:23 +09:00
|
|
|
var upvotes = 0,
|
|
|
|
downvotes = 0;
|
|
|
|
console.log("Comment: "+comment._id);
|
|
|
|
if(comment.upvoters){
|
|
|
|
upvotes = comment.upvoters.length;
|
|
|
|
console.log("Found "+upvotes+" upvotes.")
|
|
|
|
}
|
|
|
|
if(comment.downvoters){
|
|
|
|
downvotes = comment.downvoters.length;
|
|
|
|
console.log("Found "+downvotes+" downvotes.")
|
|
|
|
}
|
|
|
|
Comments.update(comment._id, {$set: {upvotes: upvotes, downvotes: downvotes}});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
headlineToTitle: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-06-23 17:09:06 +09:00
|
|
|
Posts.find({title: {$exists : false}}).forEach(function (post) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2014-05-16 09:19:35 +09:00
|
|
|
console.log("Post: "+post.headline+" "+post.title);
|
|
|
|
Posts.update(post._id, { $rename: { 'headline': 'title'}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
commentsSubmittedToCreatedAt: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-06-22 10:34:25 +09:00
|
|
|
Comments.find().forEach(function (comment) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2014-06-22 10:34:25 +09:00
|
|
|
console.log("Comment: "+comment._id);
|
|
|
|
Comments.update(comment._id, { $rename: { 'submitted': 'createdAt'}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
commentsPostToPostId: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-06-23 17:09:06 +09:00
|
|
|
Comments.find({postId: {$exists : false}}).forEach(function (comment) {
|
2014-06-23 17:08:25 +09:00
|
|
|
i++;
|
2014-06-22 10:34:25 +09:00
|
|
|
console.log("Comment: "+comment._id);
|
|
|
|
Comments.update(comment._id, { $rename: { 'post': 'postId'}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
createdAtSubmittedToDate: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-06-22 11:09:12 +09:00
|
|
|
Posts.find().forEach(function (post) {
|
2014-06-23 17:08:25 +09:00
|
|
|
if(typeof post.submitted == "number" || typeof post.createdAt == "number"){
|
|
|
|
i++;
|
|
|
|
console.log("Posts: "+post.title);
|
|
|
|
var createdAt = new Date(post.createdAt);
|
|
|
|
var submitted = new Date(post.submitted);
|
|
|
|
console.log(createdAt)
|
|
|
|
Posts.update(post._id, { $set: { 'createdAt': createdAt, submitted: submitted}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
}
|
2014-06-22 11:09:12 +09:00
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-06-23 14:28:37 +09:00
|
|
|
},
|
|
|
|
commentsCreatedAtToDate: function () {
|
2014-06-23 17:08:25 +09:00
|
|
|
var i = 0;
|
2014-06-22 11:09:12 +09:00
|
|
|
Comments.find().forEach(function (comment) {
|
2014-06-23 17:08:25 +09:00
|
|
|
if(typeof comment.createdAt == "number"){
|
|
|
|
i++;
|
|
|
|
console.log("Comment: "+comment._id);
|
|
|
|
var createdAt = new Date(comment.createdAt);
|
|
|
|
console.log(createdAt)
|
|
|
|
Comments.update(comment._id, { $set: { 'createdAt': createdAt}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
}
|
2014-06-22 11:09:12 +09:00
|
|
|
});
|
2014-06-23 17:08:25 +09:00
|
|
|
return i;
|
2014-07-03 13:15:23 +09:00
|
|
|
},
|
|
|
|
submittedToPostedAt: function () {
|
|
|
|
var i = 0;
|
|
|
|
Posts.find({postedAt: {$exists : false}}).forEach(function (post) {
|
|
|
|
i++;
|
|
|
|
console.log("Post: "+post._id);
|
|
|
|
Posts.update(post._id, { $rename: { 'submitted': 'postedAt'}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
|
|
|
return i;
|
|
|
|
},
|
|
|
|
addPostedAtToComments: function () {
|
|
|
|
var i = 0;
|
|
|
|
Comments.find({postedAt: {$exists : false}}).forEach(function (comment) {
|
|
|
|
i++;
|
|
|
|
console.log("Comment: "+comment._id);
|
|
|
|
Comments.update(comment._id, { $set: { 'postedAt': comment.createdAt}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
|
|
|
return i;
|
2014-07-05 16:21:28 +09:00
|
|
|
},
|
|
|
|
parentToParentCommentId: function () {
|
|
|
|
var i = 0;
|
|
|
|
Comments.find({parentCommentId: {$exists : false}}).forEach(function (comment) {
|
|
|
|
i++;
|
|
|
|
console.log("Comment: "+comment._id);
|
|
|
|
Comments.update(comment._id, { $set: { 'parentCommentId': comment.parent}}, {multi: true, validate: false});
|
|
|
|
console.log("---------------------");
|
|
|
|
});
|
|
|
|
return i;
|
2014-07-19 15:08:28 +09:00
|
|
|
},
|
2014-07-22 10:29:37 +09:00
|
|
|
addLastCommentedAt: function () {
|
2014-07-19 15:08:28 +09:00
|
|
|
var i = 0;
|
|
|
|
Posts.find({comments: {$gt: 0}}).forEach(function (post) {
|
|
|
|
i++;
|
|
|
|
console.log("Post: "+post._id);
|
|
|
|
var postComments = Comments.find({postId: post._id}, {sort: {postedAt: -1}}).fetch();
|
|
|
|
var lastComment = postComments[0];
|
2014-07-22 10:29:37 +09:00
|
|
|
Posts.update(post._id, { $set: { lastCommentedAt: lastComment.postedAt}}, {multi: false, validate: false});
|
2014-07-19 15:08:28 +09:00
|
|
|
console.log("---------------------");
|
|
|
|
});
|
|
|
|
return i;
|
2014-06-22 11:09:12 +09:00
|
|
|
}
|
2014-06-23 14:28:37 +09:00
|
|
|
}
|