migrations.js 28 KB

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