pg_embed/
pg_commands.rs

1//!
2//! Create postgres command executor
3//!
4//! Command executors for initdb, pg_ctl start, pg_ctl stop
5//!
6use std::path::PathBuf;
7
8use crate::command_executor::{AsyncCommand, AsyncCommandExecutor};
9use crate::pg_enums::{PgAuthMethod, PgProcessType, PgServerStatus};
10use crate::pg_errors::PgEmbedError;
11use crate::pg_types::PgResult;
12
13///
14/// Postgres command executors
15///
16pub struct PgCommand {}
17
18impl PgCommand {
19    ///
20    /// Create initdb command
21    ///
22    pub fn init_db_executor(
23        init_db_exe: &PathBuf,
24        database_dir: &PathBuf,
25        pw_file_path: &PathBuf,
26        user: &str,
27        auth_method: &PgAuthMethod,
28    ) -> PgResult<AsyncCommandExecutor<PgServerStatus, PgEmbedError, PgProcessType>> {
29        let init_db_executable = init_db_exe.as_os_str();
30        let password_file_arg = format!("--pwfile={}", pw_file_path.to_str().unwrap());
31        let auth_host = match auth_method {
32            PgAuthMethod::Plain => "password",
33            PgAuthMethod::MD5 => "md5",
34            PgAuthMethod::ScramSha256 => "scram-sha-256",
35        };
36        let args = [
37            "-A",
38            auth_host,
39            "-U",
40            user,
41            "-D",
42            database_dir.to_str().unwrap(),
43            &password_file_arg,
44        ];
45
46        let command_executor =
47            AsyncCommandExecutor::<PgServerStatus, PgEmbedError, PgProcessType>::new(
48                init_db_executable,
49                args,
50                PgProcessType::InitDb,
51            )?;
52
53        Ok(command_executor)
54    }
55
56    ///
57    /// Create pg_ctl start command
58    ///
59    pub fn start_db_executor(
60        pg_ctl_exe: &PathBuf,
61        database_dir: &PathBuf,
62        port: &u16,
63    ) -> PgResult<AsyncCommandExecutor<PgServerStatus, PgEmbedError, PgProcessType>> {
64        let pg_ctl_executable = pg_ctl_exe.as_os_str();
65        let port_arg = format!("-F -p {}", port.to_string());
66        let args = [
67            "-o",
68            &port_arg,
69            "start",
70            "-w",
71            "-D",
72            database_dir.to_str().unwrap(),
73        ];
74        let command_executor =
75            AsyncCommandExecutor::<PgServerStatus, PgEmbedError, PgProcessType>::new(
76                pg_ctl_executable,
77                args,
78                PgProcessType::StartDb,
79            )?;
80
81        Ok(command_executor)
82    }
83
84    ///
85    /// Create pg_ctl stop command
86    ///
87    pub fn stop_db_executor(
88        pg_ctl_exe: &PathBuf,
89        database_dir: &PathBuf,
90    ) -> PgResult<AsyncCommandExecutor<PgServerStatus, PgEmbedError, PgProcessType>> {
91        let pg_ctl_executable = pg_ctl_exe.as_os_str();
92        let args = ["stop", "-w", "-D", database_dir.to_str().unwrap()];
93        let command_executor =
94            AsyncCommandExecutor::<PgServerStatus, PgEmbedError, PgProcessType>::new(
95                pg_ctl_executable,
96                args,
97                PgProcessType::StopDb,
98            )?;
99
100        Ok(command_executor)
101    }
102}