migrations.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Anytime you change the schema of one of the collection in a non-backward
  2. // compatible way you have to write a migration in this file using the following
  3. // API:
  4. //
  5. // Migrations.add(name, migrationCallback, optionalOrder);
  6. Migrations.add('board-background-color', function() {
  7. var defaultColor = '#16A085';
  8. Boards.update({
  9. background: {
  10. $exists: false
  11. }
  12. }, {
  13. $set: {
  14. background: {
  15. type: 'color',
  16. color: defaultColor
  17. }
  18. }
  19. }, {
  20. multi: true
  21. });
  22. });
  23. Migrations.add('lowercase-board-permission', function() {
  24. _.forEach(['Public', 'Private'], function(permission) {
  25. Boards.update(
  26. { permission: permission },
  27. { $set: { permission: permission.toLowerCase() } },
  28. { multi: true }
  29. );
  30. });
  31. });
  32. // Security migration: see https://github.com/libreboard/libreboard/issues/99
  33. Migrations.add('change-attachments-type-for-non-images', function() {
  34. var newTypeForNonImage = 'application/octet-stream';
  35. Attachments.find().forEach(function(file) {
  36. if (! file.isImage()) {
  37. Attachments.update(file._id, {
  38. $set: {
  39. 'original.type': newTypeForNonImage,
  40. 'copies.attachments.type': newTypeForNonImage
  41. }
  42. });
  43. }
  44. });
  45. });
  46. Migrations.add('card-covers', function() {
  47. Cards.find().forEach(function(card) {
  48. var cover = Attachments.findOne({ cardId: card._id, cover: true });
  49. if (cover) {
  50. Cards.update(card._id, {$set: {coverId: cover._id}});
  51. }
  52. });
  53. Attachments.update({}, {$unset: {cover: ''}}, {multi: true});
  54. });
  55. Migrations.add('use-css-class-for-boards-colors', function() {
  56. var associationTable = {
  57. '#27AE60': 'nephritis',
  58. '#C0392B': 'pomegranate',
  59. '#2980B9': 'belize',
  60. '#8E44AD': 'wisteria',
  61. '#2C3E50': 'midnight',
  62. '#E67E22': 'pumpkin'
  63. };
  64. Boards.find().forEach(function(board) {
  65. var oldBoardColor = board.background.color;
  66. var newBoardColor = associationTable[oldBoardColor];
  67. Boards._collection.update({ _id: board._id }, {
  68. $set: { color: newBoardColor },
  69. $unset: { background: '' }
  70. });
  71. });
  72. });
  73. Migrations.add('denormalize-star-number-per-board', function() {
  74. Boards.find().forEach(function(board) {
  75. var nStars = Users.find({'profile.starredBoards': board._id}).count();
  76. Boards.update(board._id, {$set: {stars: nStars}});
  77. });
  78. });
  79. // We want to keep a trace of former members so we can efficiently publish their
  80. // infos in the general board publication.
  81. Migrations.add('add-member-isactive-field', function() {
  82. Boards.find({}, {fields: {members: 1}}).forEach(function(board) {
  83. var allUsersWithSomeActivity = _.chain(
  84. Activities.find({boardId: board._id}, {fields:{userId:1}}).fetch())
  85. .pluck('userId')
  86. .uniq()
  87. .value();
  88. var currentUsers = _.pluck(board.members, 'userId');
  89. var formerUsers = _.difference(allUsersWithSomeActivity, currentUsers);
  90. var newMemberSet = [];
  91. _.forEach(board.members, function(member) {
  92. member.isActive = true;
  93. newMemberSet.push(member);
  94. });
  95. _.forEach(formerUsers, function(userId) {
  96. newMemberSet.push({
  97. userId: userId,
  98. isAdmin: false,
  99. isActive: false
  100. });
  101. });
  102. Boards._collection.update({_id: board._id},
  103. {$set: {members: newMemberSet}});
  104. });
  105. });