migrations.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. import fs from 'fs';
  2. import path from 'path';
  3. import AccountSettings from '../models/accountSettings';
  4. import TableVisibilityModeSettings from '../models/tableVisibilityModeSettings';
  5. import Actions from '../models/actions';
  6. import Activities from '../models/activities';
  7. import Announcements from '../models/announcements';
  8. import Attachments from '../models/attachments';
  9. import AttachmentsOld from '../models/attachments_old';
  10. import Avatars from '../models/avatars';
  11. import AvatarsOld from '../models/avatars_old';
  12. import Boards from '../models/boards';
  13. import CardComments from '../models/cardComments';
  14. import Cards from '../models/cards';
  15. import ChecklistItems from '../models/checklistItems';
  16. import Checklists from '../models/checklists';
  17. import CustomFields from '../models/customFields';
  18. import Integrations from '../models/integrations';
  19. import InvitationCodes from '../models/invitationCodes';
  20. import Lists from '../models/lists';
  21. import Rules from '../models/rules';
  22. import Settings from '../models/settings';
  23. import Swimlanes from '../models/swimlanes';
  24. import Triggers from '../models/triggers';
  25. import UnsavedEdits from '../models/unsavedEdits';
  26. import Users from '../models/users';
  27. // Anytime you change the schema of one of the collection in a non-backward
  28. // compatible way you have to write a migration in this file using the following
  29. // API:
  30. //
  31. // Migrations.add(name, migrationCallback, optionalOrder);
  32. // Note that we have extra migrations defined in `sandstorm.js` that are
  33. // exclusive to Sandstorm and shouldn’t be executed in the general case.
  34. // XXX I guess if we had ES6 modules we could
  35. // `import { isSandstorm } from sandstorm.js` and define the migration here as
  36. // well, but for now I want to avoid definied too many globals.
  37. // In the context of migration functions we don't want to validate database
  38. // mutation queries against the current (ie, latest) collection schema. Doing
  39. // that would work at the time we write the migration but would break in the
  40. // future when we'll update again the concerned collection schema.
  41. //
  42. // To prevent this bug we always have to disable the schema validation and
  43. // argument transformations. We generally use the shorthandlers defined below.
  44. const noValidate = {
  45. validate: false,
  46. filter: false,
  47. autoConvert: false,
  48. removeEmptyStrings: false,
  49. getAutoValues: false,
  50. };
  51. const noValidateMulti = { ...noValidate, multi: true };
  52. Migrations.add('board-background-color', () => {
  53. const defaultColor = '#16A085';
  54. Boards.update(
  55. {
  56. background: {
  57. $exists: false,
  58. },
  59. },
  60. {
  61. $set: {
  62. background: {
  63. type: 'color',
  64. color: defaultColor,
  65. },
  66. },
  67. },
  68. noValidateMulti,
  69. );
  70. });
  71. Migrations.add('lowercase-board-permission', () => {
  72. ['Public', 'Private'].forEach(permission => {
  73. Boards.update(
  74. { permission },
  75. { $set: { permission: permission.toLowerCase() } },
  76. noValidateMulti,
  77. );
  78. });
  79. });
  80. // Security migration: see https://github.com/wekan/wekan/issues/99
  81. Migrations.add('change-attachments-type-for-non-images', () => {
  82. const newTypeForNonImage = 'application/octet-stream';
  83. Attachments.find().forEach(file => {
  84. if (!file.isImage()) {
  85. Attachments.update(
  86. file._id,
  87. {
  88. $set: {
  89. 'original.type': newTypeForNonImage,
  90. 'copies.attachments.type': newTypeForNonImage,
  91. },
  92. },
  93. noValidate,
  94. );
  95. }
  96. });
  97. });
  98. Migrations.add('card-covers', () => {
  99. Cards.find().forEach(card => {
  100. const cover = Attachments.findOne({ cardId: card._id, cover: true });
  101. if (cover) {
  102. Cards.update(card._id, { $set: { coverId: cover._id } }, noValidate);
  103. }
  104. });
  105. Attachments.update({}, { $unset: { cover: '' } }, noValidateMulti);
  106. });
  107. Migrations.add('use-css-class-for-boards-colors', () => {
  108. const associationTable = {
  109. '#27AE60': 'nephritis',
  110. '#C0392B': 'pomegranate',
  111. '#2980B9': 'belize',
  112. '#8E44AD': 'wisteria',
  113. '#2C3E50': 'midnight',
  114. '#E67E22': 'pumpkin',
  115. '#CD5A91': 'moderatepink',
  116. '#00AECC': 'strongcyan',
  117. '#4BBF6B': 'limegreen',
  118. '#2C3E51': 'dark',
  119. '#27AE61': 'relax',
  120. '#568BA2': 'corteza',
  121. '#499BEA': 'clearblue',
  122. '#596557': 'natural',
  123. '#2A80B8': 'modern',
  124. '#2a2a2a': 'moderndark',
  125. };
  126. Boards.find().forEach(board => {
  127. const oldBoardColor = board.background.color;
  128. const newBoardColor = associationTable[oldBoardColor];
  129. Boards.update(
  130. board._id,
  131. {
  132. $set: { color: newBoardColor },
  133. $unset: { background: '' },
  134. },
  135. noValidate,
  136. );
  137. });
  138. });
  139. Migrations.add('denormalize-star-number-per-board', () => {
  140. Boards.find().forEach(board => {
  141. const nStars = Users.find({ 'profile.starredBoards': board._id }).count();
  142. Boards.update(board._id, { $set: { stars: nStars } }, noValidate);
  143. });
  144. });
  145. // We want to keep a trace of former members so we can efficiently publish their
  146. // infos in the general board publication.
  147. Migrations.add('add-member-isactive-field', () => {
  148. Boards.find({}, { fields: { members: 1 } }).forEach(board => {
  149. const allUsersWithSomeActivity = _.chain(
  150. Activities.find(
  151. { boardId: board._id },
  152. { fields: { userId: 1 } },
  153. ).fetch(),
  154. )
  155. .pluck('userId')
  156. .uniq()
  157. .value();
  158. const currentUsers = _.pluck(board.members, 'userId');
  159. const formerUsers = _.difference(allUsersWithSomeActivity, currentUsers);
  160. const newMemberSet = [];
  161. board.members.forEach(member => {
  162. member.isActive = true;
  163. newMemberSet.push(member);
  164. });
  165. formerUsers.forEach(userId => {
  166. newMemberSet.push({
  167. userId,
  168. isAdmin: false,
  169. isActive: false,
  170. });
  171. });
  172. Boards.update(board._id, { $set: { members: newMemberSet } }, noValidate);
  173. });
  174. });
  175. Migrations.add('add-sort-checklists', () => {
  176. Checklists.find().forEach((checklist, index) => {
  177. if (!checklist.hasOwnProperty('sort')) {
  178. Checklists.direct.update(
  179. checklist._id,
  180. { $set: { sort: index } },
  181. noValidate,
  182. );
  183. }
  184. checklist.items.forEach((item, index) => {
  185. if (!item.hasOwnProperty('sort')) {
  186. Checklists.direct.update(
  187. { _id: checklist._id, 'items._id': item._id },
  188. { $set: { 'items.$.sort': index } },
  189. noValidate,
  190. );
  191. }
  192. });
  193. });
  194. });
  195. Migrations.add('add-swimlanes', () => {
  196. Boards.find().forEach(board => {
  197. const swimlaneId = board.getDefaultSwimline()._id;
  198. Cards.find({ boardId: board._id }).forEach(card => {
  199. if (!card.hasOwnProperty('swimlaneId')) {
  200. Cards.direct.update(
  201. { _id: card._id },
  202. { $set: { swimlaneId } },
  203. noValidate,
  204. );
  205. }
  206. });
  207. });
  208. });
  209. Migrations.add('add-views', () => {
  210. Boards.find().forEach(board => {
  211. if (!board.hasOwnProperty('view')) {
  212. Boards.direct.update(
  213. { _id: board._id },
  214. { $set: { view: 'board-view-swimlanes' } },
  215. noValidate,
  216. );
  217. }
  218. });
  219. });
  220. Migrations.add('add-checklist-items', () => {
  221. Checklists.find().forEach(checklist => {
  222. // Create new items
  223. _.sortBy(checklist.items, 'sort').forEach((item, index) => {
  224. ChecklistItems.direct.insert({
  225. title: item.title ? item.title : 'Checklist',
  226. sort: index,
  227. isFinished: item.isFinished,
  228. checklistId: checklist._id,
  229. cardId: checklist.cardId,
  230. });
  231. });
  232. // Delete old ones
  233. Checklists.direct.update(
  234. { _id: checklist._id },
  235. { $unset: { items: 1 } },
  236. noValidate,
  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-displayAuthenticationMethod', () => {
  443. Settings.update(
  444. {
  445. displayAuthenticationMethod: {
  446. $exists: false,
  447. },
  448. },
  449. {
  450. $set: {
  451. displayAuthenticationMethod: true,
  452. },
  453. },
  454. noValidateMulti,
  455. );
  456. });
  457. Migrations.add('add-defaultAuthenticationMethod', () => {
  458. Settings.update(
  459. {
  460. defaultAuthenticationMethod: {
  461. $exists: false,
  462. },
  463. },
  464. {
  465. $set: {
  466. defaultAuthenticationMethod: 'password',
  467. },
  468. },
  469. noValidateMulti,
  470. );
  471. });
  472. Migrations.add('add-templates', () => {
  473. Boards.update(
  474. {
  475. type: {
  476. $exists: false,
  477. },
  478. },
  479. {
  480. $set: {
  481. type: 'board',
  482. },
  483. },
  484. noValidateMulti,
  485. );
  486. Swimlanes.update(
  487. {
  488. type: {
  489. $exists: false,
  490. },
  491. },
  492. {
  493. $set: {
  494. type: 'swimlane',
  495. },
  496. },
  497. noValidateMulti,
  498. );
  499. Lists.update(
  500. {
  501. type: {
  502. $exists: false,
  503. },
  504. swimlaneId: {
  505. $exists: false,
  506. },
  507. },
  508. {
  509. $set: {
  510. type: 'list',
  511. swimlaneId: '',
  512. },
  513. },
  514. noValidateMulti,
  515. );
  516. Users.find({
  517. 'profile.templatesBoardId': {
  518. $exists: false,
  519. },
  520. }).forEach(user => {
  521. // Create board and swimlanes
  522. Boards.insert(
  523. {
  524. title: TAPi18n.__('templates'),
  525. permission: 'private',
  526. type: 'template-container',
  527. members: [
  528. {
  529. userId: user._id,
  530. isAdmin: true,
  531. isActive: true,
  532. isNoComments: false,
  533. isCommentOnly: false,
  534. },
  535. ],
  536. },
  537. (err, boardId) => {
  538. // Insert the reference to our templates board
  539. Users.update(user._id, {
  540. $set: { 'profile.templatesBoardId': boardId },
  541. });
  542. // Insert the card templates swimlane
  543. Swimlanes.insert(
  544. {
  545. title: TAPi18n.__('card-templates-swimlane'),
  546. boardId,
  547. sort: 1,
  548. type: 'template-container',
  549. },
  550. (err, swimlaneId) => {
  551. // Insert the reference to out card templates swimlane
  552. Users.update(user._id, {
  553. $set: { 'profile.cardTemplatesSwimlaneId': swimlaneId },
  554. });
  555. },
  556. );
  557. // Insert the list templates swimlane
  558. Swimlanes.insert(
  559. {
  560. title: TAPi18n.__('list-templates-swimlane'),
  561. boardId,
  562. sort: 2,
  563. type: 'template-container',
  564. },
  565. (err, swimlaneId) => {
  566. // Insert the reference to out list templates swimlane
  567. Users.update(user._id, {
  568. $set: { 'profile.listTemplatesSwimlaneId': swimlaneId },
  569. });
  570. },
  571. );
  572. // Insert the board templates swimlane
  573. Swimlanes.insert(
  574. {
  575. title: TAPi18n.__('board-templates-swimlane'),
  576. boardId,
  577. sort: 3,
  578. type: 'template-container',
  579. },
  580. (err, swimlaneId) => {
  581. // Insert the reference to out board templates swimlane
  582. Users.update(user._id, {
  583. $set: { 'profile.boardTemplatesSwimlaneId': swimlaneId },
  584. });
  585. },
  586. );
  587. },
  588. );
  589. });
  590. });
  591. Migrations.add('fix-circular-reference_', () => {
  592. Cards.find().forEach(card => {
  593. if (card.parentId === card._id) {
  594. Cards.update(card._id, { $set: { parentId: '' } }, noValidateMulti);
  595. }
  596. });
  597. });
  598. Migrations.add('mutate-boardIds-in-customfields', () => {
  599. CustomFields.find().forEach(cf => {
  600. CustomFields.update(
  601. cf,
  602. {
  603. $set: {
  604. boardIds: [cf.boardId],
  605. },
  606. $unset: {
  607. boardId: '',
  608. },
  609. },
  610. noValidateMulti,
  611. );
  612. });
  613. });
  614. const modifiedAtTables = [
  615. AccountSettings,
  616. TableVisibilityModeSettings,
  617. Actions,
  618. Activities,
  619. Announcements,
  620. Boards,
  621. CardComments,
  622. Cards,
  623. ChecklistItems,
  624. Checklists,
  625. CustomFields,
  626. Integrations,
  627. InvitationCodes,
  628. Lists,
  629. Rules,
  630. Settings,
  631. Swimlanes,
  632. Triggers,
  633. UnsavedEdits,
  634. Users,
  635. ];
  636. Migrations.add('add-missing-created-and-modified', () => {
  637. Promise.all(
  638. modifiedAtTables.map(db =>
  639. db
  640. .rawCollection()
  641. .update(
  642. { modifiedAt: { $exists: false } },
  643. { $set: { modifiedAt: new Date() } },
  644. { multi: true },
  645. )
  646. .then(() =>
  647. db
  648. .rawCollection()
  649. .update(
  650. { createdAt: { $exists: false } },
  651. { $set: { createdAt: new Date() } },
  652. { multi: true },
  653. ),
  654. ),
  655. ),
  656. )
  657. .then(() => {
  658. // eslint-disable-next-line no-console
  659. console.info('Successfully added createdAt and updatedAt to all tables');
  660. })
  661. .catch(e => {
  662. // eslint-disable-next-line no-console
  663. console.error(e);
  664. });
  665. });
  666. Migrations.add('fix-incorrect-dates', () => {
  667. const tables = [
  668. AccountSettings,
  669. TableVisibilityModeSettings,
  670. Actions,
  671. Activities,
  672. Announcements,
  673. Boards,
  674. CardComments,
  675. Cards,
  676. ChecklistItems,
  677. Checklists,
  678. CustomFields,
  679. Integrations,
  680. InvitationCodes,
  681. Lists,
  682. Rules,
  683. Settings,
  684. Swimlanes,
  685. Triggers,
  686. UnsavedEdits,
  687. ];
  688. // Dates were previously created with Date.now() which is a number, not a date
  689. tables.forEach(t =>
  690. t
  691. .rawCollection()
  692. .find({ $or: [{ createdAt: { $type: 1 } }, { updatedAt: { $type: 1 } }] })
  693. .forEach(({ _id, createdAt, updatedAt }) => {
  694. t.rawCollection().update(
  695. { _id },
  696. {
  697. $set: {
  698. createdAt: new Date(createdAt),
  699. updatedAt: new Date(updatedAt),
  700. },
  701. },
  702. );
  703. }),
  704. );
  705. });
  706. Migrations.add('add-assignee', () => {
  707. Cards.update(
  708. {
  709. assignees: {
  710. $exists: false,
  711. },
  712. },
  713. {
  714. $set: {
  715. assignees: [],
  716. },
  717. },
  718. noValidateMulti,
  719. );
  720. });
  721. Migrations.add('add-profile-showDesktopDragHandles', () => {
  722. Users.update(
  723. {
  724. 'profile.showDesktopDragHandles': {
  725. $exists: false,
  726. },
  727. },
  728. {
  729. $set: {
  730. 'profile.showDesktopDragHandles': false,
  731. },
  732. },
  733. noValidateMulti,
  734. );
  735. });
  736. Migrations.add('add-profile-hiddenMinicardLabelText', () => {
  737. Users.update(
  738. {
  739. 'profile.hiddenMinicardLabelText': {
  740. $exists: false,
  741. },
  742. },
  743. {
  744. $set: {
  745. 'profile.hiddenMinicardLabelText': false,
  746. },
  747. },
  748. noValidateMulti,
  749. );
  750. });
  751. Migrations.add('add-receiveddate-allowed', () => {
  752. Boards.update(
  753. {
  754. allowsReceivedDate: {
  755. $exists: false,
  756. },
  757. },
  758. {
  759. $set: {
  760. allowsReceivedDate: true,
  761. },
  762. },
  763. noValidateMulti,
  764. );
  765. });
  766. Migrations.add('add-startdate-allowed', () => {
  767. Boards.update(
  768. {
  769. allowsStartDate: {
  770. $exists: false,
  771. },
  772. },
  773. {
  774. $set: {
  775. allowsStartDate: true,
  776. },
  777. },
  778. noValidateMulti,
  779. );
  780. });
  781. Migrations.add('add-duedate-allowed', () => {
  782. Boards.update(
  783. {
  784. allowsDueDate: {
  785. $exists: false,
  786. },
  787. },
  788. {
  789. $set: {
  790. allowsDueDate: true,
  791. },
  792. },
  793. noValidateMulti,
  794. );
  795. });
  796. Migrations.add('add-enddate-allowed', () => {
  797. Boards.update(
  798. {
  799. allowsEndDate: {
  800. $exists: false,
  801. },
  802. },
  803. {
  804. $set: {
  805. allowsEndDate: true,
  806. },
  807. },
  808. noValidateMulti,
  809. );
  810. });
  811. Migrations.add('add-members-allowed', () => {
  812. Boards.update(
  813. {
  814. allowsMembers: {
  815. $exists: false,
  816. },
  817. },
  818. {
  819. $set: {
  820. allowsMembers: true,
  821. },
  822. },
  823. noValidateMulti,
  824. );
  825. });
  826. Migrations.add('add-assignee-allowed', () => {
  827. Boards.update(
  828. {
  829. allowsAssignee: {
  830. $exists: false,
  831. },
  832. },
  833. {
  834. $set: {
  835. allowsAssignee: true,
  836. },
  837. },
  838. noValidateMulti,
  839. );
  840. });
  841. Migrations.add('add-labels-allowed', () => {
  842. Boards.update(
  843. {
  844. allowsLabels: {
  845. $exists: false,
  846. },
  847. },
  848. {
  849. $set: {
  850. allowsLabels: true,
  851. },
  852. },
  853. noValidateMulti,
  854. );
  855. });
  856. Migrations.add('add-checklists-allowed', () => {
  857. Boards.update(
  858. {
  859. allowsChecklists: {
  860. $exists: false,
  861. },
  862. },
  863. {
  864. $set: {
  865. allowsChecklists: true,
  866. },
  867. },
  868. noValidateMulti,
  869. );
  870. });
  871. Migrations.add('add-attachments-allowed', () => {
  872. Boards.update(
  873. {
  874. allowsAttachments: {
  875. $exists: false,
  876. },
  877. },
  878. {
  879. $set: {
  880. allowsAttachments: true,
  881. },
  882. },
  883. noValidateMulti,
  884. );
  885. });
  886. Migrations.add('add-comments-allowed', () => {
  887. Boards.update(
  888. {
  889. allowsComments: {
  890. $exists: false,
  891. },
  892. },
  893. {
  894. $set: {
  895. allowsComments: true,
  896. },
  897. },
  898. noValidateMulti,
  899. );
  900. });
  901. Migrations.add('add-assigned-by-allowed', () => {
  902. Boards.update(
  903. {
  904. allowsAssignedBy: {
  905. $exists: false,
  906. },
  907. },
  908. {
  909. $set: {
  910. allowsAssignedBy: true,
  911. },
  912. },
  913. noValidateMulti,
  914. );
  915. });
  916. Migrations.add('add-requested-by-allowed', () => {
  917. Boards.update(
  918. {
  919. allowsRequestedBy: {
  920. $exists: false,
  921. },
  922. },
  923. {
  924. $set: {
  925. allowsRequestedBy: true,
  926. },
  927. },
  928. noValidateMulti,
  929. );
  930. });
  931. Migrations.add('add-activities-allowed', () => {
  932. Boards.update(
  933. {
  934. allowsActivities: {
  935. $exists: false,
  936. },
  937. },
  938. {
  939. $set: {
  940. allowsActivities: true,
  941. },
  942. },
  943. noValidateMulti,
  944. );
  945. });
  946. Migrations.add('add-description-title-allowed', () => {
  947. Boards.update(
  948. {
  949. allowsDescriptionTitle: {
  950. $exists: false,
  951. },
  952. },
  953. {
  954. $set: {
  955. allowsDescriptionTitle: true,
  956. },
  957. },
  958. noValidateMulti,
  959. );
  960. });
  961. Migrations.add('add-description-text-allowed', () => {
  962. Boards.update(
  963. {
  964. allowsDescriptionText: {
  965. $exists: false,
  966. },
  967. },
  968. {
  969. $set: {
  970. allowsDescriptionText: true,
  971. },
  972. },
  973. noValidateMulti,
  974. );
  975. });
  976. Migrations.add('add-sort-field-to-boards', () => {
  977. Boards.find().forEach((board, index) => {
  978. if (!board.hasOwnProperty('sort')) {
  979. Boards.direct.update(board._id, { $set: { sort: index } }, noValidate);
  980. }
  981. });
  982. });
  983. Migrations.add('add-default-profile-view', () => {
  984. Users.find().forEach(user => {
  985. if (!user.hasOwnProperty('profile.boardView')) {
  986. // Set default view
  987. Users.direct.update(
  988. { _id: user._id },
  989. { $set: { 'profile.boardView': 'board-view-swimlanes' } },
  990. noValidate,
  991. );
  992. }
  993. });
  994. });
  995. Migrations.add('add-hide-logo-by-default', () => {
  996. Settings.update(
  997. {
  998. hideLogo: {
  999. hideLogo: false,
  1000. },
  1001. },
  1002. {
  1003. $set: {
  1004. hideLogo: true,
  1005. },
  1006. },
  1007. noValidateMulti,
  1008. );
  1009. });
  1010. Migrations.add('add-card-number-allowed', () => {
  1011. Boards.update(
  1012. {
  1013. allowsCardNumber: {
  1014. $exists: false,
  1015. },
  1016. },
  1017. {
  1018. $set: {
  1019. allowsCardNumber: false,
  1020. },
  1021. },
  1022. noValidateMulti,
  1023. );
  1024. });
  1025. Migrations.add('assign-boardwise-card-numbers', () => {
  1026. Boards.find().forEach(board => {
  1027. let nextCardNumber = board.getNextCardNumber();
  1028. Cards.find(
  1029. {
  1030. boardId: board._id,
  1031. cardNumber: {
  1032. $exists: false
  1033. }
  1034. },
  1035. {
  1036. sort: { createdAt: 1 }
  1037. }
  1038. ).forEach(card => {
  1039. Cards.update(
  1040. card._id,
  1041. { $set: { cardNumber: nextCardNumber } },
  1042. noValidate);
  1043. nextCardNumber++;
  1044. });
  1045. })
  1046. });
  1047. Migrations.add('add-card-details-show-lists', () => {
  1048. Boards.update(
  1049. {
  1050. allowsShowLists: {
  1051. $exists: false,
  1052. },
  1053. },
  1054. {
  1055. $set: {
  1056. allowsShowLists: true,
  1057. },
  1058. },
  1059. noValidateMulti,
  1060. );
  1061. });
  1062. Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
  1063. //const storagePath = Attachments.storagePath();
  1064. const storagePath = process.env.WRITABLE_PATH || `./wekan-uploads`;
  1065. if (!fs.existsSync(storagePath)) {
  1066. console.log("create storagePath because it doesn't exist: " + storagePath);
  1067. fs.mkdirSync(storagePath, { recursive: true });
  1068. }
  1069. AttachmentsOld.find().forEach(function(fileObj) {
  1070. const newFileName = fileObj.name();
  1071. const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1072. // This is "example" variable, change it to the userId that you might be using.
  1073. const userId = fileObj.userId;
  1074. const fileType = fileObj.type();
  1075. const fileSize = fileObj.size();
  1076. const fileId = fileObj._id;
  1077. const readStream = fileObj.createReadStream('attachments');
  1078. const writeStream = fs.createWriteStream(filePath);
  1079. writeStream.on('error', error => {
  1080. console.error('[writeStream error]: ', error, filePath);
  1081. });
  1082. readStream.on('error', error => {
  1083. console.error('[readStream error]: ', error, filePath);
  1084. });
  1085. // Once we have a file, then upload it to our new data storage
  1086. readStream.on('end', () => {
  1087. console.log('Ended: ', filePath);
  1088. // UserFiles is the new Meteor-Files/FilesCollection collection instance
  1089. Attachments.addFile(
  1090. filePath,
  1091. {
  1092. fileName: newFileName,
  1093. type: fileType,
  1094. meta: {
  1095. boardId: fileObj.boardId,
  1096. cardId: fileObj.cardId,
  1097. listId: fileObj.listId,
  1098. swimlaneId: fileObj.swimlaneId,
  1099. source: 'import,'
  1100. },
  1101. userId,
  1102. size: fileSize,
  1103. fileId,
  1104. },
  1105. (error, fileRef) => {
  1106. if (error) {
  1107. console.error('[Attachments#addFile error]: ', error);
  1108. } else {
  1109. console.log('File Inserted: ', fileRef);
  1110. // Set the userId again
  1111. Attachments.update({ _id: fileRef._id }, { $set: { userId } });
  1112. fileObj.remove();
  1113. }
  1114. },
  1115. true,
  1116. ); // proceedAfterUpload
  1117. });
  1118. readStream.pipe(writeStream);
  1119. });
  1120. });
  1121. Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
  1122. //const storagePath = Avatars.storagePath();
  1123. const storagePath = process.env.WRITABLE_PATH || `./wekan-uploads`;
  1124. if (!fs.existsSync(storagePath)) {
  1125. console.log("create storagePath because it doesn't exist: " + storagePath);
  1126. fs.mkdirSync(storagePath, { recursive: true });
  1127. }
  1128. AvatarsOld.find().forEach(function(fileObj) {
  1129. const newFileName = fileObj.name();
  1130. const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1131. // This is "example" variable, change it to the userId that you might be using.
  1132. const userId = fileObj.userId;
  1133. const fileType = fileObj.type();
  1134. const fileSize = fileObj.size();
  1135. const fileId = fileObj._id;
  1136. const readStream = fileObj.createReadStream('avatars');
  1137. const writeStream = fs.createWriteStream(filePath);
  1138. writeStream.on('error', error => {
  1139. console.error('[writeStream error]: ', error, filePath);
  1140. });
  1141. readStream.on('error', error => {
  1142. console.error('[readStream error]: ', error, filePath);
  1143. });
  1144. // Once we have a file, then upload it to our new data storage
  1145. readStream.on('end', () => {
  1146. console.log('Ended: ', filePath);
  1147. // UserFiles is the new Meteor-Files/FilesCollection collection instance
  1148. Avatars.addFile(
  1149. filePath,
  1150. {
  1151. fileName: newFileName,
  1152. type: fileType,
  1153. meta: {
  1154. boardId: fileObj.boardId,
  1155. cardId: fileObj.cardId,
  1156. listId: fileObj.listId,
  1157. swimlaneId: fileObj.swimlaneId,
  1158. },
  1159. userId,
  1160. size: fileSize,
  1161. fileId,
  1162. },
  1163. (error, fileRef) => {
  1164. if (error) {
  1165. console.error('[Avatars#addFile error]: ', error);
  1166. } else {
  1167. console.log('File Inserted: ', newFileName, fileRef);
  1168. // Set the userId again
  1169. Avatars.update({ _id: fileRef._id }, { $set: { userId } });
  1170. Users.find().forEach(user => {
  1171. const old_url = fileObj.url();
  1172. new_url = Avatars.findOne({ _id: fileRef._id }).link(
  1173. 'original',
  1174. '/',
  1175. );
  1176. if (user.profile.avatarUrl.startsWith(old_url)) {
  1177. // Set avatar url to new url
  1178. Users.direct.update(
  1179. { _id: user._id },
  1180. { $set: { 'profile.avatarUrl': new_url } },
  1181. noValidate,
  1182. );
  1183. console.log('User avatar updated: ', user._id, new_url);
  1184. }
  1185. });
  1186. fileObj.remove();
  1187. }
  1188. },
  1189. true, // proceedAfterUpload
  1190. );
  1191. });
  1192. readStream.pipe(writeStream);
  1193. });
  1194. });
  1195. Migrations.add('migrate-attachment-drop-index-cardId', () => {
  1196. try {
  1197. Attachments.collection._dropIndex({'cardId': 1});
  1198. } catch (error) {
  1199. }
  1200. });