20200827045151-remove-layout-setting.js 1.7 KB

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