use r2d2::Pool;
use r2d2_postgres::PostgresConnectionManager;
use postgres::NoTls;
#[derive(Debug, thiserror::Error)]
pub enum PostgresStoreError {
#[error("postgres driver error: {0}")]
Postgres(#[from] postgres::Error),
#[error("r2d2 pool error: {0}")]
Pool(#[from] r2d2::Error),
#[error("refinery migration error: {0}")]
Migration(#[from] refinery::Error),
#[error("domain mapping error: {0}")]
Mapping(String),
}
pub struct PostgresPersistenceStore {
pub(crate) pool: Pool<PostgresConnectionManager<NoTls>>,
}
impl PostgresPersistenceStore {
pub fn new(connection_string: &str) -> Result<Self, PostgresStoreError> {
let mut mig_client = postgres::Client::connect(connection_string, NoTls)?;
crate::migrations::runner().run(&mut mig_client)?;
drop(mig_client);
let manager = PostgresConnectionManager::new(
connection_string.parse()?,
NoTls,
);
let pool = r2d2::Pool::builder()
.max_size(20)
.connection_timeout(std::time::Duration::from_secs(5))
.build(manager)?;
Ok(Self { pool })
}
}