Skip to content

Commit dc60164

Browse files
committed
db: Add oneoff_async_connection() fns
1 parent 03c5016 commit dc60164

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/db.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::certs::CRUNCHY;
22
use diesel::{Connection, ConnectionResult, PgConnection, QueryResult};
33
use diesel_async::pooled_connection::deadpool::{Hook, HookError};
44
use diesel_async::pooled_connection::ManagerConfig;
5-
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
66
use native_tls::{Certificate, TlsConnector};
77
use postgres_native_tls::MakeTlsConnector;
88
use secrecy::ExposeSecret;
@@ -23,6 +23,18 @@ pub fn oneoff_connection() -> anyhow::Result<PgConnection> {
2323
oneoff_connection_with_config(&config).map_err(Into::into)
2424
}
2525

26+
pub async fn oneoff_async_connection_with_config(
27+
config: &config::DatabasePools,
28+
) -> ConnectionResult<AsyncPgConnection> {
29+
let url = connection_url(config, config.primary.url.expose_secret());
30+
AsyncPgConnection::establish(&url).await
31+
}
32+
33+
pub async fn oneoff_async_connection() -> anyhow::Result<AsyncPgConnection> {
34+
let config = config::DatabasePools::full_from_environment(&config::Base::from_environment()?)?;
35+
Ok(oneoff_async_connection_with_config(&config).await?)
36+
}
37+
2638
pub fn connection_url(config: &config::DatabasePools, url: &str) -> String {
2739
let mut url = Url::parse(url).expect("Invalid database URL");
2840

0 commit comments

Comments
 (0)