34 lines
907 B
Rust
34 lines
907 B
Rust
use anyhow::Context;
|
|
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
|
|
use tracing::log::info;
|
|
|
|
pub type ConnectionPool = Pool<Postgres>;
|
|
|
|
pub struct ConnectionPoolManager;
|
|
|
|
impl ConnectionPoolManager {
|
|
pub async fn new_pool(
|
|
connection_string: &str,
|
|
run_migrations: bool,
|
|
) -> anyhow::Result<ConnectionPool> {
|
|
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)
|
|
}
|
|
}
|