use anyhow::Context; use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; use tracing::log::info; pub type ConnectionPool = Pool; pub struct ConnectionPoolManager; impl ConnectionPoolManager { pub async fn new_pool( connection_string: &str, run_migrations: bool, ) -> anyhow::Result { info!("initializing the database connection pool"); let pool = PgPoolOptions::new() .max_connections(5) .connect(connection_string) .await .context("error while initializing the database connection pool")?; if run_migrations { info!("migrations enabled"); info!("migrating database"); sqlx::migrate!() .run(&pool) .await .context("error while running database migrations")?; } Ok(pool) } }