33
crates/como_infrastructure/src/database/mod.rs
Normal file
33
crates/como_infrastructure/src/database/mod.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user