20200827045151-remove-layout-setting.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import mongoose from 'mongoose';
  2. import { getMongoUri, mongoOptions } from '@growi/core';
  3. import Config from '~/server/models/config';
  4. import loggerFactory from '~/utils/logger';
  5. const logger = loggerFactory('growi:migrate:remove-layout-setting');
  6. module.exports = {
  7. async up(db, client) {
  8. logger.info('Apply migration');
  9. mongoose.connect(getMongoUri(), mongoOptions);
  10. const layoutType = await Config.findOne({ key: 'customize:layout' });
  11. if (layoutType == null) {
  12. return;
  13. }
  14. const promise = [
  15. // remove layout
  16. Config.findOneAndDelete({ key: 'customize:layout' }),
  17. ];
  18. if (layoutType.value === '"kibela"') {
  19. promise.push(
  20. Config.update(
  21. { key: 'customize:theme' },
  22. { value: JSON.stringify('kibela') },
  23. ),
  24. );
  25. }
  26. await Promise.all(promise);
  27. logger.info('Migration has successfully applied');
  28. },
  29. async down(db, client) {
  30. logger.info('Rollback migration');
  31. mongoose.connect(getMongoUri(), mongoOptions);
  32. const theme = await Config.findOne({ key: 'customize:theme' });
  33. const insertLayoutType = (theme.value === '"kibela"') ? 'kibela' : 'growi';
  34. const insertConfig = new Config({
  35. ns: 'crowi',
  36. key: 'customize:layout',
  37. value: JSON.stringify(insertLayoutType),
  38. });
  39. const promise = [
  40. insertConfig.save(),
  41. Config.update(
  42. { key: 'customize:theme', value: JSON.stringify('kibela') },
  43. { value: JSON.stringify('default') },
  44. ),
  45. ];
  46. await Promise.all(promise);
  47. logger.info('Migration has been successfully rollbacked');
  48. },
  49. };