From c776dbaca9820bcd5e0396fee540e485eb674ea2 Mon Sep 17 00:00:00 2001 From: NoodleSamaChan Date: Wed, 17 Apr 2024 17:09:30 +0200 Subject: [PATCH] created new snapshot method --- .code-samples.meilisearch.yaml | 5 ++ src/lib.rs | 2 + src/snapshots.rs | 121 +++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/snapshots.rs diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 6b401409..d403c95d 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -1626,3 +1626,8 @@ reset_proximity_precision_settings_1: |- .reset_proximity_precision() .await .unwrap(); +create_snapshot_1: |- + client + .create_snapshot() + .await + .unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 553c4760..73f943c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -231,6 +231,8 @@ pub mod client; pub mod documents; /// Module containing the [dumps] trait. pub mod dumps; +/// Module containing the [snapshots] trait. +pub mod snapshots; /// Module containing the [`errors::Error`] struct. pub mod errors; /// Module related to runtime and instance features. diff --git a/src/snapshots.rs b/src/snapshots.rs new file mode 100644 index 00000000..2e5308ec --- /dev/null +++ b/src/snapshots.rs @@ -0,0 +1,121 @@ +//! The `snapshots` module allows the creation of database snapshots. +//! +//! - snapshots are `.snapshots` files that can be used to launch Meilisearch. +//! +//! - snapshots are not compatible between Meilisearch versions. +//! +//! # Example +//! +//! ``` +//! # use meilisearch_sdk::{client::*, errors::*, snapshots::*, snapshots::*, task_info::*, tasks::*}; +//! # use futures_await_test::async_test; +//! # use std::{thread::sleep, time::Duration}; +//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { +//! # +//! # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); +//! # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); +//! # +//! # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap(); +//! +//! // Create a snapshot +//! let task_info = client.create_snapshot().await.unwrap(); +//! assert!(matches!( +//! task_info, +//! TaskInfo { +//! update_type: TaskType::SnapshotCreation { .. }, +//! .. +//! } +//! )); +//! # }); +//! ``` + +use crate::{client::Client, errors::Error, request::*, task_info::TaskInfo}; + +/// Snapshots related methods. +/// See the [snapshots](crate::snapshots) module. +impl Client { + /// Triggers a snapshots creation process. + /// + /// Once the process is complete, a snapshots is created in the [snapshots directory]. + /// If the snapshots directory does not exist yet, it will be created. + /// + /// # Example + /// + /// ``` + /// # use meilisearch_sdk::{client::*, errors::*, snapshots::*, snapshots::*, task_info::*, tasks::*}; + /// # use futures_await_test::async_test; + /// # use std::{thread::sleep, time::Duration}; + /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { + /// # + /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); + /// # + /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap(); + /// # + /// let task_info = client.create_snapshot().await.unwrap(); + /// + /// assert!(matches!( + /// task_info, + /// TaskInfo { + /// update_type: TaskType::SnapshotCreation { .. }, + /// .. + /// } + /// )); + /// # }); + /// ``` + pub async fn create_snapshot(&self) -> Result { + self.http_client + .request::<(), (), TaskInfo>( + &format!("{}/snapshots", self.host), + Method::Post { + query: (), + body: (), + }, + 202, + ) + .await + } +} + +/// Alias for [`create_snapshot`](Client::create_snapshot). +pub async fn create_snapshot(client: &Client) -> Result { + client.create_snapshot().await +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{client::*, tasks::*}; + use meilisearch_test_macro::meilisearch_test; + use std::time::Duration; + + #[meilisearch_test] + async fn test_snapshot_success_creation(client: Client) -> Result<(), Error> { + let task = client + .create_snapshot() + .await? + .wait_for_completion( + &client, + None, + None, + ) + .await?; + + assert!(matches!(task, Task::Succeeded { .. })); + Ok(()) + } + + #[meilisearch_test] + async fn test_snapshot_correct_update_type(client: Client) -> Result<(), Error> { + let task_info = client.create_snapshot().await.unwrap(); + + assert!(matches!( + task_info, + TaskInfo { + update_type: TaskType::SnapshotCreation { .. }, + .. + } + )); + Ok(()) + } +}