Просмотр исходного кода

update dropIndexIfExists method

Yuki Takei 6 лет назад
Родитель
Сommit
22751565df
1 измененных файлов с 11 добавлено и 5 удалено
  1. 11 5
      src/migrations/20191102223901-drop-pages-indices.js

+ 11 - 5
src/migrations/20191102223901-drop-pages-indices.js

@@ -4,7 +4,14 @@ const logger = require('@alias/logger')('growi:migrate:drop-pages-indices');
 const mongoose = require('mongoose');
 const mongoose = require('mongoose');
 const config = require('@root/config/migrate');
 const config = require('@root/config/migrate');
 
 
-async function dropIndexIfExists(collection, indexName) {
+async function dropIndexIfExists(db, collectionName, indexName) {
+  // check existence of the collection
+  const items = await db.listCollections({ name: collectionName }, { nameOnly: true }).toArray();
+  if (items.length === 0) {
+    return;
+  }
+
+  const collection = await db.collection(collectionName);
   if (await collection.indexExists(indexName)) {
   if (await collection.indexExists(indexName)) {
     await collection.dropIndex(indexName);
     await collection.dropIndex(indexName);
   }
   }
@@ -15,10 +22,9 @@ module.exports = {
     logger.info('Apply migration');
     logger.info('Apply migration');
     mongoose.connect(config.mongoUri, config.mongodb.options);
     mongoose.connect(config.mongoUri, config.mongodb.options);
 
 
-    const collection = db.collection('pages');
-    await dropIndexIfExists(collection, 'lastUpdateUser_1');
-    await dropIndexIfExists(collection, 'liker_1');
-    await dropIndexIfExists(collection, 'seenUsers_1');
+    await dropIndexIfExists(db, 'pages', 'lastUpdateUser_1');
+    await dropIndexIfExists(db, 'pages', 'liker_1');
+    await dropIndexIfExists(db, 'pages', 'seenUsers_1');
 
 
     logger.info('Migration has successfully applied');
     logger.info('Migration has successfully applied');
   },
   },