PageRenderTime 325ms CodeModel.GetById 100ms app.highlight 103ms RepoModel.GetById 119ms app.codeStats 0ms

/server/migrations.js

https://github.com/clouderg-rd/Telescope
JavaScript | 227 lines | 193 code | 15 blank | 19 comment | 18 complexity | 6fc3db3c0dc1c97a77a64b51bfe0c2c4 MD5 | raw file
  1// database migrations
  2// http://stackoverflow.com/questions/10365496/meteor-how-to-perform-database-migrations
  3Migrations = new Meteor.Collection('migrations');
  4
  5Meteor.startup(function () {
  6  allMigrations = Object.keys(migrationsList);
  7  _.each(allMigrations, function(migrationName){
  8    runMigration(migrationName);
  9  });
 10});
 11
 12// wrapper function for all migrations
 13var runMigration = function (migrationName) {
 14  // migration updatePostStatus: make sure posts have a status
 15  if (!Migrations.findOne({name: migrationName})) {
 16    console.log("//----------------------------------------------------------------------//");
 17    console.log("//------------//    Starting "+migrationName+" Migration    //-----------//");
 18    console.log("//----------------------------------------------------------------------//");
 19    Migrations.insert({name: migrationName, startedAt: new Date(), completed: false});
 20
 21    // execute migration function
 22    var itemsAffected = migrationsList[migrationName]() || 0;
 23
 24    Migrations.update({name: migrationName}, {$set: {finishedAt: new Date(), completed: true, itemsAffected: itemsAffected}});
 25    console.log("//----------------------------------------------------------------------//");
 26    console.log("//------------//     Ending "+migrationName+" Migration     //-----------//");
 27    console.log("//----------------------------------------------------------------------//");
 28  }
 29}
 30
 31var migrationsList = {
 32  updatePostStatus: function () {
 33    var i = 0;
 34    Posts.find({status: {$exists : false}}).forEach(function (post) {
 35      i++;
 36      Posts.update(post._id, {$set: {status: 2}});
 37      console.log("---------------------");
 38      console.log("Post: "+post.title);
 39      console.log("Updating status to approved");  
 40    });
 41    return i;
 42  },
 43  updateCategories: function () {
 44    var i = 0;
 45    Categories.find({slug: {$exists : false}}).forEach(function (category) {
 46        i++;
 47        var slug = slugify(category.name);
 48        Categories.update(category._id, {$set: {slug: slug}});
 49        console.log("---------------------");
 50        console.log("Category: "+category.name);
 51        console.log("Updating category with new slug: "+slug);
 52    
 53    });
 54    return i;
 55  },
 56  updatePostCategories: function () {
 57    var i = 0;
 58    Posts.find().forEach(function (post) {
 59      i++;
 60      var oldCategories = post.categories;
 61      var newCategories = [];
 62      var category = {};
 63      var updating = false; // by default, assume we're not going to do anything
 64
 65      // iterate over the post.categories array
 66      // if the post has no categories then nothing will happen
 67      _.each(oldCategories, function(value, key, list){
 68        // make sure the categories are strings
 69        if((typeof value === "string") && (category = Categories.findOne({name: value}))){
 70          // if value is a string, then look for the matching category object
 71          // and if it exists push it to the newCategories array
 72          updating = true; // we're updating at least one category for this post
 73          newCategories.push(category);
 74        }else{
 75          // if category A) is already an object, or B) it's a string but a matching category object doesn't exist
 76          // just keep the current value
 77          newCategories.push(value);
 78        }
 79      });
 80
 81      if(updating){
 82        // update categories property on post
 83        Posts.update(post._id, {$set: {categories: newCategories}});
 84      }
 85
 86      // START CONSOLE LOGS
 87      console.log("---------------------");
 88      console.log("Post: "+post.title);
 89      if(updating){
 90        console.log(oldCategories.length+" categories: "+oldCategories);
 91        console.log("Updating categories array to: ");
 92        console.log(newCategories);
 93      }else{
 94        console.log("No updates");
 95      }
 96      // END CONSOLE LOGS
 97    });
 98    return i;
 99  },
100  updateUserProfiles: function () {
101    var i = 0;
102    var allUsers = Meteor.users.find();
103    console.log('> Found '+allUsers.count()+' users.\n');
104
105    allUsers.forEach(function(user){
106      i++;
107      console.log('> Updating user '+user._id+' ('+user.username+')');
108
109      // update user slug
110      if(getUserName(user))
111        Meteor.users.update(user._id, {$set:{slug: slugify(getUserName(user))}});
112
113      // update user isAdmin flag
114      if(typeof user.isAdmin === 'undefined')
115        Meteor.users.update(user._id, {$set: {isAdmin: false}});
116
117      // update postCount
118      var postsByUser = Posts.find({userId: user._id});
119      Meteor.users.update(user._id, {$set: {postCount: postsByUser.count()}});
120      
121      // update commentCount
122      var commentsByUser = Comments.find({userId: user._id});
123      Meteor.users.update(user._id, {$set: {commentCount: commentsByUser.count()}});
124
125    });
126    return i;
127  },
128  resetUpvotesDownvotes: function () {
129    var i = 0;
130    Posts.find().forEach(function (post) {
131      i++;
132      var upvotes = 0,
133          downvotes = 0;
134      console.log("Post: "+post.title);
135      if(post.upvoters){
136        upvotes = post.upvoters.length;
137        console.log("Found "+upvotes+" upvotes.")
138      }
139      if(post.downvoters){
140        downvotes = post.downvoters.length;
141        console.log("Found "+downvotes+" downvotes.")
142      }
143      Posts.update(post._id, {$set: {upvotes: upvotes, downvotes: downvotes}});
144      console.log("---------------------");
145    });
146    return i;
147  },
148  resetCommentsUpvotesDownvotes: function () {
149    var i = 0;
150    Comments.find().forEach(function (comment) {
151      i++;
152      var upvotes = 0,
153          downvotes = 0;
154      console.log("Comment: "+comment._id);
155      if(comment.upvoters){
156        upvotes = comment.upvoters.length;
157        console.log("Found "+upvotes+" upvotes.")
158      }
159      if(comment.downvoters){
160        downvotes = comment.downvoters.length;
161        console.log("Found "+downvotes+" downvotes.")
162      }
163      Comments.update(comment._id, {$set: {upvotes: upvotes, downvotes: downvotes}});
164      console.log("---------------------");
165    });
166    return i;
167  },
168  headlineToTitle: function () {
169    var i = 0;
170    Posts.find({title: {$exists : false}}).forEach(function (post) {
171      i++;
172      console.log("Post: "+post.headline+" "+post.title);
173      Posts.update(post._id, { $rename: { 'headline': 'title'}}, {multi: true, validate: false});
174      console.log("---------------------");
175    });
176    return i;
177  },
178  commentsSubmittedToCreatedAt: function () {
179    var i = 0;
180    Comments.find().forEach(function (comment) {
181      i++;
182      console.log("Comment: "+comment._id);
183      Comments.update(comment._id, { $rename: { 'submitted': 'createdAt'}}, {multi: true, validate: false});
184      console.log("---------------------");
185    });
186    return i;
187  },
188  commentsPostToPostId: function () {
189    var i = 0;
190    Comments.find({postId: {$exists : false}}).forEach(function (comment) {
191      i++;
192      console.log("Comment: "+comment._id);
193      Comments.update(comment._id, { $rename: { 'post': 'postId'}}, {multi: true, validate: false});
194      console.log("---------------------");
195    });
196    return i;
197  },
198  createdAtSubmittedToDate: function () {
199    var i = 0;
200    Posts.find().forEach(function (post) {
201      if(typeof post.submitted == "number" || typeof post.createdAt == "number"){
202        i++;
203        console.log("Posts: "+post.title);
204        var createdAt = new Date(post.createdAt);
205        var submitted = new Date(post.submitted);
206        console.log(createdAt)
207        Posts.update(post._id, { $set: { 'createdAt': createdAt, submitted: submitted}}, {multi: true, validate: false});
208        console.log("---------------------");
209      }
210    });
211    return i;
212  },
213  commentsCreatedAtToDate: function () {
214    var i = 0;
215    Comments.find().forEach(function (comment) {
216      if(typeof comment.createdAt == "number"){
217        i++;
218        console.log("Comment: "+comment._id);
219        var createdAt = new Date(comment.createdAt);
220        console.log(createdAt)
221        Comments.update(comment._id, { $set: { 'createdAt': createdAt}}, {multi: true, validate: false});
222        console.log("---------------------");
223      }
224    });
225    return i;
226  }
227}