migrations.js 27 KB

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