migrations.js 27 KB

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