diff --git a/AUTHORS b/AUTHORS index af87d503..9d105a60 100644 --- a/AUTHORS +++ b/AUTHORS @@ -119,7 +119,7 @@ that much better: * Anton Kolechkin * Sergey Nikitin * psychogenic - * Stefan Wójcik + * Stefan Wójcik (https://github.com/wojcikstefan) * dimonb * Garry Polley * James Slagle @@ -138,7 +138,6 @@ that much better: * hellysmile * Jaepil Jeong * Daniil Sharou - * Stefan Wójcik * Pete Campton * Martyn Smith * Marcelo Anton diff --git a/docs/changelog.rst b/docs/changelog.rst index 2a6f7802..f4b5ec6c 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -14,6 +14,7 @@ Changes in 0.9.X - DEV - Fixed unpickled documents replacing the global field's list. #888 - Fixed storage of microseconds in ComplexDateTimeField and unused separator option. #910 - Django support was removed and will be available as a separate extension. #958 +- Don't send a "cls" option to ensureIndex (related to https://jira.mongodb.org/browse/SERVER-769) Changes in 0.9.0 ================ diff --git a/mongoengine/document.py b/mongoengine/document.py index f8275021..01083d24 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -677,6 +677,12 @@ class Document(BaseDocument): cls_indexed = cls_indexed or includes_cls(fields) opts = index_opts.copy() opts.update(spec) + + # we shouldn't pass 'cls' to the collection.ensureIndex options + # because of https://jira.mongodb.org/browse/SERVER-769 + if 'cls' in opts: + del opts['cls'] + collection.ensure_index(fields, background=background, drop_dups=drop_dups, **opts) @@ -684,6 +690,12 @@ class Document(BaseDocument): # only if another index doesn't begin with _cls if (index_cls and not cls_indexed and cls._meta.get('allow_inheritance', ALLOW_INHERITANCE) is True): + + # we shouldn't pass 'cls' to the collection.ensureIndex options + # because of https://jira.mongodb.org/browse/SERVER-769 + if 'cls' in index_opts: + del index_opts['cls'] + collection.ensure_index('_cls', background=background, **index_opts) diff --git a/tests/document/indexes.py b/tests/document/indexes.py index ab156f68..6256cde3 100644 --- a/tests/document/indexes.py +++ b/tests/document/indexes.py @@ -826,5 +826,63 @@ class IndexesTest(unittest.TestCase): post2 = BlogPost(title='test2', slug='test') self.assertRaises(NotUniqueError, post2.save) + def test_index_dont_send_cls_option(self): + """ + Ensure that 'cls' option is not sent through ensureIndex. We shouldn't + send internal MongoEngine arguments that are not a part of the index + spec. + + This is directly related to the fact that MongoDB doesn't validate the + options that are passed to ensureIndex. For more details, see: + https://jira.mongodb.org/browse/SERVER-769 + """ + class TestDoc(Document): + txt = StringField() + + meta = { + 'allow_inheritance': True, + 'indexes': [ + { 'fields': ('txt',), 'cls': False } + ] + } + + class TestChildDoc(TestDoc): + txt2 = StringField() + + meta = { + 'indexes': [ + { 'fields': ('txt2',), 'cls': False } + ] + } + + TestDoc.drop_collection() + TestDoc.ensure_indexes() + TestChildDoc.ensure_indexes() + + index_info = TestDoc._get_collection().index_information() + for key in index_info: + del index_info[key]['v'] # drop the index version - we don't care about that here + + self.assertEqual(index_info, { + 'txt_1': { + 'key': [('txt', 1)], + 'dropDups': False, + 'background': False + }, + '_id_': { + 'key': [('_id', 1)], + }, + 'txt2_1': { + 'key': [('txt2', 1)], + 'dropDups': False, + 'background': False + }, + '_cls_1': { + 'key': [('_cls', 1)], + 'background': False, + } + }) + + if __name__ == '__main__': unittest.main()