migrations.js 31 KB

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