20191126173016-adjust-pages-path.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. require('module-alias/register');
  2. const logger = require('@alias/logger')('growi:migrate:adjust-pages-path');
  3. const mongoose = require('mongoose');
  4. const config = require('@root/config/migrate');
  5. const pathUtils = require('growi-commons').pathUtils;
  6. module.exports = {
  7. async up(db) {
  8. logger.info('Apply migration');
  9. mongoose.connect(config.mongoUri, config.mongodb.options);
  10. const Page = require('@server/models/page')();
  11. // retrieve target data
  12. const pages = await Page.find({ path: /^(?!\/)/ });
  13. // create requests for bulkWrite
  14. const requests = pages.map((page) => {
  15. const adjustedPath = pathUtils.addHeadingSlash(page.path);
  16. return {
  17. updateOne: {
  18. filter: { _id: page._id },
  19. update: { $set: { path: adjustedPath } },
  20. },
  21. };
  22. });
  23. if (requests.length > 0) {
  24. await db.collection('pages').bulkWrite(requests);
  25. }
  26. logger.info('Migration has successfully applied');
  27. },
  28. down(db) {
  29. // do not rollback
  30. },
  31. };