Feature request: add public call to reset index cache
In order to setup unit testing of mongoengine I've written a little test framework, I use the following function in teardown:
def clear_database(self):
connection = _get_connection()
db = connection[self.mongodb_config['database']]
cols = db.collection_names()
for col in cols:
if col not in ['system.indexes']:
db.drop_collection(col)
After that I've got an issue that tests order is significant. I've got issues similar to mentioned in #419, because after dropping collections unique/unique_with indices were not recreated. After hours of digging in mongoengine sources this line helped:
QuerySet._reset_already_indexed()
I think this is not obvious enough, I suggest reflect this in documentation or add drop_collection() to Document class which will reset all caches, or add this line to reload().
Yeah I had to do similar especially once pymongo's connection pool caching and mongoengine started to clash.
This has been fixed in dev : https://github.com/hmarr/mongoengine/blob/dev/mongoengine/document.py#L330-338 and will be in the 0.6 release - eta End of Feb
I'm not sure this is all, in my case I needed to add one more line: MyModel._collection = None only after that it raised exceptions on unique constraint violations.
Now it looks like that:
@classmethod
def clear_mongonengine_caches(cls):
QuerySet._reset_already_indexed()
import inspect
for name, obj in inspect.getmembers(sys.modules[cls.__module__], inspect.isclass):
from mongoengine.base import TopLevelDocumentMetaclass
if hasattr(obj, '__class__') and issubclass(obj.__class__, TopLevelDocumentMetaclass):
obj._collection = None
@danaki - does this work for you? https://github.com/hmarr/mongoengine/issues/419#issuecomment-3760555
btw this was my old teardown:
def teardown(self):
"""Clean all connections cached in the _document_registry."""
QuerySet._reset_already_indexed()
from mongoengine.base import _document_registry
for k, doc in _document_registry.items():
if not hasattr(doc, '_get_collection') or doc._meta.get('abstract', False):
continue
col = doc._get_collection()
col.database.connection.drop_database(col.database.name)
But I'm not sure its needed now..
I've checked out the "dev" branch and got so many new issues in existing code I had no time to fix. I'll send you my testcase classes by email.
ok - what version where you running 0.5.2?
Exactly 0.5.2