migration11.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import async from "async";
  2. /**
  3. * Migration 11
  4. *
  5. * Migration for changing language of verifying a song from 'accepted' to 'verified' for songs
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 11. Finding songs with document version 4.`);
  17. songModel.find({ documentVersion: 4 }, (err, songs) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. songs.map(songi => songi._doc),
  22. 1,
  23. (songi, next) =>
  24. songModel.updateOne(
  25. { _id: songi._id },
  26. {
  27. $set: {
  28. verifiedBy: songi.acceptedBy,
  29. verifiedAt: songi.acceptedAt,
  30. documentVersion: 5
  31. },
  32. $unset: {
  33. acceptedBy: "",
  34. acceptedAt: ""
  35. }
  36. },
  37. next
  38. ),
  39. err => {
  40. if (err) next(err);
  41. else {
  42. this.log("INFO", `Migration 11. Songs found: ${songs.length}.`);
  43. next();
  44. }
  45. }
  46. );
  47. }
  48. });
  49. }
  50. ],
  51. err => {
  52. if (err) reject(new Error(err));
  53. else resolve();
  54. }
  55. );
  56. });
  57. }