| 1 |
import asyncio |
| 2 |
from collections.abc import Callable |
| 3 |
from inspect import isclass, signature |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from beanie.migrations.controllers.base import BaseMigrationController |
| 7 |
from beanie.migrations.utils import update_dict |
| 8 |
from beanie.odm.documents import Document |
| 9 |
from beanie.odm.utils.pydantic import parse_model |
| 10 |
|
| 11 |
|
| 12 |
class DummyOutput: |
| 13 |
def __init__(self): |
| 14 |
super().__setattr__("_internal_structure_dict", {}) |
| 15 |
|
| 16 |
def __setattr__(self, key, value): |
| 17 |
self._internal_structure_dict[key] = value |
| 18 |
|
| 19 |
def __getattr__(self, item): |
| 20 |
try: |
| 21 |
return self._internal_structure_dict[item] |
| 22 |
except KeyError: |
| 23 |
self._internal_structure_dict[item] = DummyOutput() |
| 24 |
return self._internal_structure_dict[item] |
| 25 |
|
| 26 |
def dict(self, to_parse: "dict | DummyOutput | None" = None): |
| 27 |
if to_parse is None: |
| 28 |
to_parse = self |
| 29 |
input_dict = ( |
| 30 |
to_parse._internal_structure_dict |
| 31 |
if isinstance(to_parse, DummyOutput) |
| 32 |
else to_parse |
| 33 |
) |
| 34 |
result_dict = {} |
| 35 |
for key, value in input_dict.items(): |
| 36 |
if isinstance(value, (DummyOutput, dict)): |
| 37 |
result_dict[key] = self.dict(to_parse=value) |
| 38 |
else: |
| 39 |
result_dict[key] = value |
| 40 |
return result_dict |
| 41 |
|
| 42 |
|
| 43 |
def iterative_migration( |
| 44 |
document_models: list[type[Document]] | None = None, |
| 45 |
batch_size: int = 10000, |
| 46 |
): |
| 47 |
class IterativeMigration(BaseMigrationController): |
| 48 |
def __init__(self, function: Callable) -> None: |
| 49 |
self.function = function |
| 50 |
self.function_signature = signature(function) |
| 51 |
input_signature = self.function_signature.parameters.get( |
| 52 |
"input_document" |
| 53 |
) |
| 54 |
if input_signature is None: |
| 55 |
raise RuntimeError("input_signature must not be None") |
| 56 |
self.input_document_model: type[Document] = ( |
| 57 |
input_signature.annotation |
| 58 |
) |
| 59 |
output_signature = self.function_signature.parameters.get( |
| 60 |
"output_document" |
| 61 |
) |
| 62 |
if output_signature is None: |
| 63 |
raise RuntimeError("output_signature must not be None") |
| 64 |
self.output_document_model: type[Document] = ( |
| 65 |
output_signature.annotation |
| 66 |
) |
| 67 |
|
| 68 |
if ( |
| 69 |
not isclass(self.input_document_model) |
| 70 |
or not issubclass(self.input_document_model, Document) |
| 71 |
or not isclass(self.output_document_model) |
| 72 |
or not issubclass(self.output_document_model, Document) |
| 73 |
): |
| 74 |
raise TypeError( |
| 75 |
"input_document and output_document must have annotation of Document subclass" |
| 76 |
) |
| 77 |
|
| 78 |
self.batch_size = batch_size |
| 79 |
|
| 80 |
def __call__(self, *args: Any, **kwargs: Any): |
| 81 |
pass |
| 82 |
|
| 83 |
@property |
| 84 |
def models(self) -> list[type[Document]]: |
| 85 |
preset_models = document_models |
| 86 |
if preset_models is None: |
| 87 |
preset_models = [] |
| 88 |
return preset_models + [ |
| 89 |
self.input_document_model, |
| 90 |
self.output_document_model, |
| 91 |
] |
| 92 |
|
| 93 |
async def run(self, session): |
| 94 |
output_documents = [] |
| 95 |
all_migration_ops = [] |
| 96 |
async for input_document in self.input_document_model.find_all( |
| 97 |
session=session |
| 98 |
): |
| 99 |
output = DummyOutput() |
| 100 |
function_kwargs = { |
| 101 |
"input_document": input_document, |
| 102 |
"output_document": output, |
| 103 |
} |
| 104 |
if "self" in self.function_signature.parameters: |
| 105 |
function_kwargs["self"] = None |
| 106 |
await self.function(**function_kwargs) |
| 107 |
output_dict = input_document.model_dump() |
| 108 |
update_dict(output_dict, output.dict()) |
| 109 |
output_document = parse_model( |
| 110 |
self.output_document_model, output_dict |
| 111 |
) |
| 112 |
output_documents.append(output_document) |
| 113 |
|
| 114 |
if len(output_documents) == self.batch_size: |
| 115 |
all_migration_ops.append( |
| 116 |
self.output_document_model.replace_many( |
| 117 |
documents=output_documents, session=session |
| 118 |
) |
| 119 |
) |
| 120 |
output_documents = [] |
| 121 |
|
| 122 |
if output_documents: |
| 123 |
all_migration_ops.append( |
| 124 |
self.output_document_model.replace_many( |
| 125 |
documents=output_documents, session=session |
| 126 |
) |
| 127 |
) |
| 128 |
await asyncio.gather(*all_migration_ops) |
| 129 |
|
| 130 |
return IterativeMigration |