20200828024025-copy-aws-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. await mongoose.connect(getMongoUri(), mongoOptions);
  11. const [accessKeyId, secretAccessKey] = await Promise.all([
  12. Config.findOne({ key: 'aws:accessKeyId' }),
  13. Config.findOne({ key: 'aws:secretAccessKey' }),
  14. ]);
  15. const request = [];
  16. if (accessKeyId != null) {
  17. if (accessKeyId.value != null) {
  18. request.push({
  19. insertOne: {
  20. document: {
  21. key: 'mail:sesAccessKeyId',
  22. ns: 'crowi',
  23. value: accessKeyId.value,
  24. },
  25. },
  26. });
  27. }
  28. }
  29. if (secretAccessKey != null) {
  30. if (secretAccessKey.value != null) {
  31. request.push({
  32. insertOne: {
  33. document: {
  34. key: 'mail:sesSecretAccessKey',
  35. ns: 'crowi',
  36. value: secretAccessKey.value,
  37. },
  38. },
  39. });
  40. }
  41. }
  42. if (request.length > 0) {
  43. await Config.bulkWrite(request);
  44. }
  45. logger.info('Migration has successfully applied');
  46. },
  47. async down(db, client) {
  48. logger.info('Rollback migration');
  49. await mongoose.connect(getMongoUri(), mongoOptions);
  50. await Config.deleteMany({ key: { $in: ['mail:sesAccessKeyId', 'mail:sesSecretAccessKey'] } });
  51. logger.info('Migration has been successfully rollbacked');
  52. },
  53. };