Skip to content

Commit

Permalink
db: Add oneoff_async_connection() fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Oct 4, 2024
1 parent 03c5016 commit dc60164
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::certs::CRUNCHY;
use diesel::{Connection, ConnectionResult, PgConnection, QueryResult};
use diesel_async::pooled_connection::deadpool::{Hook, HookError};
use diesel_async::pooled_connection::ManagerConfig;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
use native_tls::{Certificate, TlsConnector};
use postgres_native_tls::MakeTlsConnector;
use secrecy::ExposeSecret;
Expand All @@ -23,6 +23,18 @@ pub fn oneoff_connection() -> anyhow::Result<PgConnection> {
oneoff_connection_with_config(&config).map_err(Into::into)
}

pub async fn oneoff_async_connection_with_config(
config: &config::DatabasePools,
) -> ConnectionResult<AsyncPgConnection> {
let url = connection_url(config, config.primary.url.expose_secret());
AsyncPgConnection::establish(&url).await
}

pub async fn oneoff_async_connection() -> anyhow::Result<AsyncPgConnection> {
let config = config::DatabasePools::full_from_environment(&config::Base::from_environment()?)?;
Ok(oneoff_async_connection_with_config(&config).await?)
}

pub fn connection_url(config: &config::DatabasePools, url: &str) -> String {
let mut url = Url::parse(url).expect("Invalid database URL");

Expand Down

0 comments on commit dc60164

Please sign in to comment.