pgdrift_db/
connection.rs

1use sqlx::postgres::{PgPool, PgPoolOptions};
2use std::time::Duration;
3
4#[derive(Debug, Clone)]
5pub struct ConnectionPool {
6    pool: PgPool,
7}
8
9impl ConnectionPool {
10    /// Create a new connection pool from a database URL
11    pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
12        let pool = PgPoolOptions::new()
13            .max_connections(5)
14            .acquire_timeout(Duration::from_secs(30))
15            .connect(database_url)
16            .await?;
17
18        Ok(Self { pool })
19    }
20
21    /// Get a reference to the underlying PgPool
22    pub fn pool(&self) -> &PgPool {
23        &self.pool
24    }
25
26    /// Test the database connection by executing a simple query
27    pub async fn test_connection(&self) -> Result<(), sqlx::Error> {
28        sqlx::query("SELECT 1").fetch_one(&self.pool).await?;
29
30        Ok(())
31    }
32}