migration11.js 1.4 KB

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