Taichi Masuyama 4 лет назад
Родитель
Сommit
7caa84fa8c
2 измененных файлов с 33 добавлено и 18 удалено
  1. 6 0
      packages/app/src/server/service/config-loader.ts
  2. 27 18
      packages/app/src/server/service/page.js

+ 6 - 0
packages/app/src/server/service/config-loader.ts

@@ -172,6 +172,12 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
     type:    ValueType.BOOLEAN,
     type:    ValueType.BOOLEAN,
     default: false,
     default: false,
   },
   },
+  V5_PATH_INDEX_STATUS: {
+    ns:      'crowi',
+    key:     'app:v5PathIndexStatus',
+    type:    ValueType.STRING,
+    default: 'processable',
+  },
   IS_V5_COMPATIBLE: {
   IS_V5_COMPATIBLE: {
     ns:      'crowi',
     ns:      'crowi',
     key:     'app:isV5Compatible',
     key:     'app:isV5Compatible',

+ 27 - 18
packages/app/src/server/service/page.js

@@ -865,26 +865,37 @@ class PageService {
     }
     }
   }
   }
 
 
+  async _setV5PathIndexStatus(status) {
+    try {
+      await this.crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
+        'app:v5PathIndexStatus': status,
+      });
+    }
+    catch (err) {
+      logger.error(`Failed to update app:v5PathIndexStatus to ${status} .`);
+      throw err;
+    }
+  }
+
   async v5InitialMigration(grant) {
   async v5InitialMigration(grant) {
     // const socket = this.crowi.socketIoService.getAdminSocket();
     // const socket = this.crowi.socketIoService.getAdminSocket();
     const Page = this.crowi.model('Page');
     const Page = this.crowi.model('Page');
-    const indexStatus = await Page.aggregate([{ $indexStats: {} }]);
-    const pathIndexStatus = indexStatus.filter(status => status.name === 'path_1')?.[0];
-    const isPathIndexExists = pathIndexStatus != null;
-    const isUnique = isPathIndexExists && pathIndexStatus.spec?.unique === true;
+    const status = this.crowi.configManager.getConfig('crowi', 'app:v5PathIndexStatus');
 
 
     // drop unique index first
     // drop unique index first
-    if (isUnique || !isPathIndexExists) {
+    if (status === 'processable') {
+      await this._setV5PathIndexStatus('processing');
       try {
       try {
-        await this._v5NormalizeIndex(isPathIndexExists);
+        await this._v5NormalizeIndex();
       }
       }
       catch (err) {
       catch (err) {
         logger.error('V5 index normalization failed.', err);
         logger.error('V5 index normalization failed.', err);
         // socket.emit('v5IndexNormalizationFailed', { error: err.message });
         // socket.emit('v5IndexNormalizationFailed', { error: err.message });
-
+        await this._setV5PathIndexStatus('processable');
         throw err;
         throw err;
       }
       }
     }
     }
+    await this._setV5PathIndexStatus('done');
 
 
     // then migrate
     // then migrate
     try {
     try {
@@ -1078,19 +1089,17 @@ class PageService {
 
 
   }
   }
 
 
-  async _v5NormalizeIndex(isPathIndexExists) {
+  async _v5NormalizeIndex() {
     const collection = mongoose.connection.collection('pages');
     const collection = mongoose.connection.collection('pages');
 
 
-    if (isPathIndexExists) {
-      try {
-        // drop pages.path_1 indexes
-        await collection.dropIndex('path_1');
-        logger.info('Succeeded to drop unique indexes from pages.path.');
-      }
-      catch (err) {
-        logger.warn('Failed to drop unique indexes from pages.path.', err);
-        throw err;
-      }
+    try {
+      // drop pages.path_1 indexes
+      await collection.dropIndex('path_1');
+      logger.info('Succeeded to drop unique indexes from pages.path.');
+    }
+    catch (err) {
+      logger.warn('Failed to drop unique indexes from pages.path.', err);
+      // DO NOT throw err
     }
     }
 
 
     try {
     try {