use anyhow::{Context, Result}; use sqlx::postgres::PgPoolOptions; use sqlx::PgPool; const DEFAULT_MAX_CONNECTIONS: u32 = 10; pub async fn create_pool(database_url: &str) -> Result { let max_connections = std::env::var("DATABASE_MAX_CONNECTIONS") .ok() .and_then(|s| s.parse::().ok()) .unwrap_or(DEFAULT_MAX_CONNECTIONS); let pool = PgPoolOptions::new() .max_connections(max_connections) .connect(database_url) .await .context("failed to connect to database")?; sqlx::migrate!() .run(&pool) .await .context("failed to run database migrations")?; tracing::info!(max_connections, "database connected and migrations applied"); Ok(pool) }