yusuketk 6 лет назад
Родитель
Сommit
4db322a516

+ 40 - 0
src/migrations/20200414164011-add-image-attachment-path-to-user.js

@@ -0,0 +1,40 @@
+require('module-alias/register');
+const logger = require('@alias/logger')('growi:migrate:add-image-attachment-parh-to-user');
+
+const mongoose = require('mongoose');
+const config = require('@root/config/migrate');
+
+module.exports = {
+
+  async up(db) {
+    logger.info('Apply migration');
+    mongoose.connect(config.mongoUri, config.mongodb.options);
+    const User = require('@server/models/user')();
+    require('@server/models/attachment')();
+
+    const users = await User.find({ imageAttachment: { $exists: true } })
+      .populate({
+        path: 'imageAttachment',
+      })
+      .select('imageAttachment');
+
+    const requests = users.filter(user => user.imageAttachment).map((user) => {
+      return {
+        updateOne: {
+          filter: { _id: user._id },
+          update: { $set: { imageAttachmentPath: user.imageAttachment.filePathProxied } },
+        },
+      };
+    });
+
+    if (requests.length > 0) {
+      await db.collection('users').bulkWrite(requests);
+    }
+
+    logger.info('Migration has successfully applied');
+  },
+
+  down(db) {
+    db.collection('users').update({}, { $unset: 'imageAttachmentPath' });
+  },
+};

+ 1 - 0
src/server/models/user.js

@@ -38,6 +38,7 @@ module.exports = function(crowi) {
     userId: String,
     image: String,
     imageAttachment: { type: ObjectId, ref: 'Attachment' },
+    imageAttachmentPath: String,
     isGravatarEnabled: { type: Boolean, default: false },
     isEmailPublished: { type: Boolean, default: true },
     googleId: String,