Merge branch 'dev-disable-indexing' of https://github.com/colinhowe/mongoengine

Conflicts:
	mongoengine/queryset.py
This commit is contained in:
Ross Lawley 2012-05-01 09:37:01 +01:00
commit 4ce1ba81a6
4 changed files with 88 additions and 51 deletions

View File

@ -4,6 +4,7 @@ Changelog
Changes in 0.6.X Changes in 0.6.X
================ ================
- Added meta `auto_create_index` so you can disable index creation
- Added write concern options to inserts - Added write concern options to inserts
- Fixed typo in meta for index options - Fixed typo in meta for index options
- Bug fix Read preference now passed correctly - Bug fix Read preference now passed correctly

View File

@ -74,6 +74,12 @@ class Document(BaseDocument):
names. Index direction may be specified by prefixing the field names with names. Index direction may be specified by prefixing the field names with
a **+** or **-** sign. a **+** or **-** sign.
Automatic index creation can be disabled by specifying
attr:`auto_create_index` in the :attr:`meta` dictionary. If this is set to
False 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 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 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 disabled by either setting types to False on the specific index or

View File

@ -481,21 +481,12 @@ class QuerySet(object):
"""Returns all documents.""" """Returns all documents."""
return self.__call__() return self.__call__()
@property def _ensure_indexes(self):
def _collection(self): """Checks the document meta data and ensures all the indexes exist.
"""Property that returns the collection object. This allows us to
perform operations only if the collection is accessed. .. note:: You can disable automatic index creation by setting
`auto_create_index` to False in the documents meta data
""" """
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)
background = self._document._meta.get('index_background', False) background = self._document._meta.get('index_background', False)
drop_dups = self._document._meta.get('index_drop_dups', False) drop_dups = self._document._meta.get('index_drop_dups', False)
index_opts = self._document._meta.get('index_opts', {}) index_opts = self._document._meta.get('index_opts', {})
@ -543,6 +534,23 @@ class QuerySet(object):
self._collection.ensure_index(index_spec, self._collection.ensure_index(index_spec,
background=background, **index_opts) 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('auto_create_index', True):
self._ensure_indexes()
return self._collection_obj return self._collection_obj
@property @property

View File

@ -741,6 +741,28 @@ class DocumentTest(unittest.TestCase):
self.assertEqual(info.keys(), ['_types_1_user_guid_1', '_id_', '_types_1_name_1']) self.assertEqual(info.keys(), ['_types_1_user_guid_1', '_id_', '_types_1_name_1'])
Person.drop_collection() Person.drop_collection()
def test_disable_index_creation(self):
"""Tests setting auto_create_index to False on the connection will
disable any index generation.
"""
class User(Document):
meta = {
'indexes': ['user_guid'],
'auto_create_index': 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): def test_embedded_document_index(self):
"""Tests settings an index on an embedded document """Tests settings an index on an embedded document
""" """