-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factors: Add more tests to factor-outbound-pg
Signed-off-by: Caleb Schoepp <[email protected]>
- Loading branch information
1 parent
112c4e4
commit 32cfd96
Showing
4 changed files
with
207 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use native_tls::TlsConnector; | ||
use postgres_native_tls::MakeTlsConnector; | ||
use spin_world::async_trait; | ||
use tokio_postgres::{config::SslMode, types::ToSql, Error, Row, ToStatement}; | ||
use tokio_postgres::{Client as PgClient, NoTls, Socket}; | ||
|
||
#[async_trait] | ||
pub trait Client { | ||
async fn build_client(address: &str) -> anyhow::Result<Self> | ||
where | ||
Self: Sized; | ||
|
||
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> | ||
where | ||
T: ?Sized + ToStatement + Sync + Send; | ||
|
||
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error> | ||
where | ||
T: ?Sized + ToStatement + Sync + Send; | ||
} | ||
|
||
#[async_trait] | ||
impl Client for PgClient { | ||
async fn build_client(address: &str) -> anyhow::Result<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
let config = address.parse::<tokio_postgres::Config>()?; | ||
|
||
tracing::debug!("Build new connection: {}", address); | ||
|
||
if config.get_ssl_mode() == SslMode::Disable { | ||
async move { | ||
let (client, connection) = config.connect(NoTls).await?; | ||
spawn(connection); | ||
Ok(client) | ||
} | ||
.await | ||
} else { | ||
async move { | ||
let builder = TlsConnector::builder(); | ||
let connector = MakeTlsConnector::new(builder.build()?); | ||
let (client, connection) = config.connect(connector).await?; | ||
spawn(connection); | ||
Ok(client) | ||
} | ||
.await | ||
} | ||
} | ||
|
||
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> | ||
where | ||
T: ?Sized + ToStatement + Sync + Send, | ||
{ | ||
self.execute(query, params).await | ||
} | ||
|
||
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error> | ||
where | ||
T: ?Sized + ToStatement + Sync + Send, | ||
{ | ||
self.query(query, params).await | ||
} | ||
} | ||
|
||
fn spawn<T>(connection: tokio_postgres::Connection<Socket, T>) | ||
where | ||
T: tokio_postgres::tls::TlsStream + std::marker::Unpin + std::marker::Send + 'static, | ||
{ | ||
tokio::spawn(async move { | ||
if let Err(e) = connection.await { | ||
tracing::error!("Postgres connection error: {}", e); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters