Skip to content

Commit ab88f11

Browse files
Merge #572
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]>
2 parents 85ceec4 + c776dba commit ab88f11

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,3 +1626,8 @@ reset_proximity_precision_settings_1: |-
16261626
.reset_proximity_precision()
16271627
.await
16281628
.unwrap();
1629+
create_snapshot_1: |-
1630+
client
1631+
.create_snapshot()
1632+
.await
1633+
.unwrap();

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ pub mod client;
231231
pub mod documents;
232232
/// Module containing the [dumps] trait.
233233
pub mod dumps;
234+
/// Module containing the [snapshots] trait.
235+
pub mod snapshots;
234236
/// Module containing the [`errors::Error`] struct.
235237
pub mod errors;
236238
/// Module related to runtime and instance features.

src/snapshots.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//! The `snapshots` module allows the creation of database snapshots.
2+
//!
3+
//! - snapshots are `.snapshots` files that can be used to launch Meilisearch.
4+
//!
5+
//! - snapshots are not compatible between Meilisearch versions.
6+
//!
7+
//! # Example
8+
//!
9+
//! ```
10+
//! # use meilisearch_sdk::{client::*, errors::*, snapshots::*, snapshots::*, task_info::*, tasks::*};
11+
//! # use futures_await_test::async_test;
12+
//! # use std::{thread::sleep, time::Duration};
13+
//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
14+
//! #
15+
//! # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
16+
//! # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
17+
//! #
18+
//! # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
19+
//!
20+
//! // Create a snapshot
21+
//! let task_info = client.create_snapshot().await.unwrap();
22+
//! assert!(matches!(
23+
//! task_info,
24+
//! TaskInfo {
25+
//! update_type: TaskType::SnapshotCreation { .. },
26+
//! ..
27+
//! }
28+
//! ));
29+
//! # });
30+
//! ```
31+
32+
use crate::{client::Client, errors::Error, request::*, task_info::TaskInfo};
33+
34+
/// Snapshots related methods.
35+
/// See the [snapshots](crate::snapshots) module.
36+
impl<Http: HttpClient> Client<Http> {
37+
/// Triggers a snapshots creation process.
38+
///
39+
/// Once the process is complete, a snapshots is created in the [snapshots directory].
40+
/// If the snapshots directory does not exist yet, it will be created.
41+
///
42+
/// # Example
43+
///
44+
/// ```
45+
/// # use meilisearch_sdk::{client::*, errors::*, snapshots::*, snapshots::*, task_info::*, tasks::*};
46+
/// # use futures_await_test::async_test;
47+
/// # use std::{thread::sleep, time::Duration};
48+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
49+
/// #
50+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
51+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
52+
/// #
53+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
54+
/// #
55+
/// let task_info = client.create_snapshot().await.unwrap();
56+
///
57+
/// assert!(matches!(
58+
/// task_info,
59+
/// TaskInfo {
60+
/// update_type: TaskType::SnapshotCreation { .. },
61+
/// ..
62+
/// }
63+
/// ));
64+
/// # });
65+
/// ```
66+
pub async fn create_snapshot(&self) -> Result<TaskInfo, Error> {
67+
self.http_client
68+
.request::<(), (), TaskInfo>(
69+
&format!("{}/snapshots", self.host),
70+
Method::Post {
71+
query: (),
72+
body: (),
73+
},
74+
202,
75+
)
76+
.await
77+
}
78+
}
79+
80+
/// Alias for [`create_snapshot`](Client::create_snapshot).
81+
pub async fn create_snapshot<Http: HttpClient>(client: &Client<Http>) -> Result<TaskInfo, Error> {
82+
client.create_snapshot().await
83+
}
84+
85+
#[cfg(test)]
86+
mod tests {
87+
use super::*;
88+
use crate::{client::*, tasks::*};
89+
use meilisearch_test_macro::meilisearch_test;
90+
use std::time::Duration;
91+
92+
#[meilisearch_test]
93+
async fn test_snapshot_success_creation(client: Client) -> Result<(), Error> {
94+
let task = client
95+
.create_snapshot()
96+
.await?
97+
.wait_for_completion(
98+
&client,
99+
None,
100+
None,
101+
)
102+
.await?;
103+
104+
assert!(matches!(task, Task::Succeeded { .. }));
105+
Ok(())
106+
}
107+
108+
#[meilisearch_test]
109+
async fn test_snapshot_correct_update_type(client: Client) -> Result<(), Error> {
110+
let task_info = client.create_snapshot().await.unwrap();
111+
112+
assert!(matches!(
113+
task_info,
114+
TaskInfo {
115+
update_type: TaskType::SnapshotCreation { .. },
116+
..
117+
}
118+
));
119+
Ok(())
120+
}
121+
}

0 commit comments

Comments
 (0)