20200827045151-remove-layout-setting.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. require('module-alias/register');
  2. const logger = require('@alias/logger')('growi:migrate:remove-layout-setting');
  3. const mongoose = require('mongoose');
  4. const config = require('@root/config/migrate');
  5. const { getModelSafely } = require('@commons/util/mongoose-utils');
  6. module.exports = {
  7. async up(db, client) {
  8. logger.info('Apply migration');
  9. mongoose.connect(config.mongoUri, config.mongodb.options);
  10. const Config = getModelSafely('Config') || require('@server/models/config')();
  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(config.mongoUri, config.mongodb.options);
  33. const Config = getModelSafely('Config') || require('@server/models/config')();
  34. const theme = await Config.findOne({ key: 'customize:theme' });
  35. const insertLayoutType = (theme.value === '"kibela"') ? 'kibela' : 'growi';
  36. const insertConfig = new Config({
  37. ns: 'crowi',
  38. key: 'customize:layout',
  39. value: JSON.stringify(insertLayoutType),
  40. });
  41. const promise = [
  42. insertConfig.save(),
  43. Config.update(
  44. { key: 'customize:theme', value: JSON.stringify('kibela') },
  45. { value: JSON.stringify('default') },
  46. ),
  47. ];
  48. await Promise.all(promise);
  49. logger.info('Migration has been successfully rollbacked');
  50. },
  51. };