migration15.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import async from "async";
  2. /**
  3. * Migration 15
  4. *
  5. * Migration for setting user name to username if not set
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const userModel = await MigrationModule.runJob("GET_MODEL", { modelName: "user" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 15. Finding users with document version 3.`);
  17. userModel.find({ documentVersion: 3, name: { $in: [null, ""] } }, (err, users) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. users.map(user => user._doc),
  22. 1,
  23. (user, next) => {
  24. userModel.updateOne({ _id: user._id }, { $set: { name: user.username } }, next);
  25. },
  26. err => {
  27. this.log("INFO", `Migration 15. Users found: ${users.length}.`);
  28. next(err);
  29. }
  30. );
  31. }
  32. });
  33. }
  34. ],
  35. err => {
  36. if (err) {
  37. reject(new Error(err));
  38. } else {
  39. resolve();
  40. }
  41. }
  42. );
  43. });
  44. }