Skip to content

Commit

Permalink
Merge pull request #900 from elasticsales/original-upstream
Browse files Browse the repository at this point in the history
Don't send a "cls" option to ensureIndex
  • Loading branch information
MRigal committed May 6, 2015
2 parents d9c8285 + 9bdc320 commit f69ebbe
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
3 changes: 1 addition & 2 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -138,7 +138,6 @@ that much better:
* hellysmile
* Jaepil Jeong
* Daniil Sharou
* Stefan Wójcik
* Pete Campton
* Martyn Smith
* Marcelo Anton
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
================
Expand Down
12 changes: 12 additions & 0 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,25 @@ def ensure_indexes(cls):
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)

# If _cls is being used (for polymorphism), it needs an index,
# 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)

Expand Down
58 changes: 58 additions & 0 deletions tests/document/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,5 +826,63 @@ class BlogPost(Document):
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()

0 comments on commit f69ebbe

Please sign in to comment.