20190624110950-fill-last-update-user.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. require('module-alias/register');
  2. const logger = require('@alias/logger')('growi:migrate:abolish-page-group-relation');
  3. const mongoose = require('mongoose');
  4. const config = require('@root/config/migrate');
  5. /**
  6. * FIX https://github.com/weseek/growi/issues/1067
  7. */
  8. module.exports = {
  9. async up(db) {
  10. logger.info('Apply migration');
  11. mongoose.connect(config.mongoUri, config.mongodb.options);
  12. const Page = require('@server/models/page')();
  13. // see https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field/37280419#37280419
  14. // retrieve target data
  15. const pages = await Page.find({
  16. $or: [
  17. { lastUpdateUser: { $exists: false } },
  18. { lastUpdateUser: { $eq: null } },
  19. ],
  20. }).select('_id creator');
  21. // create requests for bulkWrite
  22. const requests = pages.map((page) => {
  23. return {
  24. updateOne: {
  25. filter: { _id: page._id },
  26. update: { $set: { lastUpdateUser: page.creator } },
  27. },
  28. };
  29. });
  30. if (requests.length > 0) {
  31. await db.collection('pages').bulkWrite(requests);
  32. }
  33. logger.info('Migration has successfully applied');
  34. },
  35. down(db) {
  36. // do not rollback
  37. },
  38. };