meilisearch_sdk/
snapshots.rs

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
32use crate::{client::Client, errors::Error, request::*, task_info::TaskInfo};
33
34/// Snapshots related methods.
35/// See the [snapshots](crate::snapshots) module.
36impl<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).
81pub async fn create_snapshot<Http: HttpClient>(client: &Client<Http>) -> Result<TaskInfo, Error> {
82    client.create_snapshot().await
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use crate::{client::*, tasks::*};
89    use meilisearch_test_macro::meilisearch_test;
90
91    #[meilisearch_test]
92    async fn test_snapshot_success_creation(client: Client) -> Result<(), Error> {
93        let task = client
94            .create_snapshot()
95            .await?
96            .wait_for_completion(&client, None, None)
97            .await?;
98
99        assert!(matches!(task, Task::Succeeded { .. }));
100        Ok(())
101    }
102
103    #[meilisearch_test]
104    async fn test_snapshot_correct_update_type(client: Client) -> Result<(), Error> {
105        let task_info = client.create_snapshot().await.unwrap();
106
107        assert!(matches!(
108            task_info,
109            TaskInfo {
110                update_type: TaskType::SnapshotCreation { .. },
111                ..
112            }
113        ));
114        Ok(())
115    }
116}