-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
572: created new snapshot method - fixes #537 r=irevoire a=NoodleSamaChan # Pull Request ## Related issue Fixes #537 ## What does this PR do? - creates new snapshot method ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: NoodleSamaChan <[email protected]>
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |