Taichi Masuyama 4 лет назад
Родитель
Сommit
fa362deb8b

+ 64 - 0
packages/app/src/migrations/20211227060705-revision-path-to-page-id-schema-migration.js

@@ -0,0 +1,64 @@
+import mongoose from 'mongoose';
+
+import { getModelSafely, getMongoUri, mongoOptions } from '@growi/core';
+import loggerFactory from '~/utils/logger';
+import getPageModel from '~/server/models/page';
+
+
+const logger = loggerFactory('growi:migrate:revision-path-to-page-id-schema-migration');
+
+module.exports = {
+  // path => pageId
+  async up(db, client) {
+    mongoose.connect(getMongoUri(), mongoOptions);
+    const Page = getModelSafely('Page') || getPageModel();
+    const Revision = getModelSafely('Revision');
+
+    const pages = await Page.find({ revision: { $ne: null } }, { _id: 1, revision: 1 });
+
+    // make map revisionId to pageId
+    const updateManyOperations = pages.map((page) => {
+      return {
+        updateMany: {
+          filter: { _id: page.revision },
+          update: {
+            $set: { pageId: page._id },
+            $unset: { path: null },
+          },
+        },
+      };
+    });
+
+    // updateMany by array
+    await Revision.bulkWrite(updateManyOperations);
+
+    logger.info('Migration has successfully applied');
+  },
+
+  // pageId => path
+  async down(db, client) {
+    mongoose.connect(getMongoUri(), mongoOptions);
+    const Page = getModelSafely('Page') || getPageModel();
+    const Revision = getModelSafely('Revision');
+
+    const pages = await Page.find({ revision: { $ne: null } }, { _id: 1, revision: 1 });
+
+    // make map revisionId to pageId
+    const updateManyOperations = pages.map((page) => {
+      return {
+        updateMany: {
+          filter: { _id: page.revision },
+          update: {
+            $set: { path: page.path },
+            $unset: { pageId: null },
+          },
+        },
+      };
+    });
+
+    // updateMany by array
+    await Revision.bulkWrite(updateManyOperations);
+
+    logger.info('Migration down has successfully applied');
+  },
+};

+ 4 - 32
packages/app/src/server/models/revision.js

@@ -12,7 +12,8 @@ module.exports = function(crowi) {
 
   const ObjectId = mongoose.Schema.Types.ObjectId;
   const revisionSchema = new mongoose.Schema({
-    path: { type: String, required: true, index: true },
+    // OBSOLETE path: { type: String, required: true, index: true }
+    pageId: { type: ObjectId, required: true, index: true },
     body: {
       type: String,
       required: true,
@@ -29,25 +30,10 @@ module.exports = function(crowi) {
   });
   revisionSchema.plugin(mongoosePaginate);
 
-  revisionSchema.statics.findRevisionIdList = function(path) {
-    return this.find({ path })
-      .select('_id author createdAt hasDiffToPrev')
-      .sort({ createdAt: -1 })
-      .exec();
-  };
-
-  revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
+  revisionSchema.statics.updateRevisionListByPath = async function(path, updateData) {
     const Revision = this;
 
-    return new Promise(((resolve, reject) => {
-      Revision.update({ path }, { $set: updateData }, { multi: true }, (err, data) => {
-        if (err) {
-          return reject(err);
-        }
-
-        return resolve(data);
-      });
-    }));
+    return Revision.updateMany({ path }, { $set: updateData });
   };
 
   revisionSchema.statics.prepareRevision = function(pageData, body, previousBody, user, options) {
@@ -76,19 +62,5 @@ module.exports = function(crowi) {
     return newRevision;
   };
 
-  revisionSchema.statics.removeRevisionsByPath = function(path) {
-    const Revision = this;
-
-    return new Promise(((resolve, reject) => {
-      Revision.remove({ path }, (err, data) => {
-        if (err) {
-          return reject(err);
-        }
-
-        return resolve(data);
-      });
-    }));
-  };
-
   return mongoose.model('Revision', revisionSchema);
 };