From 75db4a4cd5ebc50042984ead3ce8a1f1bdafe14d Mon Sep 17 00:00:00 2001 From: jmarchan <8100110+jorma16@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:47:08 +0100 Subject: [PATCH] feat(skip_index): possibility added to skip index actions (#942) * feat(skip_index): possibility added to skip index actions * test: :white_check_mark: test init skip_indexes * chore: satisfy pre-commit * fix: collection not being created --------- Co-authored-by: adeelsohailahmed <37113831+adeelsohailahmed@users.noreply.github.com> --- beanie/odm/utils/init.py | 10 +++++++++- tests/odm/documents/test_init.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/beanie/odm/utils/init.py b/beanie/odm/utils/init.py index 212d6072..3ba8b456 100644 --- a/beanie/odm/utils/init.py +++ b/beanie/odm/utils/init.py @@ -69,6 +69,7 @@ def __init__( allow_index_dropping: bool = False, recreate_views: bool = False, multiprocessing_mode: bool = False, + skip_indexes: bool = False, ): """ Beanie initializer @@ -82,11 +83,13 @@ def __init__( :param recreate_views: bool - if views should be recreated. Default False :param multiprocessing_mode: bool - if multiprocessing mode is on it will patch the motor client to use process's event loop. + :param skip_indexes: bool - if you want to skip working with indexes. Default False :return: None """ self.inited_classes: List[Type] = [] self.allow_index_dropping = allow_index_dropping + self.skip_indexes = skip_indexes self.recreate_views = recreate_views self.models_with_updated_forward_refs: List[Type[BaseModel]] = [] @@ -606,7 +609,8 @@ async def init_document(self, cls: Type[Document]) -> Optional[Output]: cls._inheritance_inited = True await self.init_document_collection(cls) - await self.init_indexes(cls, self.allow_index_dropping) + if not self.skip_indexes: + await self.init_indexes(cls, self.allow_index_dropping) self.init_document_fields(cls) self.init_cache(cls) self.init_actions(cls) @@ -763,6 +767,7 @@ async def init_beanie( allow_index_dropping: bool = False, recreate_views: bool = False, multiprocessing_mode: bool = False, + skip_indexes: bool = False, ): """ Beanie initialization @@ -776,6 +781,8 @@ async def init_beanie( :param recreate_views: bool - if views should be recreated. Default False :param multiprocessing_mode: bool - if multiprocessing mode is on it will patch the motor client to use process's event loop. Default False + :param skip_indexes: bool - if you want to skip working with the indexes. + Default False :return: None """ @@ -786,4 +793,5 @@ async def init_beanie( allow_index_dropping=allow_index_dropping, recreate_views=recreate_views, multiprocessing_mode=multiprocessing_mode, + skip_indexes=skip_indexes, ) diff --git a/tests/odm/documents/test_init.py b/tests/odm/documents/test_init.py index b87a62fd..e06972e5 100644 --- a/tests/odm/documents/test_init.py +++ b/tests/odm/documents/test_init.py @@ -379,3 +379,24 @@ class Settings(Sample1.Settings): assert Sample2.get_settings().bson_encoders != {} assert Sample2.get_settings().name == "sample2" + + +async def test_init_beanie_with_skip_indexes(db): + class NewDocument(Document): + test_str: str + + class Settings: + indexes = ["test_str"] + + await init_beanie( + database=db, + document_models=[NewDocument], + skip_indexes=True, + ) + + # To force collection creation + await NewDocument(test_str="Roman Right").save() + + collection: AsyncIOMotorCollection = NewDocument.get_motor_collection() + index_info = await collection.index_information() + assert len(index_info) == 1 # Only the default _id index should be present