migrations.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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('add-cardcounterlist-allowed', () => {
  72. Boards.update(
  73. {
  74. allowsCardCounterList: {
  75. $exists: false,
  76. },
  77. },
  78. {
  79. $set: {
  80. allowsCardCounterList: true,
  81. },
  82. },
  83. noValidateMulti,
  84. );
  85. });
  86. /*
  87. Migrations.add('add-boardmemberlist-allowed', () => {
  88. Boards.update(
  89. {
  90. allowsBoardMemberList: {
  91. $exists: false,
  92. },
  93. },
  94. {
  95. $set: {
  96. allowsBoardMemberList: true,
  97. },
  98. },
  99. noValidateMulti,
  100. );
  101. });
  102. */
  103. Migrations.add('lowercase-board-permission', () => {
  104. ['Public', 'Private'].forEach(permission => {
  105. Boards.update(
  106. { permission },
  107. { $set: { permission: permission.toLowerCase() } },
  108. noValidateMulti,
  109. );
  110. });
  111. });
  112. // Security migration: see https://github.com/wekan/wekan/issues/99
  113. Migrations.add('change-attachments-type-for-non-images', () => {
  114. const newTypeForNonImage = 'application/octet-stream';
  115. Attachments.find().forEach(file => {
  116. if (!file.isImage()) {
  117. Attachments.update(
  118. file._id,
  119. {
  120. $set: {
  121. 'original.type': newTypeForNonImage,
  122. 'copies.attachments.type': newTypeForNonImage,
  123. },
  124. },
  125. noValidate,
  126. );
  127. }
  128. });
  129. });
  130. Migrations.add('card-covers', () => {
  131. Cards.find().forEach(card => {
  132. const cover = Attachments.findOne({ cardId: card._id, cover: true });
  133. if (cover) {
  134. Cards.update(card._id, { $set: { coverId: cover._id } }, noValidate);
  135. }
  136. });
  137. Attachments.update({}, { $unset: { cover: '' } }, noValidateMulti);
  138. });
  139. Migrations.add('use-css-class-for-boards-colors', () => {
  140. const associationTable = {
  141. '#27AE60': 'nephritis',
  142. '#C0392B': 'pomegranate',
  143. '#2980B9': 'belize',
  144. '#8E44AD': 'wisteria',
  145. '#2C3E50': 'midnight',
  146. '#E67E22': 'pumpkin',
  147. '#CD5A91': 'moderatepink',
  148. '#00AECC': 'strongcyan',
  149. '#4BBF6B': 'limegreen',
  150. '#2C3E51': 'dark',
  151. '#27AE61': 'relax',
  152. '#568BA2': 'corteza',
  153. '#499BEA': 'clearblue',
  154. '#596557': 'natural',
  155. '#2A80B8': 'modern',
  156. '#2a2a2a': 'moderndark',
  157. '#222222': 'exodark',
  158. };
  159. Boards.find().forEach(board => {
  160. const oldBoardColor = board.background.color;
  161. const newBoardColor = associationTable[oldBoardColor];
  162. Boards.update(
  163. board._id,
  164. {
  165. $set: { color: newBoardColor },
  166. $unset: { background: '' },
  167. },
  168. noValidate,
  169. );
  170. });
  171. });
  172. Migrations.add('denormalize-star-number-per-board', () => {
  173. Boards.find().forEach(board => {
  174. const nStars = Users.find({ 'profile.starredBoards': board._id }).count();
  175. Boards.update(board._id, { $set: { stars: nStars } }, noValidate);
  176. });
  177. });
  178. // We want to keep a trace of former members so we can efficiently publish their
  179. // infos in the general board publication.
  180. Migrations.add('add-member-isactive-field', () => {
  181. Boards.find({}, { fields: { members: 1 } }).forEach(board => {
  182. const allUsersWithSomeActivity = _.chain(
  183. Activities.find(
  184. { boardId: board._id },
  185. { fields: { userId: 1 } },
  186. ).fetch(),
  187. )
  188. .pluck('userId')
  189. .uniq()
  190. .value();
  191. const currentUsers = _.pluck(board.members, 'userId');
  192. const formerUsers = _.difference(allUsersWithSomeActivity, currentUsers);
  193. const newMemberSet = [];
  194. board.members.forEach(member => {
  195. member.isActive = true;
  196. newMemberSet.push(member);
  197. });
  198. formerUsers.forEach(userId => {
  199. newMemberSet.push({
  200. userId,
  201. isAdmin: false,
  202. isActive: false,
  203. });
  204. });
  205. Boards.update(board._id, { $set: { members: newMemberSet } }, noValidate);
  206. });
  207. });
  208. Migrations.add('add-sort-checklists', () => {
  209. Checklists.find().forEach((checklist, index) => {
  210. if (!checklist.hasOwnProperty('sort')) {
  211. Checklists.direct.update(
  212. checklist._id,
  213. { $set: { sort: index } },
  214. noValidate,
  215. );
  216. }
  217. checklist.items.forEach((item, index) => {
  218. if (!item.hasOwnProperty('sort')) {
  219. Checklists.direct.update(
  220. { _id: checklist._id, 'items._id': item._id },
  221. { $set: { 'items.$.sort': index } },
  222. noValidate,
  223. );
  224. }
  225. });
  226. });
  227. });
  228. Migrations.add('add-swimlanes', () => {
  229. Boards.find().forEach(board => {
  230. const swimlaneId = board.getDefaultSwimline()._id;
  231. Cards.find({ boardId: board._id }).forEach(card => {
  232. if (!card.hasOwnProperty('swimlaneId')) {
  233. Cards.direct.update(
  234. { _id: card._id },
  235. { $set: { swimlaneId } },
  236. noValidate,
  237. );
  238. }
  239. });
  240. });
  241. });
  242. Migrations.add('add-views', () => {
  243. Boards.find().forEach(board => {
  244. if (!board.hasOwnProperty('view')) {
  245. Boards.direct.update(
  246. { _id: board._id },
  247. { $set: { view: 'board-view-swimlanes' } },
  248. noValidate,
  249. );
  250. }
  251. });
  252. });
  253. Migrations.add('add-checklist-items', () => {
  254. Checklists.find().forEach(checklist => {
  255. // Create new items
  256. _.sortBy(checklist.items, 'sort').forEach((item, index) => {
  257. ChecklistItems.direct.insert({
  258. title: item.title ? item.title : 'Checklist',
  259. sort: index,
  260. isFinished: item.isFinished,
  261. checklistId: checklist._id,
  262. cardId: checklist.cardId,
  263. });
  264. });
  265. // Delete old ones
  266. Checklists.direct.update(
  267. { _id: checklist._id },
  268. { $unset: { items: 1 } },
  269. noValidate,
  270. );
  271. });
  272. });
  273. Migrations.add('add-card-types', () => {
  274. Cards.find().forEach(card => {
  275. Cards.direct.update(
  276. { _id: card._id },
  277. {
  278. $set: {
  279. type: 'cardType-card',
  280. linkedId: null,
  281. },
  282. },
  283. noValidate,
  284. );
  285. });
  286. });
  287. Migrations.add('add-custom-fields-to-cards', () => {
  288. Cards.update(
  289. {
  290. customFields: {
  291. $exists: false,
  292. },
  293. },
  294. {
  295. $set: {
  296. customFields: [],
  297. },
  298. },
  299. noValidateMulti,
  300. );
  301. });
  302. Migrations.add('add-requester-field', () => {
  303. Cards.update(
  304. {
  305. requestedBy: {
  306. $exists: false,
  307. },
  308. },
  309. {
  310. $set: {
  311. requestedBy: '',
  312. },
  313. },
  314. noValidateMulti,
  315. );
  316. });
  317. Migrations.add('add-assigner-field', () => {
  318. Cards.update(
  319. {
  320. assignedBy: {
  321. $exists: false,
  322. },
  323. },
  324. {
  325. $set: {
  326. assignedBy: '',
  327. },
  328. },
  329. noValidateMulti,
  330. );
  331. });
  332. Migrations.add('add-parent-field-to-cards', () => {
  333. Cards.update(
  334. {
  335. parentId: {
  336. $exists: false,
  337. },
  338. },
  339. {
  340. $set: {
  341. parentId: '',
  342. },
  343. },
  344. noValidateMulti,
  345. );
  346. });
  347. Migrations.add('add-subtasks-boards', () => {
  348. Boards.update(
  349. {
  350. subtasksDefaultBoardId: {
  351. $exists: false,
  352. },
  353. },
  354. {
  355. $set: {
  356. subtasksDefaultBoardId: null,
  357. subtasksDefaultListId: null,
  358. },
  359. },
  360. noValidateMulti,
  361. );
  362. });
  363. Migrations.add('add-subtasks-sort', () => {
  364. Boards.update(
  365. {
  366. subtaskSort: {
  367. $exists: false,
  368. },
  369. },
  370. {
  371. $set: {
  372. subtaskSort: -1,
  373. },
  374. },
  375. noValidateMulti,
  376. );
  377. });
  378. Migrations.add('add-subtasks-allowed', () => {
  379. Boards.update(
  380. {
  381. allowsSubtasks: {
  382. $exists: false,
  383. },
  384. },
  385. {
  386. $set: {
  387. allowsSubtasks: true,
  388. },
  389. },
  390. noValidateMulti,
  391. );
  392. });
  393. Migrations.add('add-subtasks-allowed', () => {
  394. Boards.update(
  395. {
  396. presentParentTask: {
  397. $exists: false,
  398. },
  399. },
  400. {
  401. $set: {
  402. presentParentTask: 'no-parent',
  403. },
  404. },
  405. noValidateMulti,
  406. );
  407. });
  408. Migrations.add('add-authenticationMethod', () => {
  409. Users.update(
  410. {
  411. authenticationMethod: {
  412. $exists: false,
  413. },
  414. },
  415. {
  416. $set: {
  417. authenticationMethod: 'password',
  418. },
  419. },
  420. noValidateMulti,
  421. );
  422. });
  423. Migrations.add('remove-tag', () => {
  424. Users.update(
  425. {},
  426. {
  427. $unset: {
  428. 'profile.tags': 1,
  429. },
  430. },
  431. noValidateMulti,
  432. );
  433. });
  434. Migrations.add('remove-customFields-references-broken', () => {
  435. Cards.update(
  436. { 'customFields.$value': null },
  437. {
  438. $pull: {
  439. customFields: { value: null },
  440. },
  441. },
  442. noValidateMulti,
  443. );
  444. });
  445. Migrations.add('add-product-name', () => {
  446. Settings.update(
  447. {
  448. productName: {
  449. $exists: false,
  450. },
  451. },
  452. {
  453. $set: {
  454. productName: '',
  455. },
  456. },
  457. noValidateMulti,
  458. );
  459. });
  460. Migrations.add('add-hide-logo', () => {
  461. Settings.update(
  462. {
  463. hideLogo: {
  464. $exists: false,
  465. },
  466. },
  467. {
  468. $set: {
  469. hideLogo: false,
  470. },
  471. },
  472. noValidateMulti,
  473. );
  474. });
  475. Migrations.add('add-displayAuthenticationMethod', () => {
  476. Settings.update(
  477. {
  478. displayAuthenticationMethod: {
  479. $exists: false,
  480. },
  481. },
  482. {
  483. $set: {
  484. displayAuthenticationMethod: true,
  485. },
  486. },
  487. noValidateMulti,
  488. );
  489. });
  490. Migrations.add('add-defaultAuthenticationMethod', () => {
  491. Settings.update(
  492. {
  493. defaultAuthenticationMethod: {
  494. $exists: false,
  495. },
  496. },
  497. {
  498. $set: {
  499. defaultAuthenticationMethod: 'password',
  500. },
  501. },
  502. noValidateMulti,
  503. );
  504. });
  505. Migrations.add('add-templates', () => {
  506. Boards.update(
  507. {
  508. type: {
  509. $exists: false,
  510. },
  511. },
  512. {
  513. $set: {
  514. type: 'board',
  515. },
  516. },
  517. noValidateMulti,
  518. );
  519. Swimlanes.update(
  520. {
  521. type: {
  522. $exists: false,
  523. },
  524. },
  525. {
  526. $set: {
  527. type: 'swimlane',
  528. },
  529. },
  530. noValidateMulti,
  531. );
  532. Lists.update(
  533. {
  534. type: {
  535. $exists: false,
  536. },
  537. swimlaneId: {
  538. $exists: false,
  539. },
  540. },
  541. {
  542. $set: {
  543. type: 'list',
  544. swimlaneId: '',
  545. },
  546. },
  547. noValidateMulti,
  548. );
  549. Users.find({
  550. 'profile.templatesBoardId': {
  551. $exists: false,
  552. },
  553. }).forEach(user => {
  554. // Create board and swimlanes
  555. Boards.insert(
  556. {
  557. title: TAPi18n.__('templates'),
  558. permission: 'private',
  559. type: 'template-container',
  560. members: [
  561. {
  562. userId: user._id,
  563. isAdmin: true,
  564. isActive: true,
  565. isNoComments: false,
  566. isCommentOnly: false,
  567. },
  568. ],
  569. },
  570. (err, boardId) => {
  571. // Insert the reference to our templates board
  572. Users.update(user._id, {
  573. $set: { 'profile.templatesBoardId': boardId },
  574. });
  575. // Insert the card templates swimlane
  576. Swimlanes.insert(
  577. {
  578. title: TAPi18n.__('card-templates-swimlane'),
  579. boardId,
  580. sort: 1,
  581. type: 'template-container',
  582. },
  583. (err, swimlaneId) => {
  584. // Insert the reference to out card templates swimlane
  585. Users.update(user._id, {
  586. $set: { 'profile.cardTemplatesSwimlaneId': swimlaneId },
  587. });
  588. },
  589. );
  590. // Insert the list templates swimlane
  591. Swimlanes.insert(
  592. {
  593. title: TAPi18n.__('list-templates-swimlane'),
  594. boardId,
  595. sort: 2,
  596. type: 'template-container',
  597. },
  598. (err, swimlaneId) => {
  599. // Insert the reference to out list templates swimlane
  600. Users.update(user._id, {
  601. $set: { 'profile.listTemplatesSwimlaneId': swimlaneId },
  602. });
  603. },
  604. );
  605. // Insert the board templates swimlane
  606. Swimlanes.insert(
  607. {
  608. title: TAPi18n.__('board-templates-swimlane'),
  609. boardId,
  610. sort: 3,
  611. type: 'template-container',
  612. },
  613. (err, swimlaneId) => {
  614. // Insert the reference to out board templates swimlane
  615. Users.update(user._id, {
  616. $set: { 'profile.boardTemplatesSwimlaneId': swimlaneId },
  617. });
  618. },
  619. );
  620. },
  621. );
  622. });
  623. });
  624. Migrations.add('fix-circular-reference_', () => {
  625. Cards.find().forEach(card => {
  626. if (card.parentId === card._id) {
  627. Cards.update(card._id, { $set: { parentId: '' } }, noValidateMulti);
  628. }
  629. });
  630. });
  631. Migrations.add('mutate-boardIds-in-customfields', () => {
  632. CustomFields.find().forEach(cf => {
  633. CustomFields.update(
  634. cf,
  635. {
  636. $set: {
  637. boardIds: [cf.boardId],
  638. },
  639. $unset: {
  640. boardId: '',
  641. },
  642. },
  643. noValidateMulti,
  644. );
  645. });
  646. });
  647. const modifiedAtTables = [
  648. AccountSettings,
  649. TableVisibilityModeSettings,
  650. Actions,
  651. Activities,
  652. Announcements,
  653. Boards,
  654. CardComments,
  655. Cards,
  656. ChecklistItems,
  657. Checklists,
  658. CustomFields,
  659. Integrations,
  660. InvitationCodes,
  661. Lists,
  662. Rules,
  663. Settings,
  664. Swimlanes,
  665. Triggers,
  666. UnsavedEdits,
  667. Users,
  668. ];
  669. Migrations.add('add-missing-created-and-modified', () => {
  670. Promise.all(
  671. modifiedAtTables.map(db =>
  672. db
  673. .rawCollection()
  674. .update(
  675. { modifiedAt: { $exists: false } },
  676. { $set: { modifiedAt: new Date() } },
  677. { multi: true },
  678. )
  679. .then(() =>
  680. db
  681. .rawCollection()
  682. .update(
  683. { createdAt: { $exists: false } },
  684. { $set: { createdAt: new Date() } },
  685. { multi: true },
  686. ),
  687. ),
  688. ),
  689. )
  690. .then(() => {
  691. // eslint-disable-next-line no-console
  692. console.info('Successfully added createdAt and updatedAt to all tables');
  693. })
  694. .catch(e => {
  695. // eslint-disable-next-line no-console
  696. console.error(e);
  697. });
  698. });
  699. Migrations.add('fix-incorrect-dates', () => {
  700. const tables = [
  701. AccountSettings,
  702. TableVisibilityModeSettings,
  703. Actions,
  704. Activities,
  705. Announcements,
  706. Boards,
  707. CardComments,
  708. Cards,
  709. ChecklistItems,
  710. Checklists,
  711. CustomFields,
  712. Integrations,
  713. InvitationCodes,
  714. Lists,
  715. Rules,
  716. Settings,
  717. Swimlanes,
  718. Triggers,
  719. UnsavedEdits,
  720. ];
  721. // Dates were previously created with Date.now() which is a number, not a date
  722. tables.forEach(t =>
  723. t
  724. .rawCollection()
  725. .find({ $or: [{ createdAt: { $type: 1 } }, { updatedAt: { $type: 1 } }] })
  726. .forEach(({ _id, createdAt, updatedAt }) => {
  727. t.rawCollection().update(
  728. { _id },
  729. {
  730. $set: {
  731. createdAt: new Date(createdAt),
  732. updatedAt: new Date(updatedAt),
  733. },
  734. },
  735. );
  736. }),
  737. );
  738. });
  739. Migrations.add('add-assignee', () => {
  740. Cards.update(
  741. {
  742. assignees: {
  743. $exists: false,
  744. },
  745. },
  746. {
  747. $set: {
  748. assignees: [],
  749. },
  750. },
  751. noValidateMulti,
  752. );
  753. });
  754. Migrations.add('add-profile-showDesktopDragHandles', () => {
  755. Users.update(
  756. {
  757. 'profile.showDesktopDragHandles': {
  758. $exists: false,
  759. },
  760. },
  761. {
  762. $set: {
  763. 'profile.showDesktopDragHandles': false,
  764. },
  765. },
  766. noValidateMulti,
  767. );
  768. });
  769. Migrations.add('add-profile-hiddenMinicardLabelText', () => {
  770. Users.update(
  771. {
  772. 'profile.hiddenMinicardLabelText': {
  773. $exists: false,
  774. },
  775. },
  776. {
  777. $set: {
  778. 'profile.hiddenMinicardLabelText': false,
  779. },
  780. },
  781. noValidateMulti,
  782. );
  783. });
  784. Migrations.add('add-receiveddate-allowed', () => {
  785. Boards.update(
  786. {
  787. allowsReceivedDate: {
  788. $exists: false,
  789. },
  790. },
  791. {
  792. $set: {
  793. allowsReceivedDate: true,
  794. },
  795. },
  796. noValidateMulti,
  797. );
  798. });
  799. Migrations.add('add-startdate-allowed', () => {
  800. Boards.update(
  801. {
  802. allowsStartDate: {
  803. $exists: false,
  804. },
  805. },
  806. {
  807. $set: {
  808. allowsStartDate: true,
  809. },
  810. },
  811. noValidateMulti,
  812. );
  813. });
  814. Migrations.add('add-duedate-allowed', () => {
  815. Boards.update(
  816. {
  817. allowsDueDate: {
  818. $exists: false,
  819. },
  820. },
  821. {
  822. $set: {
  823. allowsDueDate: true,
  824. },
  825. },
  826. noValidateMulti,
  827. );
  828. });
  829. Migrations.add('add-enddate-allowed', () => {
  830. Boards.update(
  831. {
  832. allowsEndDate: {
  833. $exists: false,
  834. },
  835. },
  836. {
  837. $set: {
  838. allowsEndDate: true,
  839. },
  840. },
  841. noValidateMulti,
  842. );
  843. });
  844. Migrations.add('add-members-allowed', () => {
  845. Boards.update(
  846. {
  847. allowsMembers: {
  848. $exists: false,
  849. },
  850. },
  851. {
  852. $set: {
  853. allowsMembers: true,
  854. },
  855. },
  856. noValidateMulti,
  857. );
  858. });
  859. Migrations.add('add-assignee-allowed', () => {
  860. Boards.update(
  861. {
  862. allowsAssignee: {
  863. $exists: false,
  864. },
  865. },
  866. {
  867. $set: {
  868. allowsAssignee: true,
  869. },
  870. },
  871. noValidateMulti,
  872. );
  873. });
  874. Migrations.add('add-labels-allowed', () => {
  875. Boards.update(
  876. {
  877. allowsLabels: {
  878. $exists: false,
  879. },
  880. },
  881. {
  882. $set: {
  883. allowsLabels: true,
  884. },
  885. },
  886. noValidateMulti,
  887. );
  888. });
  889. Migrations.add('add-checklists-allowed', () => {
  890. Boards.update(
  891. {
  892. allowsChecklists: {
  893. $exists: false,
  894. },
  895. },
  896. {
  897. $set: {
  898. allowsChecklists: true,
  899. },
  900. },
  901. noValidateMulti,
  902. );
  903. });
  904. Migrations.add('add-attachments-allowed', () => {
  905. Boards.update(
  906. {
  907. allowsAttachments: {
  908. $exists: false,
  909. },
  910. },
  911. {
  912. $set: {
  913. allowsAttachments: true,
  914. },
  915. },
  916. noValidateMulti,
  917. );
  918. });
  919. Migrations.add('add-comments-allowed', () => {
  920. Boards.update(
  921. {
  922. allowsComments: {
  923. $exists: false,
  924. },
  925. },
  926. {
  927. $set: {
  928. allowsComments: true,
  929. },
  930. },
  931. noValidateMulti,
  932. );
  933. });
  934. Migrations.add('add-assigned-by-allowed', () => {
  935. Boards.update(
  936. {
  937. allowsAssignedBy: {
  938. $exists: false,
  939. },
  940. },
  941. {
  942. $set: {
  943. allowsAssignedBy: true,
  944. },
  945. },
  946. noValidateMulti,
  947. );
  948. });
  949. Migrations.add('add-requested-by-allowed', () => {
  950. Boards.update(
  951. {
  952. allowsRequestedBy: {
  953. $exists: false,
  954. },
  955. },
  956. {
  957. $set: {
  958. allowsRequestedBy: true,
  959. },
  960. },
  961. noValidateMulti,
  962. );
  963. });
  964. Migrations.add('add-activities-allowed', () => {
  965. Boards.update(
  966. {
  967. allowsActivities: {
  968. $exists: false,
  969. },
  970. },
  971. {
  972. $set: {
  973. allowsActivities: true,
  974. },
  975. },
  976. noValidateMulti,
  977. );
  978. });
  979. Migrations.add('add-description-title-allowed', () => {
  980. Boards.update(
  981. {
  982. allowsDescriptionTitle: {
  983. $exists: false,
  984. },
  985. },
  986. {
  987. $set: {
  988. allowsDescriptionTitle: true,
  989. },
  990. },
  991. noValidateMulti,
  992. );
  993. });
  994. Migrations.add('add-description-text-allowed', () => {
  995. Boards.update(
  996. {
  997. allowsDescriptionText: {
  998. $exists: false,
  999. },
  1000. },
  1001. {
  1002. $set: {
  1003. allowsDescriptionText: true,
  1004. },
  1005. },
  1006. noValidateMulti,
  1007. );
  1008. });
  1009. Migrations.add('add-description-text-allowed-on-minicard', () => {
  1010. Boards.update(
  1011. {
  1012. allowsDescriptionTextOnMinicard: {
  1013. $exists: false,
  1014. },
  1015. },
  1016. {
  1017. $set: {
  1018. allowsDescriptionTextOnMinicard: true,
  1019. },
  1020. },
  1021. noValidateMulti,
  1022. );
  1023. });
  1024. Migrations.add('add-sort-field-to-boards', () => {
  1025. Boards.find().forEach((board, index) => {
  1026. if (!board.hasOwnProperty('sort')) {
  1027. Boards.direct.update(board._id, { $set: { sort: index } }, noValidate);
  1028. }
  1029. });
  1030. });
  1031. Migrations.add('add-default-profile-view', () => {
  1032. Users.find().forEach(user => {
  1033. if (!user.hasOwnProperty('profile.boardView')) {
  1034. // Set default view
  1035. Users.direct.update(
  1036. { _id: user._id },
  1037. { $set: { 'profile.boardView': 'board-view-swimlanes' } },
  1038. noValidate,
  1039. );
  1040. }
  1041. });
  1042. });
  1043. Migrations.add('add-hide-logo-by-default', () => {
  1044. Settings.update(
  1045. {
  1046. hideLogo: {
  1047. hideLogo: false,
  1048. },
  1049. },
  1050. {
  1051. $set: {
  1052. hideLogo: true,
  1053. },
  1054. },
  1055. noValidateMulti,
  1056. );
  1057. });
  1058. Migrations.add('add-card-number-allowed', () => {
  1059. Boards.update(
  1060. {
  1061. allowsCardNumber: {
  1062. $exists: false,
  1063. },
  1064. },
  1065. {
  1066. $set: {
  1067. allowsCardNumber: false,
  1068. },
  1069. },
  1070. noValidateMulti,
  1071. );
  1072. });
  1073. Migrations.add('assign-boardwise-card-numbers', () => {
  1074. Boards.find().forEach(board => {
  1075. let nextCardNumber = board.getNextCardNumber();
  1076. Cards.find(
  1077. {
  1078. boardId: board._id,
  1079. cardNumber: {
  1080. $exists: false
  1081. }
  1082. },
  1083. {
  1084. sort: { createdAt: 1 }
  1085. }
  1086. ).forEach(card => {
  1087. Cards.update(
  1088. card._id,
  1089. { $set: { cardNumber: nextCardNumber } },
  1090. noValidate);
  1091. nextCardNumber++;
  1092. });
  1093. })
  1094. });
  1095. Migrations.add('add-card-details-show-lists', () => {
  1096. Boards.update(
  1097. {
  1098. allowsShowLists: {
  1099. $exists: false,
  1100. },
  1101. },
  1102. {
  1103. $set: {
  1104. allowsShowLists: true,
  1105. },
  1106. },
  1107. noValidateMulti,
  1108. );
  1109. });
  1110. Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
  1111. //const storagePath = Attachments.storagePath();
  1112. const storagePath = process.env.WRITABLE_PATH || `./wekan-uploads`;
  1113. if (!fs.existsSync(storagePath)) {
  1114. console.log("create storagePath because it doesn't exist: " + storagePath);
  1115. fs.mkdirSync(storagePath, { recursive: true });
  1116. }
  1117. AttachmentsOld.find().forEach(function(fileObj) {
  1118. const newFileName = fileObj.name();
  1119. const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1120. // This is "example" variable, change it to the userId that you might be using.
  1121. const userId = fileObj.userId;
  1122. const fileType = fileObj.type();
  1123. const fileSize = fileObj.size();
  1124. const fileId = fileObj._id;
  1125. const readStream = fileObj.createReadStream('attachments');
  1126. const writeStream = fs.createWriteStream(filePath);
  1127. writeStream.on('error', error => {
  1128. console.error('[writeStream error]: ', error, filePath);
  1129. });
  1130. readStream.on('error', error => {
  1131. console.error('[readStream error]: ', error, filePath);
  1132. });
  1133. // Once we have a file, then upload it to our new data storage
  1134. readStream.on('end', () => {
  1135. console.log('Ended: ', filePath);
  1136. // UserFiles is the new Meteor-Files/FilesCollection collection instance
  1137. Attachments.addFile(
  1138. filePath,
  1139. {
  1140. fileName: newFileName,
  1141. type: fileType,
  1142. meta: {
  1143. boardId: fileObj.boardId,
  1144. cardId: fileObj.cardId,
  1145. listId: fileObj.listId,
  1146. swimlaneId: fileObj.swimlaneId,
  1147. source: 'import'
  1148. },
  1149. userId,
  1150. size: fileSize,
  1151. fileId,
  1152. },
  1153. (error, fileRef) => {
  1154. if (error) {
  1155. console.error('[Attachments#addFile error]: ', error);
  1156. } else {
  1157. console.log('File Inserted: ', fileRef);
  1158. // Set the userId again
  1159. Attachments.update({ _id: fileRef._id }, { $set: { userId } });
  1160. fileObj.remove();
  1161. }
  1162. },
  1163. true,
  1164. ); // proceedAfterUpload
  1165. });
  1166. readStream.pipe(writeStream);
  1167. });
  1168. });
  1169. Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
  1170. //const storagePath = Avatars.storagePath();
  1171. const storagePath = process.env.WRITABLE_PATH || `./wekan-uploads`;
  1172. if (!fs.existsSync(storagePath)) {
  1173. console.log("create storagePath because it doesn't exist: " + storagePath);
  1174. fs.mkdirSync(storagePath, { recursive: true });
  1175. }
  1176. AvatarsOld.find().forEach(function(fileObj) {
  1177. const newFileName = fileObj.name();
  1178. const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1179. // This is "example" variable, change it to the userId that you might be using.
  1180. const userId = fileObj.userId;
  1181. const fileType = fileObj.type();
  1182. const fileSize = fileObj.size();
  1183. const fileId = fileObj._id;
  1184. const readStream = fileObj.createReadStream('avatars');
  1185. const writeStream = fs.createWriteStream(filePath);
  1186. writeStream.on('error', error => {
  1187. console.error('[writeStream error]: ', error, filePath);
  1188. });
  1189. readStream.on('error', error => {
  1190. console.error('[readStream error]: ', error, filePath);
  1191. });
  1192. // Once we have a file, then upload it to our new data storage
  1193. readStream.on('end', () => {
  1194. console.log('Ended: ', filePath);
  1195. // UserFiles is the new Meteor-Files/FilesCollection collection instance
  1196. Avatars.addFile(
  1197. filePath,
  1198. {
  1199. fileName: newFileName,
  1200. type: fileType,
  1201. meta: {
  1202. boardId: fileObj.boardId,
  1203. cardId: fileObj.cardId,
  1204. listId: fileObj.listId,
  1205. swimlaneId: fileObj.swimlaneId,
  1206. },
  1207. userId,
  1208. size: fileSize,
  1209. fileId,
  1210. },
  1211. (error, fileRef) => {
  1212. if (error) {
  1213. console.error('[Avatars#addFile error]: ', error);
  1214. } else {
  1215. console.log('File Inserted: ', newFileName, fileRef);
  1216. // Set the userId again
  1217. Avatars.update({ _id: fileRef._id }, { $set: { userId } });
  1218. Users.find().forEach(user => {
  1219. const old_url = fileObj.url();
  1220. new_url = Avatars.findOne({ _id: fileRef._id }).link(
  1221. 'original',
  1222. '/',
  1223. );
  1224. if (user.profile.avatarUrl.startsWith(old_url)) {
  1225. // Set avatar url to new url
  1226. Users.direct.update(
  1227. { _id: user._id },
  1228. { $set: { 'profile.avatarUrl': new_url } },
  1229. noValidate,
  1230. );
  1231. console.log('User avatar updated: ', user._id, new_url);
  1232. }
  1233. });
  1234. fileObj.remove();
  1235. }
  1236. },
  1237. true, // proceedAfterUpload
  1238. );
  1239. });
  1240. readStream.pipe(writeStream);
  1241. });
  1242. });
  1243. Migrations.add('migrate-attachment-drop-index-cardId', () => {
  1244. try {
  1245. Attachments.collection._dropIndex({'cardId': 1});
  1246. } catch (error) {
  1247. }
  1248. });