Updated index creation allows kwargs to be passed through refs (MongoEngine/mongoengine#104)
This commit is contained in:
		| @@ -4,6 +4,7 @@ Changelog | |||||||
|  |  | ||||||
| Changes in 0.7.X | Changes in 0.7.X | ||||||
| ================= | ================= | ||||||
|  | - Updated index creation allows kwargs to be passed through refs (MongoEngine/mongoengine#104) | ||||||
| - Fixed Q object merge edge case (MongoEngine/mongoengine#109) | - Fixed Q object merge edge case (MongoEngine/mongoengine#109) | ||||||
| - Fixed reloading on sharded documents (hmarr/mongoengine#569) | - Fixed reloading on sharded documents (hmarr/mongoengine#569) | ||||||
| - Added NotUniqueError for duplicate keys (MongoEngine/mongoengine#62) | - Added NotUniqueError for duplicate keys (MongoEngine/mongoengine#62) | ||||||
|   | |||||||
| @@ -398,12 +398,12 @@ class QuerySet(object): | |||||||
|             or a **-** to determine the index ordering |             or a **-** to determine the index ordering | ||||||
|         """ |         """ | ||||||
|         index_spec = QuerySet._build_index_spec(self._document, key_or_list) |         index_spec = QuerySet._build_index_spec(self._document, key_or_list) | ||||||
|         self._collection.ensure_index( |         fields = index_spec.pop('fields') | ||||||
|             index_spec['fields'], |         index_spec['drop_dups'] = drop_dups | ||||||
|             drop_dups=drop_dups, |         index_spec['background'] = background | ||||||
|             background=background, |         index_spec.update(kwargs) | ||||||
|             sparse=index_spec.get('sparse', False), |  | ||||||
|             unique=index_spec.get('unique', False)) |         self._collection.ensure_index(fields, **index_spec) | ||||||
|         return self |         return self | ||||||
|  |  | ||||||
|     def __call__(self, q_obj=None, class_check=True, slave_okay=False, **query): |     def __call__(self, q_obj=None, class_check=True, slave_okay=False, **query): | ||||||
| @@ -473,11 +473,11 @@ class QuerySet(object): | |||||||
|         # Ensure document-defined indexes are created |         # Ensure document-defined indexes are created | ||||||
|         if self._document._meta['index_specs']: |         if self._document._meta['index_specs']: | ||||||
|             for spec in self._document._meta['index_specs']: |             for spec in self._document._meta['index_specs']: | ||||||
|                 types_indexed = types_indexed or includes_types(spec['fields']) |                 fields = spec.pop('fields') | ||||||
|  |                 types_indexed = types_indexed or includes_types(fields) | ||||||
|                 opts = index_opts.copy() |                 opts = index_opts.copy() | ||||||
|                 opts['unique'] = spec.get('unique', False) |                 opts.update(spec) | ||||||
|                 opts['sparse'] = spec.get('sparse', False) |                 self._collection.ensure_index(fields, | ||||||
|                 self._collection.ensure_index(spec['fields'], |  | ||||||
|                     background=background, **opts) |                     background=background, **opts) | ||||||
|  |  | ||||||
|         # If _types is being used (for polymorphism), it needs an index, |         # If _types is being used (for polymorphism), it needs an index, | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ from tests.fixtures import Base, Mixin, PickleEmbedded, PickleTest | |||||||
| from mongoengine import * | from mongoengine import * | ||||||
| from mongoengine.base import NotRegistered, InvalidDocumentError | from mongoengine.base import NotRegistered, InvalidDocumentError | ||||||
| from mongoengine.queryset import InvalidQueryError | from mongoengine.queryset import InvalidQueryError | ||||||
| from mongoengine.connection import get_db | from mongoengine.connection import get_db, get_connection | ||||||
|  |  | ||||||
| TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__), 'mongoengine.png') | TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__), 'mongoengine.png') | ||||||
|  |  | ||||||
| @@ -1102,6 +1102,32 @@ class DocumentTest(unittest.TestCase): | |||||||
|  |  | ||||||
|         BlogPost.drop_collection() |         BlogPost.drop_collection() | ||||||
|  |  | ||||||
|  |     def test_ttl_indexes(self): | ||||||
|  |  | ||||||
|  |         class Log(Document): | ||||||
|  |             created = DateTimeField(default=datetime.now) | ||||||
|  |             meta = { | ||||||
|  |                 'indexes': [ | ||||||
|  |                     {'fields': ['created'], 'expireAfterSeconds': 3600} | ||||||
|  |                 ] | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |         Log.drop_collection() | ||||||
|  |  | ||||||
|  |         if pymongo.version_tuple[0] < 2 and pymongo.version_tuple[1] < 3: | ||||||
|  |             raise SkipTest('pymongo needs to be 2.3 or higher for this test') | ||||||
|  |  | ||||||
|  |         connection = get_connection() | ||||||
|  |         version_array = connection.server_info()['versionArray'] | ||||||
|  |         if version_array[0] < 2 and version_array[1] < 2: | ||||||
|  |             raise SkipTest('MongoDB needs to be 2.2 or higher for this test') | ||||||
|  |  | ||||||
|  |         # Indexes are lazy so use list() to perform query | ||||||
|  |         list(Log.objects) | ||||||
|  |         info = Log.objects._collection.index_information() | ||||||
|  |         self.assertEqual(3600, | ||||||
|  |                 info['_types_1_created_1']['expireAfterSeconds']) | ||||||
|  |  | ||||||
|     def test_unique_and_indexes(self): |     def test_unique_and_indexes(self): | ||||||
|         """Ensure that 'unique' constraints aren't overridden by |         """Ensure that 'unique' constraints aren't overridden by | ||||||
|         meta.indexes. |         meta.indexes. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user