migrations.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. import AccountSettings from '../models/accountSettings';
  2. import Actions from '../models/actions';
  3. import Activities from '../models/activities';
  4. import Announcements from '../models/announcements';
  5. import Boards from '../models/boards';
  6. import CardComments from '../models/cardComments';
  7. import Cards from '../models/cards';
  8. import ChecklistItems from '../models/checklistItems';
  9. import Checklists from '../models/checklists';
  10. import CustomFields from '../models/customFields';
  11. import Integrations from '../models/integrations';
  12. import InvitationCodes from '../models/invitationCodes';
  13. import Lists from '../models/lists';
  14. import Rules from '../models/rules';
  15. import Settings from '../models/settings';
  16. import Swimlanes from '../models/swimlanes';
  17. import Triggers from '../models/triggers';
  18. import UnsavedEdits from '../models/unsavedEdits';
  19. import Users from '../models/users';
  20. // Anytime you change the schema of one of the collection in a non-backward
  21. // compatible way you have to write a migration in this file using the following
  22. // API:
  23. //
  24. // Migrations.add(name, migrationCallback, optionalOrder);
  25. // Note that we have extra migrations defined in `sandstorm.js` that are
  26. // exclusive to Sandstorm and shouldn’t be executed in the general case.
  27. // XXX I guess if we had ES6 modules we could
  28. // `import { isSandstorm } from sandstorm.js` and define the migration here as
  29. // well, but for now I want to avoid definied too many globals.
  30. // In the context of migration functions we don't want to validate database
  31. // mutation queries against the current (ie, latest) collection schema. Doing
  32. // that would work at the time we write the migration but would break in the
  33. // future when we'll update again the concerned collection schema.
  34. //
  35. // To prevent this bug we always have to disable the schema validation and
  36. // argument transformations. We generally use the shorthandlers defined below.
  37. const noValidate = {
  38. validate: false,
  39. filter: false,
  40. autoConvert: false,
  41. removeEmptyStrings: false,
  42. getAutoValues: false,
  43. };
  44. const noValidateMulti = { ...noValidate, multi: true };
  45. Migrations.add('board-background-color', () => {
  46. const defaultColor = '#16A085';
  47. Boards.update(
  48. {
  49. background: {
  50. $exists: false,
  51. },
  52. },
  53. {
  54. $set: {
  55. background: {
  56. type: 'color',
  57. color: defaultColor,
  58. },
  59. },
  60. },
  61. noValidateMulti,
  62. );
  63. });
  64. Migrations.add('lowercase-board-permission', () => {
  65. ['Public', 'Private'].forEach(permission => {
  66. Boards.update(
  67. { permission },
  68. { $set: { permission: permission.toLowerCase() } },
  69. noValidateMulti,
  70. );
  71. });
  72. });
  73. // Security migration: see https://github.com/wekan/wekan/issues/99
  74. Migrations.add('change-attachments-type-for-non-images', () => {
  75. const newTypeForNonImage = 'application/octet-stream';
  76. Attachments.find().forEach(file => {
  77. if (!file.isImage()) {
  78. Attachments.update(
  79. file._id,
  80. {
  81. $set: {
  82. 'original.type': newTypeForNonImage,
  83. 'copies.attachments.type': newTypeForNonImage,
  84. },
  85. },
  86. noValidate,
  87. );
  88. }
  89. });
  90. });
  91. Migrations.add('card-covers', () => {
  92. Cards.find().forEach(card => {
  93. const cover = Attachments.findOne({ cardId: card._id, cover: true });
  94. if (cover) {
  95. Cards.update(card._id, { $set: { coverId: cover._id } }, noValidate);
  96. }
  97. });
  98. Attachments.update({}, { $unset: { cover: '' } }, noValidateMulti);
  99. });
  100. Migrations.add('use-css-class-for-boards-colors', () => {
  101. const associationTable = {
  102. '#27AE60': 'nephritis',
  103. '#C0392B': 'pomegranate',
  104. '#2980B9': 'belize',
  105. '#8E44AD': 'wisteria',
  106. '#2C3E50': 'midnight',
  107. '#E67E22': 'pumpkin',
  108. '#CD5A91': 'moderatepink',
  109. '#00AECC': 'strongcyan',
  110. '#4BBF6B': 'limegreen',
  111. '#2C3E51': 'dark',
  112. '#27AE61': 'relax',
  113. '#568BA2': 'corteza',
  114. };
  115. Boards.find().forEach(board => {
  116. const oldBoardColor = board.background.color;
  117. const newBoardColor = associationTable[oldBoardColor];
  118. Boards.update(
  119. board._id,
  120. {
  121. $set: { color: newBoardColor },
  122. $unset: { background: '' },
  123. },
  124. noValidate,
  125. );
  126. });
  127. });
  128. Migrations.add('denormalize-star-number-per-board', () => {
  129. Boards.find().forEach(board => {
  130. const nStars = Users.find({ 'profile.starredBoards': board._id }).count();
  131. Boards.update(board._id, { $set: { stars: nStars } }, noValidate);
  132. });
  133. });
  134. // We want to keep a trace of former members so we can efficiently publish their
  135. // infos in the general board publication.
  136. Migrations.add('add-member-isactive-field', () => {
  137. Boards.find({}, { fields: { members: 1 } }).forEach(board => {
  138. const allUsersWithSomeActivity = _.chain(
  139. Activities.find(
  140. { boardId: board._id },
  141. { fields: { userId: 1 } },
  142. ).fetch(),
  143. )
  144. .pluck('userId')
  145. .uniq()
  146. .value();
  147. const currentUsers = _.pluck(board.members, 'userId');
  148. const formerUsers = _.difference(allUsersWithSomeActivity, currentUsers);
  149. const newMemberSet = [];
  150. board.members.forEach(member => {
  151. member.isActive = true;
  152. newMemberSet.push(member);
  153. });
  154. formerUsers.forEach(userId => {
  155. newMemberSet.push({
  156. userId,
  157. isAdmin: false,
  158. isActive: false,
  159. });
  160. });
  161. Boards.update(board._id, { $set: { members: newMemberSet } }, noValidate);
  162. });
  163. });
  164. Migrations.add('add-sort-checklists', () => {
  165. Checklists.find().forEach((checklist, index) => {
  166. if (!checklist.hasOwnProperty('sort')) {
  167. Checklists.direct.update(
  168. checklist._id,
  169. { $set: { sort: index } },
  170. noValidate,
  171. );
  172. }
  173. checklist.items.forEach((item, index) => {
  174. if (!item.hasOwnProperty('sort')) {
  175. Checklists.direct.update(
  176. { _id: checklist._id, 'items._id': item._id },
  177. { $set: { 'items.$.sort': index } },
  178. noValidate,
  179. );
  180. }
  181. });
  182. });
  183. });
  184. Migrations.add('add-swimlanes', () => {
  185. Boards.find().forEach(board => {
  186. const swimlaneId = board.getDefaultSwimline()._id;
  187. Cards.find({ boardId: board._id }).forEach(card => {
  188. if (!card.hasOwnProperty('swimlaneId')) {
  189. Cards.direct.update(
  190. { _id: card._id },
  191. { $set: { swimlaneId } },
  192. noValidate,
  193. );
  194. }
  195. });
  196. });
  197. });
  198. Migrations.add('add-views', () => {
  199. Boards.find().forEach(board => {
  200. if (!board.hasOwnProperty('view')) {
  201. Boards.direct.update(
  202. { _id: board._id },
  203. { $set: { view: 'board-view-swimlanes' } },
  204. noValidate,
  205. );
  206. }
  207. });
  208. });
  209. Migrations.add('add-checklist-items', () => {
  210. Checklists.find().forEach(checklist => {
  211. // Create new items
  212. _.sortBy(checklist.items, 'sort').forEach((item, index) => {
  213. ChecklistItems.direct.insert({
  214. title: item.title ? item.title : 'Checklist',
  215. sort: index,
  216. isFinished: item.isFinished,
  217. checklistId: checklist._id,
  218. cardId: checklist.cardId,
  219. });
  220. });
  221. // Delete old ones
  222. Checklists.direct.update(
  223. { _id: checklist._id },
  224. { $unset: { items: 1 } },
  225. noValidate,
  226. );
  227. });
  228. });
  229. Migrations.add('add-profile-view', () => {
  230. Users.find().forEach(user => {
  231. if (!user.hasOwnProperty('profile.boardView')) {
  232. // Set default view
  233. Users.direct.update(
  234. { _id: user._id },
  235. { $set: { 'profile.boardView': 'board-view-lists' } },
  236. noValidate,
  237. );
  238. }
  239. });
  240. });
  241. Migrations.add('add-card-types', () => {
  242. Cards.find().forEach(card => {
  243. Cards.direct.update(
  244. { _id: card._id },
  245. {
  246. $set: {
  247. type: 'cardType-card',
  248. linkedId: null,
  249. },
  250. },
  251. noValidate,
  252. );
  253. });
  254. });
  255. Migrations.add('add-custom-fields-to-cards', () => {
  256. Cards.update(
  257. {
  258. customFields: {
  259. $exists: false,
  260. },
  261. },
  262. {
  263. $set: {
  264. customFields: [],
  265. },
  266. },
  267. noValidateMulti,
  268. );
  269. });
  270. Migrations.add('add-requester-field', () => {
  271. Cards.update(
  272. {
  273. requestedBy: {
  274. $exists: false,
  275. },
  276. },
  277. {
  278. $set: {
  279. requestedBy: '',
  280. },
  281. },
  282. noValidateMulti,
  283. );
  284. });
  285. Migrations.add('add-assigner-field', () => {
  286. Cards.update(
  287. {
  288. assignedBy: {
  289. $exists: false,
  290. },
  291. },
  292. {
  293. $set: {
  294. assignedBy: '',
  295. },
  296. },
  297. noValidateMulti,
  298. );
  299. });
  300. Migrations.add('add-parent-field-to-cards', () => {
  301. Cards.update(
  302. {
  303. parentId: {
  304. $exists: false,
  305. },
  306. },
  307. {
  308. $set: {
  309. parentId: '',
  310. },
  311. },
  312. noValidateMulti,
  313. );
  314. });
  315. Migrations.add('add-subtasks-boards', () => {
  316. Boards.update(
  317. {
  318. subtasksDefaultBoardId: {
  319. $exists: false,
  320. },
  321. },
  322. {
  323. $set: {
  324. subtasksDefaultBoardId: null,
  325. subtasksDefaultListId: null,
  326. },
  327. },
  328. noValidateMulti,
  329. );
  330. });
  331. Migrations.add('add-subtasks-sort', () => {
  332. Boards.update(
  333. {
  334. subtaskSort: {
  335. $exists: false,
  336. },
  337. },
  338. {
  339. $set: {
  340. subtaskSort: -1,
  341. },
  342. },
  343. noValidateMulti,
  344. );
  345. });
  346. Migrations.add('add-subtasks-allowed', () => {
  347. Boards.update(
  348. {
  349. allowsSubtasks: {
  350. $exists: false,
  351. },
  352. },
  353. {
  354. $set: {
  355. allowsSubtasks: true,
  356. },
  357. },
  358. noValidateMulti,
  359. );
  360. });
  361. Migrations.add('add-subtasks-allowed', () => {
  362. Boards.update(
  363. {
  364. presentParentTask: {
  365. $exists: false,
  366. },
  367. },
  368. {
  369. $set: {
  370. presentParentTask: 'no-parent',
  371. },
  372. },
  373. noValidateMulti,
  374. );
  375. });
  376. Migrations.add('add-authenticationMethod', () => {
  377. Users.update(
  378. {
  379. authenticationMethod: {
  380. $exists: false,
  381. },
  382. },
  383. {
  384. $set: {
  385. authenticationMethod: 'password',
  386. },
  387. },
  388. noValidateMulti,
  389. );
  390. });
  391. Migrations.add('remove-tag', () => {
  392. Users.update(
  393. {},
  394. {
  395. $unset: {
  396. 'profile.tags': 1,
  397. },
  398. },
  399. noValidateMulti,
  400. );
  401. });
  402. Migrations.add('remove-customFields-references-broken', () => {
  403. Cards.update(
  404. { 'customFields.$value': null },
  405. {
  406. $pull: {
  407. customFields: { value: null },
  408. },
  409. },
  410. noValidateMulti,
  411. );
  412. });
  413. Migrations.add('add-product-name', () => {
  414. Settings.update(
  415. {
  416. productName: {
  417. $exists: false,
  418. },
  419. },
  420. {
  421. $set: {
  422. productName: '',
  423. },
  424. },
  425. noValidateMulti,
  426. );
  427. });
  428. Migrations.add('add-hide-logo', () => {
  429. Settings.update(
  430. {
  431. hideLogo: {
  432. $exists: false,
  433. },
  434. },
  435. {
  436. $set: {
  437. hideLogo: false,
  438. },
  439. },
  440. noValidateMulti,
  441. );
  442. });
  443. Migrations.add('add-custom-html-after-body-start', () => {
  444. Settings.update(
  445. {
  446. customHTMLafterBodyStart: {
  447. $exists: false,
  448. },
  449. },
  450. {
  451. $set: {
  452. customHTMLafterBodyStart: '',
  453. },
  454. },
  455. noValidateMulti,
  456. );
  457. });
  458. Migrations.add('add-custom-html-before-body-end', () => {
  459. Settings.update(
  460. {
  461. customHTMLbeforeBodyEnd: {
  462. $exists: false,
  463. },
  464. },
  465. {
  466. $set: {
  467. customHTMLbeforeBodyEnd: '',
  468. },
  469. },
  470. noValidateMulti,
  471. );
  472. });
  473. Migrations.add('add-displayAuthenticationMethod', () => {
  474. Settings.update(
  475. {
  476. displayAuthenticationMethod: {
  477. $exists: false,
  478. },
  479. },
  480. {
  481. $set: {
  482. displayAuthenticationMethod: true,
  483. },
  484. },
  485. noValidateMulti,
  486. );
  487. });
  488. Migrations.add('add-defaultAuthenticationMethod', () => {
  489. Settings.update(
  490. {
  491. defaultAuthenticationMethod: {
  492. $exists: false,
  493. },
  494. },
  495. {
  496. $set: {
  497. defaultAuthenticationMethod: 'password',
  498. },
  499. },
  500. noValidateMulti,
  501. );
  502. });
  503. Migrations.add('add-templates', () => {
  504. Boards.update(
  505. {
  506. type: {
  507. $exists: false,
  508. },
  509. },
  510. {
  511. $set: {
  512. type: 'board',
  513. },
  514. },
  515. noValidateMulti,
  516. );
  517. Swimlanes.update(
  518. {
  519. type: {
  520. $exists: false,
  521. },
  522. },
  523. {
  524. $set: {
  525. type: 'swimlane',
  526. },
  527. },
  528. noValidateMulti,
  529. );
  530. Lists.update(
  531. {
  532. type: {
  533. $exists: false,
  534. },
  535. swimlaneId: {
  536. $exists: false,
  537. },
  538. },
  539. {
  540. $set: {
  541. type: 'list',
  542. swimlaneId: '',
  543. },
  544. },
  545. noValidateMulti,
  546. );
  547. Users.find({
  548. 'profile.templatesBoardId': {
  549. $exists: false,
  550. },
  551. }).forEach(user => {
  552. // Create board and swimlanes
  553. Boards.insert(
  554. {
  555. title: TAPi18n.__('templates'),
  556. permission: 'private',
  557. type: 'template-container',
  558. members: [
  559. {
  560. userId: user._id,
  561. isAdmin: true,
  562. isActive: true,
  563. isNoComments: false,
  564. isCommentOnly: false,
  565. },
  566. ],
  567. },
  568. (err, boardId) => {
  569. // Insert the reference to our templates board
  570. Users.update(user._id, {
  571. $set: { 'profile.templatesBoardId': boardId },
  572. });
  573. // Insert the card templates swimlane
  574. Swimlanes.insert(
  575. {
  576. title: TAPi18n.__('card-templates-swimlane'),
  577. boardId,
  578. sort: 1,
  579. type: 'template-container',
  580. },
  581. (err, swimlaneId) => {
  582. // Insert the reference to out card templates swimlane
  583. Users.update(user._id, {
  584. $set: { 'profile.cardTemplatesSwimlaneId': swimlaneId },
  585. });
  586. },
  587. );
  588. // Insert the list templates swimlane
  589. Swimlanes.insert(
  590. {
  591. title: TAPi18n.__('list-templates-swimlane'),
  592. boardId,
  593. sort: 2,
  594. type: 'template-container',
  595. },
  596. (err, swimlaneId) => {
  597. // Insert the reference to out list templates swimlane
  598. Users.update(user._id, {
  599. $set: { 'profile.listTemplatesSwimlaneId': swimlaneId },
  600. });
  601. },
  602. );
  603. // Insert the board templates swimlane
  604. Swimlanes.insert(
  605. {
  606. title: TAPi18n.__('board-templates-swimlane'),
  607. boardId,
  608. sort: 3,
  609. type: 'template-container',
  610. },
  611. (err, swimlaneId) => {
  612. // Insert the reference to out board templates swimlane
  613. Users.update(user._id, {
  614. $set: { 'profile.boardTemplatesSwimlaneId': swimlaneId },
  615. });
  616. },
  617. );
  618. },
  619. );
  620. });
  621. });
  622. Migrations.add('fix-circular-reference_', () => {
  623. Cards.find().forEach(card => {
  624. if (card.parentId === card._id) {
  625. Cards.update(card._id, { $set: { parentId: '' } }, noValidateMulti);
  626. }
  627. });
  628. });
  629. Migrations.add('mutate-boardIds-in-customfields', () => {
  630. CustomFields.find().forEach(cf => {
  631. CustomFields.update(
  632. cf,
  633. {
  634. $set: {
  635. boardIds: [cf.boardId],
  636. },
  637. $unset: {
  638. boardId: '',
  639. },
  640. },
  641. noValidateMulti,
  642. );
  643. });
  644. });
  645. const firstBatchOfDbsToAddCreatedAndUpdated = [
  646. AccountSettings,
  647. Actions,
  648. Activities,
  649. Announcements,
  650. Boards,
  651. CardComments,
  652. Cards,
  653. ChecklistItems,
  654. Checklists,
  655. CustomFields,
  656. Integrations,
  657. InvitationCodes,
  658. Lists,
  659. Rules,
  660. Settings,
  661. Swimlanes,
  662. Triggers,
  663. UnsavedEdits,
  664. ];
  665. firstBatchOfDbsToAddCreatedAndUpdated.forEach(db => {
  666. db.before.insert((userId, doc) => {
  667. doc.createdAt = Date.now();
  668. doc.updatedAt = doc.createdAt;
  669. });
  670. db.before.update((userId, doc, fieldNames, modifier) => {
  671. modifier.$set = modifier.$set || {};
  672. modifier.$set.updatedAt = new Date();
  673. });
  674. });
  675. const modifiedAtTables = [
  676. AccountSettings,
  677. Actions,
  678. Activities,
  679. Announcements,
  680. Boards,
  681. CardComments,
  682. Cards,
  683. ChecklistItems,
  684. Checklists,
  685. CustomFields,
  686. Integrations,
  687. InvitationCodes,
  688. Lists,
  689. Rules,
  690. Settings,
  691. Swimlanes,
  692. Triggers,
  693. UnsavedEdits,
  694. Users,
  695. ];
  696. Migrations.add('add-missing-created-and-modified', () => {
  697. Promise.all(
  698. modifiedAtTables.map(db =>
  699. db
  700. .rawCollection()
  701. .update(
  702. { modifiedAt: { $exists: false } },
  703. { $set: { modifiedAt: new Date() } },
  704. { multi: true },
  705. )
  706. .then(() =>
  707. db
  708. .rawCollection()
  709. .update(
  710. { createdAt: { $exists: false } },
  711. { $set: { createdAt: new Date() } },
  712. { multi: true },
  713. ),
  714. ),
  715. ),
  716. )
  717. .then(() => {
  718. // eslint-disable-next-line no-console
  719. console.info('Successfully added createdAt and updatedAt to all tables');
  720. })
  721. .catch(e => {
  722. // eslint-disable-next-line no-console
  723. console.error(e);
  724. });
  725. });