20200828024025-copy-aws-setting.js 1.6 KB

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