Skip to content

Commit 6d1526b

Browse files
committed
Merge branch 'ensure_mongo_indexes'
2 parents fd9b051 + 710c34f commit 6d1526b

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Development
88

99
Version 0.8
1010
~~~~~~~~~~~
11+
- New: Refactor index creation. We now have a new
12+
``eve.io.mongo.ensure_mongo_indexes()`` function which ensures that eventual
13+
``mongo_indexes`` defined for a resource are created on the active
14+
database. The function can be imported and invoked, for example in
15+
multi-db workflows where a db is activated based on the
16+
authenticated user performing the request (via custom auth classes).
1117
- Fix: add sphinxcontrib-embedly to dev-requirements.txt.
1218
- New: when the media endpoint is enabled, the default authentication class
1319
will be used to secure it. Closes #1083.

eve/flaskapp.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
error_endpoint, media_endpoint, schema_collection_endpoint, \
2525
schema_item_endpoint
2626
from eve.exceptions import ConfigException, SchemaException
27-
from eve.io.mongo import Mongo, Validator, GridFSMediaStorage, create_index
27+
from eve.io.mongo import Mongo, Validator, GridFSMediaStorage, \
28+
ensure_mongo_indexes
2829
from eve.logging import RequestFilter
2930
from eve.utils import api_prefix, extract_key_values
3031

@@ -894,16 +895,7 @@ def register_resource(self, resource, settings):
894895
)
895896

896897
# create the mongo db indexes
897-
mongo_indexes = self.config['DOMAIN'][resource]['mongo_indexes']
898-
if mongo_indexes:
899-
for name, value in mongo_indexes.items():
900-
if isinstance(value, tuple):
901-
list_of_keys, index_options = value
902-
else:
903-
list_of_keys = value
904-
index_options = {}
905-
906-
create_index(self, resource, name, list_of_keys, index_options)
898+
ensure_mongo_indexes(self, resource)
907899

908900
# flask-pymongo compatibility.
909901
if 'MONGO_OPTIONS' in self.config['DOMAIN']:

eve/io/mongo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"""
1212

1313
# flake8: noqa
14-
from eve.io.mongo.mongo import Mongo, MongoJSONEncoder, create_index
14+
from eve.io.mongo.mongo import Mongo, MongoJSONEncoder, ensure_mongo_indexes
1515
from eve.io.mongo.validation import Validator
1616
from eve.io.mongo.media import GridFSMediaStorage

eve/io/mongo/mongo.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,27 @@ def db(self):
943943
return self.mongo.pymongo().db
944944

945945

946-
def create_index(app, resource, name, list_of_keys, index_options):
946+
def ensure_mongo_indexes(app, resource):
947+
""" Make sure 'mongo_indexes' is respected and mongo indexes are created on
948+
the current database.
949+
950+
.. versionaddded:: 0.8
951+
"""
952+
mongo_indexes = app.config['DOMAIN'][resource]['mongo_indexes']
953+
if not mongo_indexes:
954+
return
955+
956+
for name, value in mongo_indexes.items():
957+
if isinstance(value, tuple):
958+
list_of_keys, index_options = value
959+
else:
960+
list_of_keys = value
961+
index_options = {}
962+
963+
_create_index(app, resource, name, list_of_keys, index_options)
964+
965+
966+
def _create_index(app, resource, name, list_of_keys, index_options):
947967
""" Create a specific index composed of the `list_of_keys` for the
948968
mongo collection behind the `resource` using the `app.config`
949969
to retrieve all data needed to find out the mongodb configuration.
@@ -965,14 +985,20 @@ def create_index(app, resource, name, list_of_keys, index_options):
965985
{"sparse": True}
966986
967987
.. versionadded:: 0.6
988+
968989
"""
969990
# it doesn't work as a typical mongodb method run in the request
970991
# life cycle, it is just called when the app start and it uses
971992
# pymongo directly.
972993
collection = app.config['SOURCES'][resource]['source']
973994

974995
# get db for given prefix
975-
px = app.config['DOMAIN'][resource].get('mongo_prefix', 'MONGO')
996+
try:
997+
# mongo_prefix might have been set by Auth class instance
998+
px = g.get('mongo_prefix')
999+
except:
1000+
px = app.config['DOMAIN'][resource].get('mongo_prefix', 'MONGO')
1001+
9761002
with app.app_context():
9771003
db = app.data.pymongo(resource, px).db
9781004

@@ -989,8 +1015,9 @@ def create_index(app, resource, name, list_of_keys, index_options):
9891015
except pymongo.errors.OperationFailure as e:
9901016
if e.code == 85:
9911017
# This error is raised when the definition of the index has
992-
# been changed, we didn't found any spec out there but we think
993-
# that this error is not going to change and we can trust.
1018+
# been changed, we didn't find any spec out there but we
1019+
# think that this error is not going to change and we can
1020+
# trust.
9941021

9951022
# by default, drop the old index with old configuration and
9961023
# create the index again with the new configuration.

0 commit comments

Comments
 (0)