Skip to content

created new snapshot method - fixes #537 #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -244,6 +244,8 @@ pub mod request;
pub mod search;
/// Module containing [`Settings`].
pub mod settings;
/// Module containing the [snapshots] trait.
pub mod snapshots;
/// Module representing the [`TaskInfo`]s.
pub mod task_info;
/// Module representing the [`Task`]s.
Expand Down
117 changes: 117 additions & 0 deletions src/snapshots.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! 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(())
}
}
Loading