migrations.js 29 KB

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