Просмотр исходного кода

fix: Migrations for updating data for slackbot (#4406)

* fix migrations/20210830074539-update-configs-for-slackbot.js

* fix migration files
Yuki Takei 4 лет назад
Родитель
Сommit
61b81c35ae

+ 31 - 40
packages/app/src/migrations/20210830074539-update-configs-for-slackbot.js

@@ -6,31 +6,30 @@ import loggerFactory from '~/utils/logger';
 
 const logger = loggerFactory('growi:migrate:update-configs-for-slackbot');
 
+// key: oldKey, value: newKey
+const keyMap = {
+  'slackbot:proxyServerUri': 'slackbot:proxyUri',
+  'slackbot:token': 'slackbot:withoutProxy:botToken',
+  'slackbot:signingSecret': 'slackbot:withoutProxy:signingSecret',
+};
+
 module.exports = {
   async up(db) {
     logger.info('Apply migration');
     mongoose.connect(config.mongoUri, config.mongodb.options);
 
-    await Config.bulkWrite([
-      {
-        updateOne: {
-          filter: { key: 'slackbot:proxyServerUri' },
-          update: { key: 'slackbot:proxyUri' },
-        },
-      },
-      {
-        updateOne: {
-          filter: { key: 'slackbot:token' },
-          update: { key: 'slackbot:withoutProxy:botToken' },
-        },
-      },
-      {
-        updateOne: {
-          filter: { key: 'slackbot:signingSecret' },
-          update: { key: 'slackbot:withoutProxy:signingSecret' },
-        },
-      },
-    ]);
+    for await (const [oldKey, newKey] of Object.entries(keyMap)) {
+      const isExist = (await Config.count({ key: newKey })) > 0;
+
+      // remove old key
+      if (isExist) {
+        await Config.findOneAndRemove({ key: oldKey });
+      }
+      // update with new key
+      else {
+        await Config.findOneAndUpdate({ key: oldKey }, { key: newKey });
+      }
+    }
 
     logger.info('Migration has successfully applied');
   },
@@ -39,26 +38,18 @@ module.exports = {
     logger.info('Rollback migration');
     mongoose.connect(config.mongoUri, config.mongodb.options);
 
-    await Config.bulkWrite([
-      {
-        updateOne: {
-          filter: { key: 'slackbot:proxyUri' },
-          update: { key: 'slackbot:proxyServerUri' },
-        },
-      },
-      {
-        updateOne: {
-          filter: { key: 'slackbot:withoutProxy:botToken' },
-          update: { key: 'slackbot:token' },
-        },
-      },
-      {
-        updateOne: {
-          filter: { key: 'slackbot:withoutProxy:signingSecret' },
-          update: { key: 'slackbot:signingSecret' },
-        },
-      },
-    ]);
+    for await (const [oldKey, newKey] of Object.entries(keyMap)) {
+      const isExist = (await Config.count({ key: oldKey })) > 0;
+
+      // remove new key
+      if (isExist) {
+        await Config.findOneAndRemove({ key: newKey });
+      }
+      // update with old key
+      else {
+        await Config.findOneAndUpdate({ key: newKey }, { key: oldKey });
+      }
+    }
 
     logger.info('Migration has successfully applied');
   },

+ 1 - 1
packages/app/src/migrations/20210906194521-slack-app-integration-set-default-value.js

@@ -18,7 +18,7 @@ module.exports = {
     // Add togetter command if supportedCommandsForBroadcastUse already exists
     const slackAppIntegrations = await SlackAppIntegration.find();
     slackAppIntegrations.forEach(async(doc) => {
-      if (!doc.supportedCommandsForSingleUse.includes('togetter')) {
+      if (doc.supportedCommandsForSingleUse != null && !doc.supportedCommandsForSingleUse.includes('togetter')) {
         doc.supportedCommandsForSingleUse.push('togetter');
       }
       await doc.save();