migrations.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. /*
  1174. Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
  1175. Meteor.settings.public.ostrioFilesMigrationInProgress = "true";
  1176. AttachmentsOld.find().forEach(function(fileObj) {
  1177. const newFileName = fileObj.name();
  1178. const storagePath = Attachments.storagePath({});
  1179. // OLD:
  1180. // const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1181. // NEW: Save file only with filename of ObjectID, not including filename.
  1182. // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
  1183. //const filePath = path.join(storagePath, `${fileObj._id}`);
  1184. const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1185. // This is "example" variable, change it to the userId that you might be using.
  1186. const userId = fileObj.userId;
  1187. const fileType = fileObj.type();
  1188. const fileSize = fileObj.size();
  1189. const fileId = fileObj._id;
  1190. const readStream = fileObj.createReadStream('attachments');
  1191. const writeStream = fs.createWriteStream(filePath);
  1192. writeStream.on('error', function(err) {
  1193. console.log('Writing error: ', err, filePath);
  1194. });
  1195. // Once we have a file, then upload it to our new data storage
  1196. readStream.on('end', () => {
  1197. console.log('Ended: ', filePath);
  1198. // UserFiles is the new Meteor-Files/FilesCollection collection instance
  1199. Attachments.addFile(
  1200. filePath,
  1201. {
  1202. fileName: newFileName,
  1203. type: fileType,
  1204. meta: {
  1205. boardId: fileObj.boardId,
  1206. cardId: fileObj.cardId,
  1207. listId: fileObj.listId,
  1208. swimlaneId: fileObj.swimlaneId,
  1209. uploadedBeforeMigration: fileObj.uploadedAt,
  1210. migrationTime: new Date(),
  1211. copies: fileObj.copies,
  1212. source: 'import'
  1213. },
  1214. userId,
  1215. size: fileSize,
  1216. fileId,
  1217. },
  1218. (err, fileRef) => {
  1219. if (err) {
  1220. console.log(err);
  1221. } else {
  1222. console.log('File Inserted: ', fileRef._id);
  1223. // Set the userId again
  1224. Attachments.update({ _id: fileRef._id }, { $set: { userId } });
  1225. fileObj.remove();
  1226. }
  1227. },
  1228. true,
  1229. ); // proceedAfterUpload
  1230. });
  1231. readStream.on('error', error => {
  1232. console.log('Error: ', filePath, error);
  1233. });
  1234. readStream.pipe(writeStream);
  1235. });
  1236. Meteor.settings.public.ostrioFilesMigrationInProgress = "false";
  1237. });
  1238. Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
  1239. Meteor.settings.public.ostrioFilesMigrationInProgress = "true";
  1240. AvatarsOld.find().forEach(function(fileObj) {
  1241. const newFileName = fileObj.name();
  1242. const storagePath = Avatars.storagePath({});
  1243. // OLD:
  1244. // const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1245. // NEW: Save file only with filename of ObjectID, not including filename.
  1246. // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
  1247. //const filePath = path.join(storagePath, `${fileObj._id}`);
  1248. const filePath = path.join(storagePath, `${fileObj._id}-${newFileName}`);
  1249. // This is "example" variable, change it to the userId that you might be using.
  1250. const userId = fileObj.userId;
  1251. const fileType = fileObj.type();
  1252. const fileSize = fileObj.size();
  1253. const fileId = fileObj._id;
  1254. const readStream = fileObj.createReadStream('avatars');
  1255. const writeStream = fs.createWriteStream(filePath);
  1256. writeStream.on('error', function(err) {
  1257. console.log('Writing error: ', err, filePath);
  1258. });
  1259. // Once we have a file, then upload it to our new data storage
  1260. readStream.on('end', () => {
  1261. console.log('Ended: ', filePath);
  1262. // UserFiles is the new Meteor-Files/FilesCollection collection instance
  1263. Avatars.addFile(
  1264. filePath,
  1265. {
  1266. fileName: newFileName,
  1267. type: fileType,
  1268. meta: {
  1269. boardId: fileObj.boardId,
  1270. cardId: fileObj.cardId,
  1271. listId: fileObj.listId,
  1272. swimlaneId: fileObj.swimlaneId,
  1273. },
  1274. userId,
  1275. size: fileSize,
  1276. fileId,
  1277. },
  1278. (err, fileRef) => {
  1279. if (err) {
  1280. console.log(err);
  1281. } else {
  1282. console.log('File Inserted: ', newFileName, fileRef._id);
  1283. // Set the userId again
  1284. Avatars.update({ _id: fileRef._id }, { $set: { userId } });
  1285. Users.find().forEach(user => {
  1286. const old_url = fileObj.url();
  1287. new_url = Avatars.findOne({ _id: fileRef._id }).link(
  1288. 'original',
  1289. '/',
  1290. );
  1291. if (user.profile.avatarUrl !== undefined) {
  1292. if (user.profile.avatarUrl.startsWith(old_url)) {
  1293. // Set avatar url to new url
  1294. Users.direct.update(
  1295. { _id: user._id },
  1296. { $set: { 'profile.avatarUrl': new_url } },
  1297. noValidate,
  1298. );
  1299. console.log('User avatar updated: ', user._id, new_url);
  1300. }
  1301. }
  1302. });
  1303. fileObj.remove();
  1304. }
  1305. },
  1306. true, // proceedAfterUpload
  1307. );
  1308. });
  1309. readStream.on('error', error => {
  1310. console.log('Error: ', filePath, error);
  1311. });
  1312. readStream.pipe(writeStream);
  1313. });
  1314. Meteor.settings.public.ostrioFilesMigrationInProgress = "false";
  1315. });
  1316. */
  1317. Migrations.add('migrate-attachment-drop-index-cardId', () => {
  1318. try {
  1319. Attachments.collection._dropIndex({'cardId': 1});
  1320. } catch (error) {
  1321. }
  1322. });
  1323. Migrations.add('migrate-attachment-migration-fix-source-import', () => {
  1324. // there was an error at first versions, so source was import, instead of import
  1325. Attachments.update(
  1326. {"meta.source":"import,"},
  1327. {$set:{"meta.source":"import"}},
  1328. noValidateMulti
  1329. );
  1330. });
  1331. /*
  1332. Migrations.add('attachment-cardCopy-fix-boardId-etc', () => {
  1333. Attachments.find( {"meta.source": "copy"} ).forEach(_attachment => {
  1334. const cardId = _attachment.meta.cardId;
  1335. const card = Cards.findOne(cardId);
  1336. if (card.boardId !== undefined && card.listId !== undefined && card.swimlaneId !== undefined) {
  1337. console.log("update attachment id: ", _attachment._id);
  1338. Attachments.update(_attachment._id, {
  1339. $set: {
  1340. "meta.boardId": card.boardId,
  1341. "meta.listId": card.listId,
  1342. "meta.swimlaneId": card.swimlaneId,
  1343. }
  1344. });
  1345. }
  1346. });
  1347. });
  1348. */