|
| 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