Compares the indexes defined in MongoEngine with the ones existing in the database. Returns any missing/extra indexes.
(cls)
| 1016 | |
| 1017 | @classmethod |
| 1018 | def compare_indexes(cls): |
| 1019 | """Compares the indexes defined in MongoEngine with the ones |
| 1020 | existing in the database. Returns any missing/extra indexes. |
| 1021 | """ |
| 1022 | |
| 1023 | required = cls.list_indexes() |
| 1024 | |
| 1025 | existing = [] |
| 1026 | collection = cls._get_collection() |
| 1027 | for info in collection.index_information().values(): |
| 1028 | if "_fts" in info["key"][0]: |
| 1029 | # Useful for text indexes (but not only) |
| 1030 | index_type = info["key"][0][1] |
| 1031 | text_index_fields = info.get("weights").keys() |
| 1032 | existing.append([(key, index_type) for key in text_index_fields]) |
| 1033 | else: |
| 1034 | existing.append(info["key"]) |
| 1035 | missing = [index for index in required if index not in existing] |
| 1036 | extra = [index for index in existing if index not in required] |
| 1037 | |
| 1038 | # if { _cls: 1 } is missing, make sure it's *really* necessary |
| 1039 | if [("_cls", 1)] in missing: |
| 1040 | cls_obsolete = False |
| 1041 | for index in existing: |
| 1042 | if includes_cls(index) and index not in extra: |
| 1043 | cls_obsolete = True |
| 1044 | break |
| 1045 | if cls_obsolete: |
| 1046 | missing.remove([("_cls", 1)]) |
| 1047 | |
| 1048 | return {"missing": missing, "extra": extra} |
| 1049 | |
| 1050 | |
| 1051 | class DynamicDocument(Document, metaclass=TopLevelDocumentMetaclass): |