migrations.js 28 KB

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