mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Feature request: add public call to reset index cache

Open danaki opened this issue 13 years ago • 8 comments

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().

danaki avatar Feb 01 '12 15:02 danaki

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

rozza avatar Feb 01 '12 15:02 rozza

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.

danaki avatar Feb 01 '12 16:02 danaki

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 avatar Feb 01 '12 17:02 danaki

@danaki - does this work for you? https://github.com/hmarr/mongoengine/issues/419#issuecomment-3760555

rozza avatar Feb 02 '12 09:02 rozza

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..

rozza avatar Feb 02 '12 09:02 rozza

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.

danaki avatar Feb 02 '12 09:02 danaki

ok - what version where you running 0.5.2?

rozza avatar Feb 02 '12 09:02 rozza

Exactly 0.5.2

danaki avatar Feb 02 '12 10:02 danaki