From f67a0038cf6bb1b788079c66435ed5ab117e05f4 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 12 Feb 2022 23:47:08 +0530 Subject: [PATCH] feat : GistDatabase is Clone and add ping to GistDatabase --- database/db-core/src/lib.rs | 33 +++++++++++++++++++++++++-- database/db-sqlx-postgres/src/lib.rs | 12 ++++++++++ database/db-sqlx-sqlite/src/errors.rs | 1 + database/db-sqlx-sqlite/src/lib.rs | 12 ++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/database/db-core/src/lib.rs b/database/db-core/src/lib.rs index 469ff25..6a0ebc4 100644 --- a/database/db-core/src/lib.rs +++ b/database/db-core/src/lib.rs @@ -93,7 +93,7 @@ pub struct UpdateUsernamePayload<'a> { use dev::*; /// foo #[async_trait] -pub trait GistDatabase: std::marker::Send + std::marker::Sync { +pub trait GistDatabase: std::marker::Send + std::marker::Sync + CloneGistDatabase { /// Update email of specified user in database async fn update_email(&self, payload: &UpdateEmailPayload) -> DBResult<()>; /// Update password of specified user in database @@ -118,10 +118,13 @@ pub trait GistDatabase: std::marker::Send + std::marker::Sync { async fn email_register(&self, payload: &EmailRegisterPayload) -> DBResult<()>; /// register with username async fn username_register(&self, payload: &UsernameRegisterPayload) -> DBResult<()>; + + /// ping DB + async fn ping(&self) -> bool; } #[async_trait] -impl GistDatabase for Box { +impl GistDatabase for Box { async fn update_email(&self, payload: &UpdateEmailPayload) -> DBResult<()> { (**self).update_email(payload).await } @@ -169,4 +172,30 @@ impl GistDatabase for Box { async fn username_register(&self, payload: &UsernameRegisterPayload) -> DBResult<()> { (**self).username_register(payload).await } + + /// ping DB + async fn ping(&self) -> bool { + (**self).ping().await + } +} + +/// Trait to clone GistDatabase +pub trait CloneGistDatabase { + /// clone DB + fn clone_db<'a>(&self) -> Box; +} + +impl CloneGistDatabase for T +where + T: GistDatabase + Clone + 'static, +{ + fn clone_db(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Self { + (**self).clone_db() + } } diff --git a/database/db-sqlx-postgres/src/lib.rs b/database/db-sqlx-postgres/src/lib.rs index 9fe3c16..0a2fecc 100644 --- a/database/db-sqlx-postgres/src/lib.rs +++ b/database/db-sqlx-postgres/src/lib.rs @@ -14,6 +14,7 @@ pub mod tests; /// Database pool. All database functionallity(`libadmin` traits) are implemented on this /// data structure +#[derive(Clone)] pub struct Database { /// database pool pub pool: PgPool, @@ -269,4 +270,15 @@ impl GistDatabase for Database { Ok(secret.secret) } + + /// ping DB + async fn ping(&self) -> bool { + use sqlx::Connection; + + if let Ok(mut con) = self.pool.acquire().await { + con.ping().await.is_ok() + } else { + false + } + } } diff --git a/database/db-sqlx-sqlite/src/errors.rs b/database/db-sqlx-sqlite/src/errors.rs index 564bf86..6ee9cfd 100644 --- a/database/db-sqlx-sqlite/src/errors.rs +++ b/database/db-sqlx-sqlite/src/errors.rs @@ -7,6 +7,7 @@ pub fn map_register_err(e: Error) -> DBError { if let Error::Database(err) = e { if err.code() == Some(Cow::from("2067")) { let msg = err.message(); + println!("{}", msg); if msg.contains("admin_users.username") { DBError::DuplicateUsername } else if msg.contains("admin_users.email") { diff --git a/database/db-sqlx-sqlite/src/lib.rs b/database/db-sqlx-sqlite/src/lib.rs index 1fc60ff..8491e5d 100644 --- a/database/db-sqlx-sqlite/src/lib.rs +++ b/database/db-sqlx-sqlite/src/lib.rs @@ -7,6 +7,7 @@ pub mod errors; #[cfg(test)] pub mod tests; +#[derive(Clone)] pub struct Database { pub pool: SqlitePool, } @@ -238,4 +239,15 @@ impl GistDatabase for Database { Ok(secret.secret) } + + /// ping DB + async fn ping(&self) -> bool { + use sqlx::Connection; + + if let Ok(mut con) = self.pool.acquire().await { + con.ping().await.is_ok() + } else { + false + } + } }