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