From d52e6fc21eb209ecf74025b68a9ed5dd3fadb647 Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 24 Feb 2021 10:42:27 +0100 Subject: [PATCH] fix settings delete bug --- meilisearch-http/src/data/updates.rs | 5 +++-- .../index_controller/local_index_controller/mod.rs | 11 ++++++++--- meilisearch-http/src/index_controller/mod.rs | 5 +++-- meilisearch-http/src/routes/settings/mod.rs | 8 ++++---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/meilisearch-http/src/data/updates.rs b/meilisearch-http/src/data/updates.rs index fbb9be801..c9c8b2c41 100644 --- a/meilisearch-http/src/data/updates.rs +++ b/meilisearch-http/src/data/updates.rs @@ -55,10 +55,11 @@ impl Data { pub async fn update_settings( &self, index: impl AsRef + Send + Sync + 'static, - settings: Settings + settings: Settings, + create: bool, ) -> anyhow::Result { let index_controller = self.index_controller.clone(); - let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings)).await??; + let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings, create)).await??; Ok(update.into()) } diff --git a/meilisearch-http/src/index_controller/local_index_controller/mod.rs b/meilisearch-http/src/index_controller/local_index_controller/mod.rs index 14efe42c7..7669bbcba 100644 --- a/meilisearch-http/src/index_controller/local_index_controller/mod.rs +++ b/meilisearch-http/src/index_controller/local_index_controller/mod.rs @@ -5,7 +5,7 @@ mod update_handler; use std::path::Path; use std::sync::Arc; -use anyhow::{bail, Context}; +use anyhow::{bail, Context, anyhow}; use itertools::Itertools; use milli::Index; @@ -51,9 +51,14 @@ impl IndexController for LocalIndexController { fn update_settings>( &self, index: S, - settings: super::Settings + settings: super::Settings, + create: bool, ) -> anyhow::Result> { - let (_, update_store) = self.indexes.get_or_create_index(&index, self.update_db_size, self.index_db_size)?; + let (_, update_store) = if create { + self.indexes.get_or_create_index(&index, self.update_db_size, self.index_db_size)? + } else { + self.indexes.index(&index)?.ok_or_else(|| anyhow!("Index {:?} doesn't exist", index.as_ref()))? + }; let meta = UpdateMeta::Settings(settings); let pending = update_store.register_update(meta, &[])?; Ok(pending.into()) diff --git a/meilisearch-http/src/index_controller/mod.rs b/meilisearch-http/src/index_controller/mod.rs index b20e43749..2d23ebadf 100644 --- a/meilisearch-http/src/index_controller/mod.rs +++ b/meilisearch-http/src/index_controller/mod.rs @@ -142,8 +142,9 @@ pub trait IndexController { fn delete_documents(&self, index: impl AsRef, document_ids: Vec) -> anyhow::Result; /// Updates an index settings. If the index does not exist, it will be created when the update - /// is applied to the index. - fn update_settings>(&self, index_uid: S, settings: Settings) -> anyhow::Result; + /// is applied to the index. `create` specifies whether an index should be created if not + /// existing. + fn update_settings>(&self, index_uid: S, settings: Settings, create: bool) -> anyhow::Result; /// Create an index with the given `index_uid`. fn create_index(&self, index_settings: IndexSettings) -> Result; diff --git a/meilisearch-http/src/routes/settings/mod.rs b/meilisearch-http/src/routes/settings/mod.rs index 00bc4220e..b65729b31 100644 --- a/meilisearch-http/src/routes/settings/mod.rs +++ b/meilisearch-http/src/routes/settings/mod.rs @@ -26,7 +26,7 @@ macro_rules! make_setting_route { $attr: Some(None), ..Default::default() }; - match data.update_settings(index_uid.into_inner(), settings).await { + match data.update_settings(index_uid.into_inner(), settings, false).await { Ok(update_status) => { let json = serde_json::to_string(&update_status).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -48,7 +48,7 @@ macro_rules! make_setting_route { ..Default::default() }; - match data.update_settings(index_uid.into_inner(), settings).await { + match data.update_settings(index_uid.into_inner(), settings, true).await { Ok(update_status) => { let json = serde_json::to_string(&update_status).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -137,7 +137,7 @@ async fn update_all( index_uid: web::Path, body: web::Json, ) -> Result { - match data.update_settings(index_uid.into_inner(), body.into_inner()).await { + match data.update_settings(index_uid.into_inner(), body.into_inner(), true).await { Ok(update_result) => { let json = serde_json::to_string(&update_result).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -170,7 +170,7 @@ async fn delete_all( index_uid: web::Path, ) -> Result { let settings = Settings::cleared(); - match data.update_settings(index_uid.into_inner(), settings).await { + match data.update_settings(index_uid.into_inner(), settings, false).await { Ok(update_result) => { let json = serde_json::to_string(&update_result).unwrap(); Ok(HttpResponse::Ok().body(json))