use crate::config::DatabaseConfig;
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use std::time::Duration;
pub type DbPool = PgPool;
pub async fn create_pool(config: &DatabaseConfig) -> Result<DbPool, sqlx::Error> {
let pool = PgPoolOptions::new()
.max_connections(config.max_connections)
.min_connections(config.min_connections)
.acquire_timeout(Duration::from_secs(config.acquire_timeout))
.connect_with(config.connect_options())
.await?;
tracing::info!(
host = %config.host,
port = %config.port,
database = %config.database,
max_connections = config.max_connections,
"Database connection pool created"
);
Ok(pool)
}
pub async fn health_check(pool: &DbPool) -> bool {
sqlx::query("SELECT 1").execute(pool).await.is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pool_type_alias() {
fn _assert_type(_: DbPool) {}
}
}