|
|
@@ -4,7 +4,14 @@ const logger = require('@alias/logger')('growi:migrate:drop-wrong-index-of-page-
|
|
|
const mongoose = require('mongoose');
|
|
|
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)) {
|
|
|
await collection.dropIndex(indexName);
|
|
|
}
|
|
|
@@ -15,8 +22,7 @@ module.exports = {
|
|
|
logger.info('Apply migration');
|
|
|
mongoose.connect(config.mongoUri, config.mongodb.options);
|
|
|
|
|
|
- const collection = db.collection('pagetagrelations');
|
|
|
- await dropIndexIfExists(collection, 'page_1_user_1');
|
|
|
+ await dropIndexIfExists(db, 'pagetagrelations', 'page_1_user_1');
|
|
|
|
|
|
logger.info('Migration has successfully applied');
|
|
|
},
|