migrations.js 27 KB

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