# Path: virtool/analyses/migrate.py
# async def nest_results(db):
#     """
#     Move the ``subtracted_count`` and ``read_count`` fields from the document to the ``results`` subdocument.
# 
#     This supports the new jobs API model where only a ``results`` field can be set on the analysis document by a
#     workflow job.
# 
#     :param db: the application database object
# 
#     """
#     async with buffered_bulk_writer(db.analyses) as writer:
#         async for document in db.analyses.find({"results.hits": {"$exists": False}}):
#             _id = document["_id"]
# 
#             results = {
#                 "hits": document["results"],
#             }
# 
#             if document["workflow"] == "pathoscope_bowtie":
#                 await writer.add(
#                     UpdateOne(
#                         {"_id": document["_id"]},
#                         {
#                             "$set": {
#                                 "results": {
#                                     **results,
#                                     "read_count": document["read_count"],
#                                     "subtracted_count": document["subtracted_count"],
#                                 }
#                             },
#                             "$unset": {"read_count": "", "subtracted_count": ""},
#                         },
#                     )
#                 )
# 
#             elif document["workflow"] == "nuvs":
#                 await writer.add(
#                     UpdateOne({"_id": document["_id"]}, {"$set": {"results": results}})
#                 )
# 
#             elif document["workflow"] == "aodp":
#                 await writer.add(
#                     UpdateOne(
#                         {"_id": document["_id"]},
#                         {
#                             "$set": {
#                                 "results": {
#                                     **results,
#                                     "join_histogram": document["join_histogram"],
#                                     "joined_pair_count": document["joined_pair_count"],
#                                     "remainder_pair_count": document[
#                                         "remainder_pair_count"
#                                     ],
#                                 }
#                             },
#                             "$unset": {
#                                 "join_histogram": "",
#                                 "joined_pair_count": "",
#                                 "remainder_pair_count": "",
#                             },
#                         },
#                     )
#                 )


from virtool.analyses.migrate import nest_results
                "results": {
                    "hits": [1, 2, 3, 4, 5],
                    "read_count": 1209,
                    "subtracted_count": 231,
                },
                "workflow": "pathoscope_bowtie",
            },
            {
                "_id": "bar",
                "read_count": 7982,
                "results": [9, 8, 7, 6, 5],
                "subtracted_count": 112,
                "workflow": "pathoscope_bowtie",
            },
            {
                "_id": "baz",
                "results": [9, 8, 7, 6, 5],
                "workflow": "nuvs",
            },
            {
                "_id": "bad",
                "join_histogram": [1, 2, 3, 4, 5],
                "joined_pair_count": 12345,
                "remainder_pair_count": 54321,
                "results": [9, 8, 7, 6, 5],
                "workflow": "aodp",
            },
        ]
    )


# Path: virtool/analyses/migrate.py
# async def nest_results(db):
#     """
#     Move the ``subtracted_count`` and ``read_count`` fields from the document to the ``results`` subdocument.
# 
#     This supports the new jobs API model where only a ``results`` field can be set on the analysis document by a
#     workflow job.
# 
#     :param db: the application database object
# 
#     """
#     async with buffered_bulk_writer(db.analyses) as writer:
#         async for document in db.analyses.find({"results.hits": {"$exists": False}}):
#             _id = document["_id"]
# 
#             results = {
#                 "hits": document["results"],
#             }
# 
#             if document["workflow"] == "pathoscope_bowtie":
#                 await writer.add(
#                     UpdateOne(
#                         {"_id": document["_id"]},
#                         {
#                             "$set": {
#                                 "results": {
#                                     **results,
#                                     "read_count": document["read_count"],
#                                     "subtracted_count": document["subtracted_count"],
#                                 }
#                             },
#                             "$unset": {"read_count": "", "subtracted_count": ""},
#                         },
#                     )
#                 )
# 
#             elif document["workflow"] == "nuvs":
#                 await writer.add(
#                     UpdateOne({"_id": document["_id"]}, {"$set": {"results": results}})
#                 )
# 
#             elif document["workflow"] == "aodp":
#                 await writer.add(
#                     UpdateOne(
#                         {"_id": document["_id"]},
#                         {
#                             "$set": {
#                                 "results": {
#                                     **results,
#                                     "join_histogram": document["join_histogram"],
#                                     "joined_pair_count": document["joined_pair_count"],
#                                     "remainder_pair_count": document[
#                                         "remainder_pair_count"
#                                     ],
#                                 }
#                             },
#                             "$unset": {
#                                 "join_histogram": "",
#                                 "joined_pair_count": "",
#                                 "remainder_pair_count": "",
#                             },
#                         },
#                     )
#                 )


# Path: tests/analyses/test_migrate.py
from virtool.analyses.migrate import nest_results

                "results": {
                    "hits": [1, 2, 3, 4, 5],
                    "read_count": 1209,
                    "subtracted_count": 231,
                },
                "workflow": "pathoscope_bowtie",
            },
            {
                "_id": "bar",
                "read_count": 7982,
                "results": [9, 8, 7, 6, 5],
                "subtracted_count": 112,
                "workflow": "pathoscope_bowtie",
            },
            {
                "_id": "baz",
                "results": [9, 8, 7, 6, 5],
                "workflow": "nuvs",
            },
            {
                "_id": "bad",
                "join_histogram": [1, 2, 3, 4, 5],
                "joined_pair_count": 12345,
                "remainder_pair_count": 54321,
                "results": [9, 8, 7, 6, 5],
                "workflow": "aodp",
            },
        ]
    )


    await nest_results(dbi)