meilisearch_sdk/
dumps.rs

1//! The `dumps` module allows the creation of database dumps.
2//!
3//! - Dumps are `.dump` files that can be used to launch Meilisearch.
4//!
5//! - Dumps are compatible between Meilisearch versions.
6//!
7//! - Creating a dump is also referred to as exporting it, whereas launching Meilisearch with a dump is referred to as importing it.
8//!
9//! - During a [dump export](crate::client::Client::create_dump), all [indexes](crate::indexes::Index) of the current instance are exported—together with their documents and settings—and saved as a single `.dump` file.
10//!
11//! - During a dump import, all indexes contained in the indicated `.dump` file are imported along with their associated documents and [settings](crate::settings::Settings).
12//! Any existing [index](crate::indexes::Index) with the same uid as an index in the dump file will be overwritten.
13//!
14//! - Dump imports are [performed at launch](https://www.meilisearch.com/docs/learn/configuration/instance_options#import-dump) using an option.
15//!
16//! # Example
17//!
18//! ```
19//! # use meilisearch_sdk::{client::*, errors::*, dumps::*, dumps::*, task_info::*, tasks::*};
20//! # use futures_await_test::async_test;
21//! # use std::{thread::sleep, time::Duration};
22//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
23//! #
24//! # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
25//! # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
26//! #
27//! # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
28//!
29//! // Create a dump
30//! let task_info = client.create_dump().await.unwrap();
31//! assert!(matches!(
32//!     task_info,
33//!     TaskInfo {
34//!         update_type: TaskType::DumpCreation { .. },
35//!         ..
36//!     }
37//! ));
38//! # });
39//! ```
40
41use crate::{client::Client, errors::Error, request::*, task_info::TaskInfo};
42
43/// Dump related methods.
44/// See the [dumps](crate::dumps) module.
45impl<Http: HttpClient> Client<Http> {
46    /// Triggers a dump creation process.
47    ///
48    /// Once the process is complete, a dump is created in the [dumps directory](https://www.meilisearch.com/docs/learn/configuration/instance_options#dump-directory).
49    /// If the dumps directory does not exist yet, it will be created.
50    ///
51    /// # Example
52    ///
53    /// ```
54    /// # use meilisearch_sdk::{client::*, errors::*, dumps::*, dumps::*, task_info::*, tasks::*};
55    /// # use futures_await_test::async_test;
56    /// # use std::{thread::sleep, time::Duration};
57    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
58    /// #
59    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
60    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
61    /// #
62    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
63    /// #
64    /// let task_info = client.create_dump().await.unwrap();
65    ///
66    /// assert!(matches!(
67    ///     task_info,
68    ///     TaskInfo {
69    ///         update_type: TaskType::DumpCreation { .. },
70    ///         ..
71    ///     }
72    /// ));
73    /// # });
74    /// ```
75    pub async fn create_dump(&self) -> Result<TaskInfo, Error> {
76        self.http_client
77            .request::<(), (), TaskInfo>(
78                &format!("{}/dumps", self.host),
79                Method::Post {
80                    query: (),
81                    body: (),
82                },
83                202,
84            )
85            .await
86    }
87}
88
89/// Alias for [`create_dump`](Client::create_dump).
90pub async fn create_dump<Http: HttpClient>(client: &Client<Http>) -> Result<TaskInfo, Error> {
91    client.create_dump().await
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use crate::{client::*, tasks::*};
98    use meilisearch_test_macro::meilisearch_test;
99    use std::time::Duration;
100
101    #[meilisearch_test]
102    async fn test_dumps_success_creation(client: Client) -> Result<(), Error> {
103        let task = client
104            .create_dump()
105            .await?
106            .wait_for_completion(
107                &client,
108                Some(Duration::from_millis(1)),
109                Some(Duration::from_millis(6000)),
110            )
111            .await?;
112
113        assert!(matches!(task, Task::Succeeded { .. }));
114        Ok(())
115    }
116
117    #[meilisearch_test]
118    async fn test_dumps_correct_update_type(client: Client) -> Result<(), Error> {
119        let task_info = client.create_dump().await.unwrap();
120
121        assert!(matches!(
122            task_info,
123            TaskInfo {
124                update_type: TaskType::DumpCreation { .. },
125                ..
126            }
127        ));
128        Ok(())
129    }
130}