def add_upload_id_field_to_items(db, storage_client):
    # prepare data for migration
    uploads = upload.find(db, with_default_projection=False)
    upload_id_filename_pairs = []
    for upload_doc in uploads:
        upload_id = upload_doc['_id']
        upload_duplicates = upload_doc.get_dict().get('duplicate_filenames',
                                                      [])
        filenames = upload_package_to_item_filenames(storage_client, upload_id)
        deduped_filenames = list(set(filenames) - set(upload_duplicates))
        upload_id_filename_pairs += list(
            product([upload_id], deduped_filenames))

    # build queries
    bulks = []
    for upload_id, filename in upload_id_filename_pairs:
        bulk = UpdateOne({'filename': filename},
                         {'$set': {
                             'upload_id': upload_id
                         }})
        bulks.append(bulk)

    # set new collection validators
    override_collection_validator(db, 'items', ITEMS_DB_SCHEMA)

    if bulks:
        # update DB
        result = db.items.bulk_write(bulks)
        print(result.bulk_api_result)
def add_tags_field_to_items(db):
    uploads = upload.find(db, with_default_projection=False)

    # build query
    bulks = []
    for upload_doc in uploads:
        upload_id = upload_doc['_id']
        upload_tags = upload_doc['tags']

        bulk = UpdateMany({'upload_id': ObjectId(upload_id)},
                          {'$set': {
                              'tags': upload_tags
                          }})
        bulks.append(bulk)

    # set new collection validators
    override_collection_validator(db, 'items', ITEMS_DB_SCHEMA)

    if bulks:
        # update DB
        result = db.items.bulk_write(bulks)
        print(result.bulk_api_result)
Ejemplo n.º 3
0
 def get(self):
     db = app.config['db']
     docs = upload.find(db)
     return [doc.serializable(shallow=True) for doc in docs][::-1]