migrations.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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. });
  1364. Migrations.add('remove-user-profile-hiddenSystemMessages', () => {
  1365. Users.update(
  1366. {
  1367. "profile.hiddenSystemMessages": {
  1368. $exists: true,
  1369. },
  1370. },
  1371. {
  1372. $unset: {
  1373. "profile.hiddenSystemMessages": 1,
  1374. },
  1375. },
  1376. noValidateMulti,
  1377. );
  1378. });
  1379. Migrations.add('remove-user-profile-hideCheckedItems', () => {
  1380. Users.update(
  1381. {
  1382. "profile.hideCheckedItems": {
  1383. $exists: true,
  1384. },
  1385. },
  1386. {
  1387. $unset: {
  1388. "profile.hideCheckedItems": 1,
  1389. },
  1390. },
  1391. noValidateMulti,
  1392. );
  1393. });