From b40efd8eb8fa5e59216d34f6482e19906be7dc4b Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Thu, 31 Mar 2022 00:27:36 +0200 Subject: [PATCH] Re-create index when already exists #11666 #11750 --- docs/history.md | 16 ++++++++++++++++ packages/mongo/collection.js | 14 +++++++++++++- packages/mongo/package.js | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/history.md b/docs/history.md index 0f73ab033f..eaa4f946db 100644 --- a/docs/history.md +++ b/docs/history.md @@ -1,3 +1,19 @@ +## v.NEXT, 2022-04-XX + +#### Highlights + +#### Breaking Changes +N/A +#### Migration Steps + +#### Meteor Version Release + +* `mongo@1.15.0` + - If an index with the same name, but different options exists Meteor will re-create it instead of crashing. + - If there is an error Meteor will output a better message naming the collection and index where the error occured. + +#### Independent Releases + ## v2.7.1, 2022-03-31 #### Highlights diff --git a/packages/mongo/collection.js b/packages/mongo/collection.js index 5e4ad21578..1a1472e39f 100644 --- a/packages/mongo/collection.js +++ b/packages/mongo/collection.js @@ -757,7 +757,19 @@ Object.assign(Mongo.Collection.prototype, { var self = this; if (!self._collection.createIndex) throw new Error('Can only call createIndex on server collections'); - self._collection.createIndex(index, options); + try { + self._collection.createIndex(index, options); + } catch (e) { + if (e.message.includes('An equivalent index already exists with the same name but different options.')) { + import { Log } from 'meteor/logging'; + + Log.info(`Re-creating index ${index} for ${self._name} due to options mismatchs.`); + self._collection._dropIndex(index); + self._collection.createIndex(index, options); + } else { + throw new Meteor.Error(`An error occurred when creating an index for collection "${self._name}: ${e.message}`); + } + } }, _dropIndex(index) { diff --git a/packages/mongo/package.js b/packages/mongo/package.js index c062c4aebb..fc6703c9b4 100644 --- a/packages/mongo/package.js +++ b/packages/mongo/package.js @@ -9,7 +9,7 @@ Package.describe({ summary: "Adaptor for using MongoDB and Minimongo over DDP", - version: '1.14.6' + version: '1.15.0' }); Npm.depends({