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

+ 3 - 2
packages/app/src/server/models/page.js

@@ -42,7 +42,7 @@ const pageSchema = new mongoose.Schema({
   },
   isEmpty: { type: Boolean, default: false },
   path: {
-    type: String, required: true, index: true, unique: true,
+    type: String, required: true,
   },
   revision: { type: ObjectId, ref: 'Revision' },
   redirectTo: { type: String, index: true },
@@ -71,8 +71,9 @@ const pageSchema = new mongoose.Schema({
 pageSchema.plugin(mongoosePaginate);
 pageSchema.plugin(uniqueValidator);
 
+// TODO: test this after modifying Page.create
 // ensure v5 clean install compatibility
-pageSchema.index({ path: 1 }, { partialFilterExpression: { parent: null } });
+pageSchema.index({ path: 1 }, { unique: true, partialFilterExpression: { parent: null } });
 
 /**
  * return an array of ancestors paths that is extracted from specified pagePath

+ 6 - 5
packages/app/src/server/routes/apiv3/pages.js

@@ -687,14 +687,16 @@ module.exports = (crowi) => {
   // TODO: use socket conn to show progress
   router.post('/v5-schema-migration', accessTokenParser, loginRequired, adminRequired, csrf, validator.v5PageMigration, apiV3FormValidator, async(req, res) => {
     const { action } = req.body;
+    const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
 
     switch (action) {
       case 'upgrade':
-
         try {
-          const Page = crowi.model('Page');
-          // not await
-          crowi.pageService.v5RecursiveMigration(Page.GRANT_PUBLIC);
+          if (!isV5Compatible) {
+            const Page = crowi.model('Page');
+            // not await
+            crowi.pageService.v5RecursiveMigration(Page.GRANT_PUBLIC);
+          }
         }
         catch (err) {
           logger.error('Error\n', err);
@@ -707,7 +709,6 @@ module.exports = (crowi) => {
         return res.apiv3Err(new ErrorV3('This action is not supported.', 'not_supported'), 400);
     }
 
-    const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
     return res.apiv3({ isV5Compatible });
   });
 

+ 21 - 4
packages/app/src/server/service/page.js

@@ -855,15 +855,32 @@ class PageService {
       return this.v5RecursiveMigration(grant, rootPath);
     }
 
+    /*
+     * After completed migration
+     */
+    if (grant !== Page.GRANT_PUBLIC) {
+      return;
+    }
+
+    const collection = mongoose.connection.collection('pages');
     try {
       // drop pages.path_1 indexes
-      const conn = mongoose.connection.collection('pages');
-      await conn.dropIndexes('path_1');
-      // then create non-unique indexes
+      await collection.dropIndex('path_1');
+      logger.info('Succeeded to drop unique indexes from pages.path.');
+    }
+    catch (err) {
+      // return not to set app:isV5Compatible to true
+      return logger.error('Failed to drop unique indexes from pages.path.', err);
+    }
+
+    try {
+      // create indexes without
+      await collection.createIndex({ path: 1 }, { unique: false });
+      logger.info('Succeeded to create non-unique indexes on pages.path.');
     }
     catch (err) {
       // return not to set app:isV5Compatible to true
-      return logger.error('Failed to drop unique indexes.', err);
+      return logger.error('Failed to create non-unique indexes on pages.path.', err);
     }
 
     try {