30 lines
1.1 KiB
Rust
30 lines
1.1 KiB
Rust
use anyhow::Context;
|
|
use diesel::r2d2::ConnectionManager;
|
|
|
|
pub type DbBackend = diesel::pg::Pg;
|
|
pub type DbConnection = diesel::PgConnection;
|
|
pub type DbPool = diesel::r2d2::Pool<
|
|
diesel::r2d2::ConnectionManager<DbConnection>
|
|
>;
|
|
|
|
pub fn connect(url: &str) -> anyhow::Result<DbPool> {
|
|
let config= erplt_config::Config::get();
|
|
let manager = ConnectionManager::<DbConnection>::new(url);
|
|
|
|
tracing::info!("Creating database pool at {}", config.database.connect_url_redacted());
|
|
|
|
let pool = DbPool::builder()
|
|
.max_size(config.database.max_pool_size)
|
|
.min_idle(Some(config.database.min_idle))
|
|
.connection_timeout(config.database.connection_timeout)
|
|
.idle_timeout(Some(config.database.idle_timeout))
|
|
.max_lifetime(Some(config.database.max_lifetime))
|
|
.test_on_check_out(config.database.test_on_check_out)
|
|
.build(manager)
|
|
.context("Failed to create database connection pool")?;
|
|
|
|
pool.get()
|
|
.context("Failed to connect to PostgreSQL")?;
|
|
tracing::info!("Connected to database");
|
|
Ok(pool)
|
|
} |