migrations.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. };
  114. Boards.find().forEach(board => {
  115. const oldBoardColor = board.background.color;
  116. const newBoardColor = associationTable[oldBoardColor];
  117. Boards.update(
  118. board._id,
  119. {
  120. $set: { color: newBoardColor },
  121. $unset: { background: '' },
  122. },
  123. noValidate,
  124. );
  125. });
  126. });
  127. Migrations.add('denormalize-star-number-per-board', () => {
  128. Boards.find().forEach(board => {
  129. const nStars = Users.find({ 'profile.starredBoards': board._id }).count();
  130. Boards.update(board._id, { $set: { stars: nStars } }, noValidate);
  131. });
  132. });
  133. // We want to keep a trace of former members so we can efficiently publish their
  134. // infos in the general board publication.
  135. Migrations.add('add-member-isactive-field', () => {
  136. Boards.find({}, { fields: { members: 1 } }).forEach(board => {
  137. const allUsersWithSomeActivity = _.chain(
  138. Activities.find(
  139. { boardId: board._id },
  140. { fields: { userId: 1 } },
  141. ).fetch(),
  142. )
  143. .pluck('userId')
  144. .uniq()
  145. .value();
  146. const currentUsers = _.pluck(board.members, 'userId');
  147. const formerUsers = _.difference(allUsersWithSomeActivity, currentUsers);
  148. const newMemberSet = [];
  149. board.members.forEach(member => {
  150. member.isActive = true;
  151. newMemberSet.push(member);
  152. });
  153. formerUsers.forEach(userId => {
  154. newMemberSet.push({
  155. userId,
  156. isAdmin: false,
  157. isActive: false,
  158. });
  159. });
  160. Boards.update(board._id, { $set: { members: newMemberSet } }, noValidate);
  161. });
  162. });
  163. Migrations.add('add-sort-checklists', () => {
  164. Checklists.find().forEach((checklist, index) => {
  165. if (!checklist.hasOwnProperty('sort')) {
  166. Checklists.direct.update(
  167. checklist._id,
  168. { $set: { sort: index } },
  169. noValidate,
  170. );
  171. }
  172. checklist.items.forEach((item, index) => {
  173. if (!item.hasOwnProperty('sort')) {
  174. Checklists.direct.update(
  175. { _id: checklist._id, 'items._id': item._id },
  176. { $set: { 'items.$.sort': index } },
  177. noValidate,
  178. );
  179. }
  180. });
  181. });
  182. });
  183. Migrations.add('add-swimlanes', () => {
  184. Boards.find().forEach(board => {
  185. const swimlaneId = board.getDefaultSwimline()._id;
  186. Cards.find({ boardId: board._id }).forEach(card => {
  187. if (!card.hasOwnProperty('swimlaneId')) {
  188. Cards.direct.update(
  189. { _id: card._id },
  190. { $set: { swimlaneId } },
  191. noValidate,
  192. );
  193. }
  194. });
  195. });
  196. });
  197. Migrations.add('add-views', () => {
  198. Boards.find().forEach(board => {
  199. if (!board.hasOwnProperty('view')) {
  200. Boards.direct.update(
  201. { _id: board._id },
  202. { $set: { view: 'board-view-swimlanes' } },
  203. noValidate,
  204. );
  205. }
  206. });
  207. });
  208. Migrations.add('add-checklist-items', () => {
  209. Checklists.find().forEach(checklist => {
  210. // Create new items
  211. _.sortBy(checklist.items, 'sort').forEach((item, index) => {
  212. ChecklistItems.direct.insert({
  213. title: item.title ? item.title : 'Checklist',
  214. sort: index,
  215. isFinished: item.isFinished,
  216. checklistId: checklist._id,
  217. cardId: checklist.cardId,
  218. });
  219. });
  220. // Delete old ones
  221. Checklists.direct.update(
  222. { _id: checklist._id },
  223. { $unset: { items: 1 } },
  224. noValidate,
  225. );
  226. });
  227. });
  228. Migrations.add('add-profile-view', () => {
  229. Users.find().forEach(user => {
  230. if (!user.hasOwnProperty('profile.boardView')) {
  231. // Set default view
  232. Users.direct.update(
  233. { _id: user._id },
  234. { $set: { 'profile.boardView': 'board-view-lists' } },
  235. noValidate,
  236. );
  237. }
  238. });
  239. });
  240. Migrations.add('add-card-types', () => {
  241. Cards.find().forEach(card => {
  242. Cards.direct.update(
  243. { _id: card._id },
  244. {
  245. $set: {
  246. type: 'cardType-card',
  247. linkedId: null,
  248. },
  249. },
  250. noValidate,
  251. );
  252. });
  253. });
  254. Migrations.add('add-custom-fields-to-cards', () => {
  255. Cards.update(
  256. {
  257. customFields: {
  258. $exists: false,
  259. },
  260. },
  261. {
  262. $set: {
  263. customFields: [],
  264. },
  265. },
  266. noValidateMulti,
  267. );
  268. });
  269. Migrations.add('add-requester-field', () => {
  270. Cards.update(
  271. {
  272. requestedBy: {
  273. $exists: false,
  274. },
  275. },
  276. {
  277. $set: {
  278. requestedBy: '',
  279. },
  280. },
  281. noValidateMulti,
  282. );
  283. });
  284. Migrations.add('add-assigner-field', () => {
  285. Cards.update(
  286. {
  287. assignedBy: {
  288. $exists: false,
  289. },
  290. },
  291. {
  292. $set: {
  293. assignedBy: '',
  294. },
  295. },
  296. noValidateMulti,
  297. );
  298. });
  299. Migrations.add('add-parent-field-to-cards', () => {
  300. Cards.update(
  301. {
  302. parentId: {
  303. $exists: false,
  304. },
  305. },
  306. {
  307. $set: {
  308. parentId: '',
  309. },
  310. },
  311. noValidateMulti,
  312. );
  313. });
  314. Migrations.add('add-subtasks-boards', () => {
  315. Boards.update(
  316. {
  317. subtasksDefaultBoardId: {
  318. $exists: false,
  319. },
  320. },
  321. {
  322. $set: {
  323. subtasksDefaultBoardId: null,
  324. subtasksDefaultListId: null,
  325. },
  326. },
  327. noValidateMulti,
  328. );
  329. });
  330. Migrations.add('add-subtasks-sort', () => {
  331. Boards.update(
  332. {
  333. subtaskSort: {
  334. $exists: false,
  335. },
  336. },
  337. {
  338. $set: {
  339. subtaskSort: -1,
  340. },
  341. },
  342. noValidateMulti,
  343. );
  344. });
  345. Migrations.add('add-subtasks-allowed', () => {
  346. Boards.update(
  347. {
  348. allowsSubtasks: {
  349. $exists: false,
  350. },
  351. },
  352. {
  353. $set: {
  354. allowsSubtasks: true,
  355. },
  356. },
  357. noValidateMulti,
  358. );
  359. });
  360. Migrations.add('add-subtasks-allowed', () => {
  361. Boards.update(
  362. {
  363. presentParentTask: {
  364. $exists: false,
  365. },
  366. },
  367. {
  368. $set: {
  369. presentParentTask: 'no-parent',
  370. },
  371. },
  372. noValidateMulti,
  373. );
  374. });
  375. Migrations.add('add-authenticationMethod', () => {
  376. Users.update(
  377. {
  378. authenticationMethod: {
  379. $exists: false,
  380. },
  381. },
  382. {
  383. $set: {
  384. authenticationMethod: 'password',
  385. },
  386. },
  387. noValidateMulti,
  388. );
  389. });
  390. Migrations.add('remove-tag', () => {
  391. Users.update(
  392. {},
  393. {
  394. $unset: {
  395. 'profile.tags': 1,
  396. },
  397. },
  398. noValidateMulti,
  399. );
  400. });
  401. Migrations.add('remove-customFields-references-broken', () => {
  402. Cards.update(
  403. { 'customFields.$value': null },
  404. {
  405. $pull: {
  406. customFields: { value: null },
  407. },
  408. },
  409. noValidateMulti,
  410. );
  411. });
  412. Migrations.add('add-product-name', () => {
  413. Settings.update(
  414. {
  415. productName: {
  416. $exists: false,
  417. },
  418. },
  419. {
  420. $set: {
  421. productName: '',
  422. },
  423. },
  424. noValidateMulti,
  425. );
  426. });
  427. Migrations.add('add-hide-logo', () => {
  428. Settings.update(
  429. {
  430. hideLogo: {
  431. $exists: false,
  432. },
  433. },
  434. {
  435. $set: {
  436. hideLogo: false,
  437. },
  438. },
  439. noValidateMulti,
  440. );
  441. });
  442. Migrations.add('add-custom-html-after-body-start', () => {
  443. Settings.update(
  444. {
  445. customHTMLafterBodyStart: {
  446. $exists: false,
  447. },
  448. },
  449. {
  450. $set: {
  451. customHTMLafterBodyStart: '',
  452. },
  453. },
  454. noValidateMulti,
  455. );
  456. });
  457. Migrations.add('add-custom-html-before-body-end', () => {
  458. Settings.update(
  459. {
  460. customHTMLbeforeBodyEnd: {
  461. $exists: false,
  462. },
  463. },
  464. {
  465. $set: {
  466. customHTMLbeforeBodyEnd: '',
  467. },
  468. },
  469. noValidateMulti,
  470. );
  471. });
  472. Migrations.add('add-displayAuthenticationMethod', () => {
  473. Settings.update(
  474. {
  475. displayAuthenticationMethod: {
  476. $exists: false,
  477. },
  478. },
  479. {
  480. $set: {
  481. displayAuthenticationMethod: true,
  482. },
  483. },
  484. noValidateMulti,
  485. );
  486. });
  487. Migrations.add('add-defaultAuthenticationMethod', () => {
  488. Settings.update(
  489. {
  490. defaultAuthenticationMethod: {
  491. $exists: false,
  492. },
  493. },
  494. {
  495. $set: {
  496. defaultAuthenticationMethod: 'password',
  497. },
  498. },
  499. noValidateMulti,
  500. );
  501. });
  502. Migrations.add('add-templates', () => {
  503. Boards.update(
  504. {
  505. type: {
  506. $exists: false,
  507. },
  508. },
  509. {
  510. $set: {
  511. type: 'board',
  512. },
  513. },
  514. noValidateMulti,
  515. );
  516. Swimlanes.update(
  517. {
  518. type: {
  519. $exists: false,
  520. },
  521. },
  522. {
  523. $set: {
  524. type: 'swimlane',
  525. },
  526. },
  527. noValidateMulti,
  528. );
  529. Lists.update(
  530. {
  531. type: {
  532. $exists: false,
  533. },
  534. swimlaneId: {
  535. $exists: false,
  536. },
  537. },
  538. {
  539. $set: {
  540. type: 'list',
  541. swimlaneId: '',
  542. },
  543. },
  544. noValidateMulti,
  545. );
  546. Users.find({
  547. 'profile.templatesBoardId': {
  548. $exists: false,
  549. },
  550. }).forEach(user => {
  551. // Create board and swimlanes
  552. Boards.insert(
  553. {
  554. title: TAPi18n.__('templates'),
  555. permission: 'private',
  556. type: 'template-container',
  557. members: [
  558. {
  559. userId: user._id,
  560. isAdmin: true,
  561. isActive: true,
  562. isNoComments: false,
  563. isCommentOnly: false,
  564. },
  565. ],
  566. },
  567. (err, boardId) => {
  568. // Insert the reference to our templates board
  569. Users.update(user._id, {
  570. $set: { 'profile.templatesBoardId': boardId },
  571. });
  572. // Insert the card templates swimlane
  573. Swimlanes.insert(
  574. {
  575. title: TAPi18n.__('card-templates-swimlane'),
  576. boardId,
  577. sort: 1,
  578. type: 'template-container',
  579. },
  580. (err, swimlaneId) => {
  581. // Insert the reference to out card templates swimlane
  582. Users.update(user._id, {
  583. $set: { 'profile.cardTemplatesSwimlaneId': swimlaneId },
  584. });
  585. },
  586. );
  587. // Insert the list templates swimlane
  588. Swimlanes.insert(
  589. {
  590. title: TAPi18n.__('list-templates-swimlane'),
  591. boardId,
  592. sort: 2,
  593. type: 'template-container',
  594. },
  595. (err, swimlaneId) => {
  596. // Insert the reference to out list templates swimlane
  597. Users.update(user._id, {
  598. $set: { 'profile.listTemplatesSwimlaneId': swimlaneId },
  599. });
  600. },
  601. );
  602. // Insert the board templates swimlane
  603. Swimlanes.insert(
  604. {
  605. title: TAPi18n.__('board-templates-swimlane'),
  606. boardId,
  607. sort: 3,
  608. type: 'template-container',
  609. },
  610. (err, swimlaneId) => {
  611. // Insert the reference to out board templates swimlane
  612. Users.update(user._id, {
  613. $set: { 'profile.boardTemplatesSwimlaneId': swimlaneId },
  614. });
  615. },
  616. );
  617. },
  618. );
  619. });
  620. });
  621. Migrations.add('fix-circular-reference_', () => {
  622. Cards.find().forEach(card => {
  623. if (card.parentId === card._id) {
  624. Cards.update(card._id, { $set: { parentId: '' } }, noValidateMulti);
  625. }
  626. });
  627. });
  628. Migrations.add('mutate-boardIds-in-customfields', () => {
  629. CustomFields.find().forEach(cf => {
  630. CustomFields.update(
  631. cf,
  632. {
  633. $set: {
  634. boardIds: [cf.boardId],
  635. },
  636. $unset: {
  637. boardId: '',
  638. },
  639. },
  640. noValidateMulti,
  641. );
  642. });
  643. });
  644. const firstBatchOfDbsToAddCreatedAndUpdated = [
  645. AccountSettings,
  646. Actions,
  647. Activities,
  648. Announcements,
  649. Boards,
  650. CardComments,
  651. Cards,
  652. ChecklistItems,
  653. Checklists,
  654. CustomFields,
  655. Integrations,
  656. InvitationCodes,
  657. Lists,
  658. Rules,
  659. Settings,
  660. Swimlanes,
  661. Triggers,
  662. UnsavedEdits,
  663. ];
  664. firstBatchOfDbsToAddCreatedAndUpdated.forEach(db => {
  665. db.before.insert((userId, doc) => {
  666. doc.createdAt = Date.now();
  667. doc.updatedAt = doc.createdAt;
  668. });
  669. db.before.update((userId, doc, fieldNames, modifier) => {
  670. modifier.$set = modifier.$set || {};
  671. modifier.$set.updatedAt = new Date();
  672. });
  673. });
  674. const modifiedAtTables = [
  675. AccountSettings,
  676. Actions,
  677. Activities,
  678. Announcements,
  679. Boards,
  680. CardComments,
  681. Cards,
  682. ChecklistItems,
  683. Checklists,
  684. CustomFields,
  685. Integrations,
  686. InvitationCodes,
  687. Lists,
  688. Rules,
  689. Settings,
  690. Swimlanes,
  691. Triggers,
  692. UnsavedEdits,
  693. Users,
  694. ];
  695. Migrations.add('add-missing-created-and-modified', () => {
  696. Promise.all(
  697. modifiedAtTables.map(db =>
  698. db
  699. .rawCollection()
  700. .update(
  701. { modifiedAt: { $exists: false } },
  702. { $set: { modifiedAt: new Date() } },
  703. { multi: true },
  704. )
  705. .then(() =>
  706. db
  707. .rawCollection()
  708. .update(
  709. { createdAt: { $exists: false } },
  710. { $set: { createdAt: new Date() } },
  711. { multi: true },
  712. ),
  713. ),
  714. ),
  715. )
  716. .then(() => {
  717. // eslint-disable-next-line no-console
  718. console.info('Successfully added createdAt and updatedAt to all tables');
  719. })
  720. .catch(e => {
  721. // eslint-disable-next-line no-console
  722. console.error(e);
  723. });
  724. });