@@ -943,7 +943,27 @@ def db(self):
943
943
return self .mongo .pymongo ().db
944
944
945
945
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 ):
947
967
""" Create a specific index composed of the `list_of_keys` for the
948
968
mongo collection behind the `resource` using the `app.config`
949
969
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):
965
985
{"sparse": True}
966
986
967
987
.. versionadded:: 0.6
988
+
968
989
"""
969
990
# it doesn't work as a typical mongodb method run in the request
970
991
# life cycle, it is just called when the app start and it uses
971
992
# pymongo directly.
972
993
collection = app .config ['SOURCES' ][resource ]['source' ]
973
994
974
995
# 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
+
976
1002
with app .app_context ():
977
1003
db = app .data .pymongo (resource , px ).db
978
1004
@@ -989,8 +1015,9 @@ def create_index(app, resource, name, list_of_keys, index_options):
989
1015
except pymongo .errors .OperationFailure as e :
990
1016
if e .code == 85 :
991
1017
# 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.
994
1021
995
1022
# by default, drop the old index with old configuration and
996
1023
# create the index again with the new configuration.
0 commit comments