Skip to content

Commit

Permalink
feat(skip_index): possibility added to skip index actions (#942)
Browse files Browse the repository at this point in the history
* feat(skip_index): possibility added to skip index actions

* test: ✅ test init skip_indexes

* chore: satisfy pre-commit

* fix: collection not being created

---------

Co-authored-by: adeelsohailahmed <[email protected]>
  • Loading branch information
jorma16 and adeelsohailahmed authored Dec 4, 2024
1 parent b4244ff commit 75db4a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion beanie/odm/utils/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]] = []
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
"""

Expand All @@ -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,
)
21 changes: 21 additions & 0 deletions tests/odm/documents/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 75db4a4

Please sign in to comment.