Yuki Takei 6 лет назад
Родитель
Сommit
65755ccf81
1 измененных файлов с 26 добавлено и 18 удалено
  1. 26 18
      src/migrations/20190624110950-fill-last-update-user.js

+ 26 - 18
src/migrations/20190624110950-fill-last-update-user.js

@@ -4,32 +4,40 @@ const logger = require('@alias/logger')('growi:migrate:abolish-page-group-relati
 const mongoose = require('mongoose');
 const config = require('@root/config/migrate');
 
+/**
+ * FIX https://github.com/weseek/growi/issues/1067
+ */
 module.exports = {
 
   async up(db) {
     logger.info('Apply migration');
     mongoose.connect(config.mongoUri, config.mongodb.options);
 
-    /*
-    await db.collection('pages').aggregate([
-      {
-        $match: {
-          $or: [
-            { lastUpdateUser: { $exists: false } },
-            { lastUpdateUser: { $eq: null } },
-          ],
-        },
-      },
-      {
-        $addFields: {
-          lastUpdateUser: '$creator',
+    const Page = require('@server/models/page')();
+
+    // see https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field/37280419#37280419
+
+    // retrieve target data
+    const pages = await Page.find({
+      $or: [
+        { lastUpdateUser: { $exists: false } },
+        { lastUpdateUser: { $eq: null } },
+      ],
+    }).select('_id creator');
+
+    // create requests for bulkWrite
+    const requests = pages.map((page) => {
+      return {
+        updateOne: {
+          filter: { _id: page._id },
+          update: { $set: { lastUpdateUser: page.creator } },
         },
-      },
-    ])
-      .out('pages')
-      .toArray();
+      };
+    });
 
-    */
+    if (requests.length > 0) {
+      await db.collection('pages').bulkWrite(requests);
+    }
 
     logger.info('Migration has successfully applied');
   },