Skip to content

Commit

Permalink
feat(resources): Add diesel-async support for shuttle-shared-db (#…
Browse files Browse the repository at this point in the history
…1664)

* Add `diesel-async` support for `shuttle-shared-db`

* Add feature flags to Makefile.toml
  • Loading branch information
aumetra authored Mar 18, 2024
1 parent 2428e99 commit cd5476c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ feature_flags = array "--all-features"
###### Crates that have mutually exclusive features define
###### their own set of feature flags to test:
if eq ${path} "resources/shared-db"
feature_flags = array "-F mongodb" "-F postgres" "-F postgres,sqlx" "-F postgres,sqlx-native-tls"
feature_flags = array "-F mongodb" "-F postgres" "-F postgres,sqlx" "-F postgres,sqlx-native-tls" "-F postgres,diesel-async" "-F postgres,diesel-async-bb8" "-F postgres,diesel-async-deadpool"
elseif eq ${path} "services/shuttle-axum"
feature_flags = array "-F axum" "-F axum-0-6"
elseif eq ${path} "services/shuttle-serenity"
Expand Down
7 changes: 6 additions & 1 deletion resources/shared-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ keywords = ["shuttle-service", "database"]

[dependencies]
async-trait = "0.1.56"
diesel-async = { version = "0.4.1", optional = true }
mongodb = { version = "2.3.0", optional = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand All @@ -21,7 +22,11 @@ default = []
mongodb = ["dep:mongodb"]

# Postgres
postgres = ["sqlx?/postgres"]
postgres = ["diesel-async?/postgres", "sqlx?/postgres"]
# Postgres with diesel-async support
diesel-async = ["dep:diesel-async"]
diesel-async-bb8 = ["diesel-async", "diesel-async/bb8"]
diesel-async-deadpool = ["diesel-async", "diesel-async/deadpool"]
# Postgres with an sqlx PgPool
sqlx = ["dep:sqlx", "sqlx/runtime-tokio", "sqlx/tls-rustls"]
sqlx-native-tls = ["dep:sqlx", "sqlx/runtime-tokio", "sqlx/tls-native-tls"]
65 changes: 63 additions & 2 deletions resources/shared-db/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ use shuttle_service::{
DatabaseResource, DbInput, Error, IntoResource, ResourceFactory, ResourceInputBuilder,
};

#[cfg(any(feature = "diesel-async-bb8", feature = "diesel-async-deadpool"))]
use diesel_async::pooled_connection::AsyncDieselConnectionManager;

#[cfg(feature = "diesel-async-bb8")]
use diesel_async::pooled_connection::bb8 as diesel_bb8;

#[cfg(feature = "diesel-async-deadpool")]
use diesel_async::pooled_connection::deadpool as diesel_deadpool;

#[allow(dead_code)]
const MIN_CONNECTIONS: u32 = 1;
#[allow(dead_code)]
const MAX_CONNECTIONS: u32 = 5;

/// Shuttle managed Postgres DB in a shared cluster
#[derive(Default)]
pub struct Postgres(DbInput);
Expand Down Expand Up @@ -47,15 +61,62 @@ impl IntoResource<String> for OutputWrapper {
}
}

#[cfg(feature = "diesel-async")]
#[async_trait]
impl IntoResource<diesel_async::AsyncPgConnection> for OutputWrapper {
async fn into_resource(self) -> Result<diesel_async::AsyncPgConnection, Error> {
use diesel_async::{AsyncConnection, AsyncPgConnection};

let connection_string: String = self.into_resource().await.unwrap();
Ok(AsyncPgConnection::establish(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
}
}

#[cfg(feature = "diesel-async-bb8")]
#[async_trait]
impl IntoResource<diesel_bb8::Pool<diesel_async::AsyncPgConnection>> for OutputWrapper {
async fn into_resource(
self,
) -> Result<diesel_bb8::Pool<diesel_async::AsyncPgConnection>, Error> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(diesel_bb8::Pool::builder()
.min_idle(Some(MIN_CONNECTIONS))
.max_size(MAX_CONNECTIONS)
.build(AsyncDieselConnectionManager::new(connection_string))
.await
.map_err(shuttle_service::error::CustomError::new)?)
}
}

#[cfg(feature = "diesel-async-deadpool")]
#[async_trait]
impl IntoResource<diesel_deadpool::Pool<diesel_async::AsyncPgConnection>> for OutputWrapper {
async fn into_resource(
self,
) -> Result<diesel_deadpool::Pool<diesel_async::AsyncPgConnection>, Error> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(
diesel_deadpool::Pool::builder(AsyncDieselConnectionManager::new(connection_string))
.max_size(MAX_CONNECTIONS as usize)
.build()
.map_err(shuttle_service::error::CustomError::new)?,
)
}
}

#[cfg(feature = "sqlx")]
#[async_trait]
impl IntoResource<sqlx::PgPool> for OutputWrapper {
async fn into_resource(self) -> Result<sqlx::PgPool, Error> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(sqlx::postgres::PgPoolOptions::new()
.min_connections(1)
.max_connections(5)
.min_connections(MIN_CONNECTIONS)
.max_connections(MAX_CONNECTIONS)
.connect(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
Expand Down

0 comments on commit cd5476c

Please sign in to comment.