migration15.js 1.1 KB

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