From af809d21bc14f8eb48a44300db9de26a358468c0 Mon Sep 17 00:00:00 2001 From: edgerunnergit Date: Sun, 16 Jun 2024 18:25:38 +0530 Subject: [PATCH 1/3] added initial support for diesel-async for in aws-rds --- resources/aws-rds/Cargo.toml | 10 ++++- resources/aws-rds/src/lib.rs | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/resources/aws-rds/Cargo.toml b/resources/aws-rds/Cargo.toml index 8c2df627d..64b7a32a3 100644 --- a/resources/aws-rds/Cargo.toml +++ b/resources/aws-rds/Cargo.toml @@ -9,6 +9,7 @@ keywords = ["shuttle-service", "rds"] [dependencies] async-trait = "0.1.56" +diesel-async = { version = "0.4.1", optional = true } paste = "1.0.7" serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -19,10 +20,15 @@ sqlx = { version = "0.7.1", optional = true } default = [] # Database -postgres = ["sqlx?/postgres"] -mysql = ["sqlx?/mysql"] +postgres = ["sqlx?/postgres", "diesel-async?/postgres"] +mysql = ["sqlx?/mysql", "diesel-async?/mysql"] mariadb = ["sqlx?/mysql"] +# Databases 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" ] + # Add an sqlx Pool as a resource output type sqlx = ["dep:sqlx", "sqlx/runtime-tokio", "sqlx/tls-rustls"] sqlx-native-tls = ["dep:sqlx", "sqlx/runtime-tokio", "sqlx/tls-native-tls"] diff --git a/resources/aws-rds/src/lib.rs b/resources/aws-rds/src/lib.rs index a00c1d38b..79beba645 100644 --- a/resources/aws-rds/src/lib.rs +++ b/resources/aws-rds/src/lib.rs @@ -8,6 +8,15 @@ 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; + macro_rules! aws_engine { ($feature:expr, $struct_ident:ident) => { paste::paste! { @@ -76,6 +85,71 @@ impl IntoResource for OutputWrapper { } // If these were done in the main macro above, this would produce two conflicting `impl IntoResource` + +#[cfg(feature = "diesel_async")] +mod _diesel_async { + use super::*; + + #[cfg(feature = "postgres")] + #[async_trait] + impl IntoResource for OutputWrapper { + async fn into_resource(self) -> Result { + 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")] +mod _diesel_async_bb8 { + use super::*; + + #[cfg(feature = "postgres")] + #[async_trait] + impl IntoResource> for OutputWrapper { + async fn into_resource( + self, + ) -> Result, Error> { + let connection_string: String = self.into_resource().await.unwrap(); + + Ok(diesel_bb8::Pool::builder() + .min_idle(Some(1)) + .max_size(5) + .build(AsyncDieselConnectionManager::new(connection_string)) + .await + .map_err(shuttle_service::error::CustomError::new)?) + } + } +} + +#[cfg(feature = "diesel-async-deadpool")] +mod _diesel_async_deadpool { + use super::*; + + #[cfg(feature = "postgres")] + #[async_trait] + impl IntoResource> for OutputWrapper { + async fn into_resource( + self, + ) -> Result, Error> { + let connection_string: String = self.into_resource().await.unwrap(); + + Ok( + diesel_deadpool::Pool::builder(AsyncDieselConnectionManager::new( + connection_string, + )) + .max_size(5 as usize) + .build() + .map_err(shuttle_service::error::CustomError::new)?, + ) + } + } +} + #[cfg(feature = "sqlx")] mod _sqlx { use super::*; From 37eb710fa19c20b4dcb3b7cdaae2eb7456e90aca Mon Sep 17 00:00:00 2001 From: edgerunnergit Date: Sun, 16 Jun 2024 22:09:58 +0530 Subject: [PATCH 2/3] add support for diesel_async with mysql databases in aws-rds --- resources/aws-rds/src/lib.rs | 68 ++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/resources/aws-rds/src/lib.rs b/resources/aws-rds/src/lib.rs index 79beba645..77a747221 100644 --- a/resources/aws-rds/src/lib.rs +++ b/resources/aws-rds/src/lib.rs @@ -17,6 +17,11 @@ 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; + macro_rules! aws_engine { ($feature:expr, $struct_ident:ident) => { paste::paste! { @@ -102,6 +107,19 @@ mod _diesel_async { .map_err(shuttle_service::error::CustomError::new)?) } } + + #[cfg(feature = "mysql")] + #[async_trait] + impl IntoResource for OutputWrapper { + async fn into_resource(self) -> Result { + 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")] @@ -117,8 +135,25 @@ mod _diesel_async_bb8 { let connection_string: String = self.into_resource().await.unwrap(); Ok(diesel_bb8::Pool::builder() - .min_idle(Some(1)) - .max_size(5) + .min_idle(Some(MIN_CONNECTIONS)) + .max_size(MAX_CONNECTIONS) + .build(AsyncDieselConnectionManager::new(connection_string)) + .await + .map_err(shuttle_service::error::CustomError::new)?) + } + } + + #[cfg(feature = "mysql")] + #[async_trait] + impl IntoResource> for OutputWrapper { + async fn into_resource( + self, + ) -> Result, 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)?) @@ -142,7 +177,26 @@ mod _diesel_async_deadpool { diesel_deadpool::Pool::builder(AsyncDieselConnectionManager::new( connection_string, )) - .max_size(5 as usize) + .max_size(MAX_CONNECTIONS as usize) + .build() + .map_err(shuttle_service::error::CustomError::new)?, + ) + } + } + + #[cfg(feature = "mysql")] + #[async_trait] + impl IntoResource> for OutputWrapper { + async fn into_resource( + self, + ) -> Result, 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)?, ) @@ -161,8 +215,8 @@ mod _sqlx { 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)?) @@ -176,8 +230,8 @@ mod _sqlx { let connection_string: String = self.into_resource().await.unwrap(); Ok(sqlx::mysql::MySqlPoolOptions::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)?) From da57883ab118b8ed3f9e3e3df8c06736b0db7504 Mon Sep 17 00:00:00 2001 From: edgerunnergit Date: Sun, 16 Jun 2024 22:54:35 +0530 Subject: [PATCH 3/3] add support for diesel_async with mariadb in aws-rds --- resources/aws-rds/Cargo.toml | 2 +- resources/aws-rds/src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/aws-rds/Cargo.toml b/resources/aws-rds/Cargo.toml index 64b7a32a3..0fe261a7f 100644 --- a/resources/aws-rds/Cargo.toml +++ b/resources/aws-rds/Cargo.toml @@ -22,7 +22,7 @@ default = [] # Database postgres = ["sqlx?/postgres", "diesel-async?/postgres"] mysql = ["sqlx?/mysql", "diesel-async?/mysql"] -mariadb = ["sqlx?/mysql"] +mariadb = ["sqlx?/mysql", "diesel-async?/mysql"] # Databases with diesel-async support diesel-async = ["dep:diesel-async"] diff --git a/resources/aws-rds/src/lib.rs b/resources/aws-rds/src/lib.rs index 77a747221..44b6174c6 100644 --- a/resources/aws-rds/src/lib.rs +++ b/resources/aws-rds/src/lib.rs @@ -108,7 +108,7 @@ mod _diesel_async { } } - #[cfg(feature = "mysql")] + #[cfg(any(feature = "mysql", feature = "mariadb"))] #[async_trait] impl IntoResource for OutputWrapper { async fn into_resource(self) -> Result { @@ -143,7 +143,7 @@ mod _diesel_async_bb8 { } } - #[cfg(feature = "mysql")] + #[cfg(any(feature = "mysql", feature = "mariadb"))] #[async_trait] impl IntoResource> for OutputWrapper { async fn into_resource( @@ -184,7 +184,7 @@ mod _diesel_async_deadpool { } } - #[cfg(feature = "mysql")] + #[cfg(any(feature = "mysql", feature = "mariadb"))] #[async_trait] impl IntoResource> for OutputWrapper { async fn into_resource(