1use std::{str::FromStr, time::Duration};
2
3use miette::{IntoDiagnostic, Result};
4use mobc::{Connection, Pool};
5use mobc_postgres::{PgConnectionManager, tokio_postgres};
6use tokio_postgres::Config;
7
8pub type PgPool = Pool<PgConnectionManager<crate::tls::MakeRustlsConnectWrapper>>;
9pub type PgConnection = Connection<PgConnectionManager<crate::tls::MakeRustlsConnectWrapper>>;
10
11pub async fn create_pool(url: &str) -> Result<PgPool> {
13 let config = Config::from_str(url).into_diagnostic()?;
14 let tls_connector = crate::tls::make_tls_connector()?;
15 let manager = PgConnectionManager::new(config, tls_connector);
16
17 Ok(Pool::builder()
18 .max_open(10)
19 .max_idle(5)
20 .max_lifetime(Some(Duration::from_secs(3600)))
21 .build(manager))
22}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27
28 #[tokio::test]
29 async fn test_create_pool_valid_connection_string() {
30 let connection_string = "postgresql://localhost/test";
31 let result = create_pool(connection_string).await;
32 assert!(result.is_ok());
33 }
34
35 #[tokio::test]
36 async fn test_create_pool_with_full_url() {
37 let connection_string = "postgresql://user:pass@localhost:5432/testdb";
38 let result = create_pool(connection_string).await;
39 assert!(result.is_ok());
40 }
41
42 #[tokio::test]
43 async fn test_pool_can_be_cloned() {
44 let connection_string = "postgresql://localhost/test";
45 let pool = create_pool(connection_string).await.unwrap();
46 let pool_clone = pool.clone();
47
48 assert_eq!(
49 pool.state().await.max_open,
50 pool_clone.state().await.max_open
51 );
52 }
53
54 #[tokio::test]
55 async fn test_pool_configuration() {
56 let connection_string = "postgresql://localhost/test";
57 let pool = create_pool(connection_string).await.unwrap();
58 let state = pool.state().await;
59
60 assert_eq!(state.max_open, 10);
61 }
62}