Ejemplo n.º 1
0
    def init_indexes(self, ignore_duplicate_keys=False):
        for resource, resource_config in self.config["DOMAIN"].items():
            mongo_indexes = resource_config.get("mongo_indexes__init")
            if not mongo_indexes:
                continue

            # Borrowed https://github.com/pyeve/eve/blob/22ea4bfebc8b633251cd06837893ff699bd07a00/eve/flaskapp.py#L915
            for name, value in mongo_indexes.items():
                if isinstance(value, tuple):
                    list_of_keys, index_options = value
                else:
                    list_of_keys = value
                    index_options = {}

                # index creation in background
                index_options.setdefault("background", True)

                try:
                    create_index(self, resource, name, list_of_keys,
                                 index_options)
                except DuplicateKeyError as err:
                    # Duplicate key for unique indexes are generally caused by invalid documents in the collection
                    # such as multiple documents not having a value for the attribute used for the index
                    # Log the error so it can be diagnosed and fixed
                    logger.exception(err)

                    if not ignore_duplicate_keys:
                        raise
 def forwards(self, mongodb_collection, mongodb_database):
     for audit in mongodb_collection.find({'resource': {'$in': PurgeAudit.item_resources}}):
         audit_id = get_resource_service(self.resource)._extract_doc_id(audit.get('extra'))
         print(mongodb_collection.update({'_id': audit.get(config.ID_FIELD)},
                                         {'$set': {
                                             'audit_id': audit_id
                                         }}))
     try:
         create_index(app=app, resource=self.resource, name='audit_id', list_of_keys=[('audit_id', 1)],
                      index_options={'background': True})
     except:
         print('create index failed')
Ejemplo n.º 3
0
    def init_indexes(self):
        for resource, resource_config in self.config['DOMAIN'].items():
            mongo_indexes = resource_config.get('mongo_indexes__init')
            if not mongo_indexes:
                continue

            # Borrowed https://github.com/pyeve/eve/blob/22ea4bfebc8b633251cd06837893ff699bd07a00/eve/flaskapp.py#L915
            for name, value in mongo_indexes.items():
                if isinstance(value, tuple):
                    list_of_keys, index_options = value
                else:
                    list_of_keys = value
                    index_options = {}

                # index creation in background
                index_options['background'] = True
                create_index(self, resource, name, list_of_keys, index_options)
Ejemplo n.º 4
0
 def forwards(self, mongodb_collection, mongodb_database):
     for audit in mongodb_collection.find(
         {'resource': {
             '$in': PurgeAudit.item_resources
         }}):
         audit_id = get_resource_service(self.resource)._extract_doc_id(
             audit.get('extra'))
         print(
             mongodb_collection.update({'_id': audit.get(config.ID_FIELD)},
                                       {'$set': {
                                           'audit_id': audit_id
                                       }}))
     try:
         create_index(app=app,
                      resource=self.resource,
                      name='audit_id',
                      list_of_keys=[('audit_id', 1)],
                      index_options={'background': True})
     except Exception:
         print('create index failed')