Skip to main content

agentics_persistence/db/
pool.rs

1//! Database pool creation and health checks.
2
3use secrecy::ExposeSecret;
4use sqlx::PgPool;
5
6use agentics_config::Config;
7use agentics_error::Result;
8
9/// Create a Postgres connection pool from application configuration.
10pub async fn create_pool(config: &Config, max_connections: u32) -> Result<PgPool> {
11    let pool = sqlx::postgres::PgPoolOptions::new()
12        .max_connections(max_connections)
13        .connect(config.database.url.expose_secret())
14        .await?;
15    Ok(pool)
16}
17
18/// Check database connectivity and return the server clock.
19pub async fn check_database(pool: &PgPool) -> Result<agentics_domain::models::DatabaseHealth> {
20    let row: (String,) = sqlx::query_as("SELECT NOW()::text").fetch_one(pool).await?;
21
22    Ok(agentics_domain::models::DatabaseHealth {
23        connected: true,
24        current_time: row.0,
25    })
26}