Skip to content

Commit

Permalink
Merge #268
Browse files Browse the repository at this point in the history
268: Adding delete if exists methods r=alallema a=sanders41

Relates to meilisearch/integration-guides#107

Co-authored-by: Paul Sanders <psanders1@gmail.com>
  • Loading branch information
bors[bot] and sanders41 authored Jun 7, 2021
2 parents defc5b8 + 80db3c0 commit 40f9a52
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
26 changes: 26 additions & 0 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> In
"""
return Index.create(self.config, uid, options)

def delete_index_if_exists(self, uid: str) -> bool:
"""Deletes an index if it already exists
Parameters
----------
uid:
UID of the index.
Returns
--------
Returns True if an index was deleted or False if not
Raises
------
MeiliSearchApiError
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
"""

try:
self.http.delete(f'{self.config.paths.index}/{uid}')
return True
except MeiliSearchApiError as error:
if error.error_code != "index_not_found":
raise error
return False

def get_indexes(self) -> List[Index]:
"""Get all indexes.
Expand Down
20 changes: 20 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from requests import Response

from meilisearch.errors import MeiliSearchApiError
from meilisearch._httprequests import HttpRequests
from meilisearch.config import Config
from meilisearch.errors import MeiliSearchTimeoutError
Expand Down Expand Up @@ -54,6 +55,25 @@ def delete(self) -> Response:

return self.http.delete(f'{self.config.paths.index}/{self.uid}')

def delete_if_exists(self) -> bool:
"""Deletes the index if it already exists
Returns
--------
Returns True if an index was deleted or False if not
Raises
MeiliSearchApiError
An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
"""
try:
self.delete()
return True
except MeiliSearchApiError as error:
if error.error_code != "index_not_found":
raise error
return False

def update(self, **body: Dict[str, Any]) -> 'Index':
"""Update the index primary-key.
Expand Down
34 changes: 33 additions & 1 deletion meilisearch/tests/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from datetime import datetime

import pytest
from meilisearch.client import Client
from meilisearch.errors import MeiliSearchApiError
from meilisearch.index import Index
from meilisearch.tests import common
from meilisearch.tests import BASE_URL, common, MASTER_KEY

def test_create_index(client):
"""Tests creating an index."""
Expand Down Expand Up @@ -184,3 +186,33 @@ def test_delete_index(client):
with pytest.raises(Exception):
client.get_index(uid=common.INDEX_UID3)
assert len(client.get_indexes()) == 0

@pytest.mark.usefixtures("indexes_sample")
def test_delete_if_exists(client):
assert client.get_index(uid=common.INDEX_UID)
deleted = Client(BASE_URL, MASTER_KEY).index(common.INDEX_UID).delete_if_exists()
assert deleted is True
with pytest.raises(MeiliSearchApiError):
client.get_index(uid=common.INDEX_UID)

def test_delete_if_exists_no_delete(client):
with pytest.raises(MeiliSearchApiError):
client.get_index(uid="none")

deleted = Client(BASE_URL, MASTER_KEY).index("none").delete_if_exists()
assert deleted is False

@pytest.mark.usefixtures("indexes_sample")
def test_delete_index_if_exists(client):
assert client.get_index(uid=common.INDEX_UID)
deleted = client.delete_index_if_exists(common.INDEX_UID)
assert deleted is True
with pytest.raises(MeiliSearchApiError):
client.get_index(uid=common.INDEX_UID)

def test_delete_index_if_exists_no_delete(client):
with pytest.raises(MeiliSearchApiError):
client.get_index(uid="none")

deleted = client.delete_index_if_exists("none")
assert deleted is False

0 comments on commit 40f9a52

Please sign in to comment.