Skip to content

Commit

Permalink
created new snapshot method
Browse files Browse the repository at this point in the history
  • Loading branch information
NoodleSamaChan committed Apr 17, 2024
1 parent 85ceec4 commit c776dba
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1626,3 +1626,8 @@ reset_proximity_precision_settings_1: |-
.reset_proximity_precision()
.await
.unwrap();
create_snapshot_1: |-
client
.create_snapshot()
.await
.unwrap();
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
121 changes: 121 additions & 0 deletions src/snapshots.rs
Original file line number Diff line number Diff line change
@@ -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<Http: HttpClient> Client<Http> {
/// 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<TaskInfo, Error> {
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<Http: HttpClient>(client: &Client<Http>) -> Result<TaskInfo, Error> {
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(())
}
}

0 comments on commit c776dba

Please sign in to comment.