use super::HistoryError;
use super::sql::{DDL, Dialect, Stmts, impl_sql_history};
use sqlx::postgres::PgPoolOptions;
use std::time::Duration;
impl_sql_history!(PostgresHistory, sqlx::PgPool);
impl PostgresHistory {
pub async fn connect(
url: &str,
idem_retention: Duration,
lease_ttl: Duration,
instance_id: String,
) -> Result<Self, HistoryError> {
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(url)
.await
.map_err(|e| HistoryError::Backend(format!("Postgres connection failed: {e}")))?;
for stmt in DDL {
sqlx::query(stmt)
.execute(&pool)
.await
.map_err(|e| HistoryError::Backend(format!("creating run-history schema: {e}")))?;
}
Ok(Self::from_parts(
pool,
idem_retention,
lease_ttl,
instance_id,
Stmts::new(Dialect::Postgres),
))
}
}