Add new meta option to Document: allow_index_creation.
Defaults to True. If set to False then MongoEngine will not ensure indexes exist
This commit is contained in:
parent
61411bb259
commit
7e376b40bb
@ -74,6 +74,11 @@ class Document(BaseDocument):
|
||||
names. Index direction may be specified by prefixing the field names with
|
||||
a **+** or **-** sign.
|
||||
|
||||
Index creation can be disabled by specifying :attr:`index_allow_creation` in
|
||||
the :attr:`meta` dictionary. If this is set to True then indexes will not be
|
||||
created by MongoEngine. This is useful in production systems where index
|
||||
creation is performed as part of a deployment system.
|
||||
|
||||
By default, _types will be added to the start of every index (that
|
||||
doesn't contain a list) if allow_inheritence is True. This can be
|
||||
disabled by either setting types to False on the specific index or
|
||||
|
@ -481,21 +481,7 @@ class QuerySet(object):
|
||||
"""Returns all documents."""
|
||||
return self.__call__()
|
||||
|
||||
@property
|
||||
def _collection(self):
|
||||
"""Property that returns the collection object. This allows us to
|
||||
perform operations only if the collection is accessed.
|
||||
"""
|
||||
if self._document not in QuerySet.__already_indexed:
|
||||
|
||||
# Ensure collection exists
|
||||
db = self._document._get_db()
|
||||
if self._collection_obj.name not in db.collection_names():
|
||||
self._document._collection = None
|
||||
self._collection_obj = self._document._get_collection()
|
||||
|
||||
QuerySet.__already_indexed.add(self._document)
|
||||
|
||||
def _ensure_indexes_exist(self):
|
||||
background = self._document._meta.get('index_background', False)
|
||||
drop_dups = self._document._meta.get('index_drop_dups', False)
|
||||
index_opts = self._document._meta.get('index_options', {})
|
||||
@ -543,6 +529,23 @@ class QuerySet(object):
|
||||
self._collection.ensure_index(index_spec,
|
||||
background=background, **index_opts)
|
||||
|
||||
@property
|
||||
def _collection(self):
|
||||
"""Property that returns the collection object. This allows us to
|
||||
perform operations only if the collection is accessed.
|
||||
"""
|
||||
if self._document not in QuerySet.__already_indexed:
|
||||
# Ensure collection exists
|
||||
db = self._document._get_db()
|
||||
if self._collection_obj.name not in db.collection_names():
|
||||
self._document._collection = None
|
||||
self._collection_obj = self._document._get_collection()
|
||||
|
||||
QuerySet.__already_indexed.add(self._document)
|
||||
|
||||
if self._document._meta.get('index_allow_creation', True):
|
||||
self._ensure_indexes_exist()
|
||||
|
||||
return self._collection_obj
|
||||
|
||||
@property
|
||||
|
@ -740,6 +740,28 @@ class DocumentTest(unittest.TestCase):
|
||||
self.assertEqual(info.keys(), ['_types_1_user_guid_1', '_id_', '_types_1_name_1'])
|
||||
Person.drop_collection()
|
||||
|
||||
def test_disable_index_creation(self):
|
||||
"""Tests setting allow_index_creation to False on the connection will
|
||||
disable any index generation.
|
||||
"""
|
||||
class User(Document):
|
||||
meta = {
|
||||
'indexes': ['user_guid'],
|
||||
'index_allow_creation': False
|
||||
}
|
||||
user_guid = StringField(required=True)
|
||||
|
||||
|
||||
User.drop_collection()
|
||||
|
||||
u = User(user_guid='123')
|
||||
u.save()
|
||||
|
||||
self.assertEquals(1, User.objects.count())
|
||||
info = User.objects._collection.index_information()
|
||||
self.assertEqual(info.keys(), ['_id_'])
|
||||
User.drop_collection()
|
||||
|
||||
def test_embedded_document_index(self):
|
||||
"""Tests settings an index on an embedded document
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user