migrations.js 30 KB

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