From b60121346fe3a4992e715dd88131d6e1c2e2c1df Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 20 Jul 2023 17:13:15 +0200 Subject: [PATCH] mas-storage-pg: use fetch_optional instead of fetch_one and matching on the error --- crates/storage-pg/src/compat/access_token.rs | 12 +++---- crates/storage-pg/src/compat/refresh_token.rs | 12 +++---- crates/storage-pg/src/compat/session.rs | 5 ++- crates/storage-pg/src/compat/sso_login.rs | 11 +++---- crates/storage-pg/src/lib.rs | 31 +------------------ crates/storage-pg/src/oauth2/access_token.rs | 12 +++---- .../src/oauth2/authorization_grant.rs | 12 +++---- crates/storage-pg/src/oauth2/client.rs | 7 ++--- crates/storage-pg/src/oauth2/refresh_token.rs | 12 +++---- crates/storage-pg/src/oauth2/session.rs | 6 ++-- crates/storage-pg/src/upstream_oauth2/link.rs | 12 +++---- .../src/upstream_oauth2/provider.rs | 6 ++-- .../storage-pg/src/upstream_oauth2/session.rs | 7 ++--- crates/storage-pg/src/user/email.rs | 16 ++++------ crates/storage-pg/src/user/mod.rs | 12 +++---- crates/storage-pg/src/user/password.rs | 7 ++--- crates/storage-pg/src/user/session.rs | 5 ++- 17 files changed, 63 insertions(+), 122 deletions(-) diff --git a/crates/storage-pg/src/compat/access_token.rs b/crates/storage-pg/src/compat/access_token.rs index 70fabac7..4781f9b8 100644 --- a/crates/storage-pg/src/compat/access_token.rs +++ b/crates/storage-pg/src/compat/access_token.rs @@ -21,7 +21,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError}; /// An implementation of [`CompatAccessTokenRepository`] for a PostgreSQL /// connection @@ -87,9 +87,8 @@ impl<'c> CompatAccessTokenRepository for PgCompatAccessTokenRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -124,9 +123,8 @@ impl<'c> CompatAccessTokenRepository for PgCompatAccessTokenRepository<'c> { access_token, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/compat/refresh_token.rs b/crates/storage-pg/src/compat/refresh_token.rs index 0811119a..b5eb414c 100644 --- a/crates/storage-pg/src/compat/refresh_token.rs +++ b/crates/storage-pg/src/compat/refresh_token.rs @@ -23,7 +23,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError}; /// An implementation of [`CompatRefreshTokenRepository`] for a PostgreSQL /// connection @@ -97,9 +97,8 @@ impl<'c> CompatRefreshTokenRepository for PgCompatRefreshTokenRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -135,9 +134,8 @@ impl<'c> CompatRefreshTokenRepository for PgCompatRefreshTokenRepository<'c> { refresh_token, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/compat/session.rs b/crates/storage-pg/src/compat/session.rs index cb1f7cd4..eed73c56 100644 --- a/crates/storage-pg/src/compat/session.rs +++ b/crates/storage-pg/src/compat/session.rs @@ -205,9 +205,8 @@ impl<'c> CompatSessionRepository for PgCompatSessionRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/compat/sso_login.rs b/crates/storage-pg/src/compat/sso_login.rs index 328bd789..1687289c 100644 --- a/crates/storage-pg/src/compat/sso_login.rs +++ b/crates/storage-pg/src/compat/sso_login.rs @@ -24,7 +24,6 @@ use uuid::Uuid; use crate::{ pagination::QueryBuilderExt, tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, - LookupResultExt, }; /// An implementation of [`CompatSsoLoginRepository`] for a PostgreSQL @@ -121,9 +120,8 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -159,9 +157,8 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> { login_token, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/lib.rs b/crates/storage-pg/src/lib.rs index 44804b32..86281408 100644 --- a/crates/storage-pg/src/lib.rs +++ b/crates/storage-pg/src/lib.rs @@ -178,36 +178,6 @@ use sqlx::migrate::Migrator; -/// An extension trait for [`Result`] which adds a [`to_option`] method, useful -/// for handling "not found" errors from [`sqlx`] -/// -/// [`to_option`]: LookupResultExt::to_option -pub trait LookupResultExt { - /// The output type - type Output; - - /// Transform a [`Result`] from a sqlx query to transform "not found" errors - /// into [`None`] - /// - /// # Errors - /// - /// Returns the original error if the error was not a - /// [`sqlx::Error::RowNotFound`] error - fn to_option(self) -> Result, sqlx::Error>; -} - -impl LookupResultExt for Result { - type Output = T; - - fn to_option(self) -> Result, sqlx::Error> { - match self { - Ok(v) => Ok(Some(v)), - Err(sqlx::Error::RowNotFound) => Ok(None), - Err(e) => Err(e), - } - } -} - pub mod compat; pub mod job; pub mod oauth2; @@ -215,6 +185,7 @@ pub mod upstream_oauth2; pub mod user; mod errors; +pub(crate) mod iden; pub(crate) mod pagination; pub(crate) mod repository; mod sea_query_sqlx; diff --git a/crates/storage-pg/src/oauth2/access_token.rs b/crates/storage-pg/src/oauth2/access_token.rs index e809fa53..396bd985 100644 --- a/crates/storage-pg/src/oauth2/access_token.rs +++ b/crates/storage-pg/src/oauth2/access_token.rs @@ -21,7 +21,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError}; /// An implementation of [`OAuth2AccessTokenRepository`] for a PostgreSQL /// connection @@ -85,9 +85,8 @@ impl<'c> OAuth2AccessTokenRepository for PgOAuth2AccessTokenRepository<'c> { "#, Uuid::from(id), ) - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -122,9 +121,8 @@ impl<'c> OAuth2AccessTokenRepository for PgOAuth2AccessTokenRepository<'c> { "#, access_token, ) - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/oauth2/authorization_grant.rs b/crates/storage-pg/src/oauth2/authorization_grant.rs index f62edae3..b01489ff 100644 --- a/crates/storage-pg/src/oauth2/authorization_grant.rs +++ b/crates/storage-pg/src/oauth2/authorization_grant.rs @@ -28,7 +28,7 @@ use ulid::Ulid; use url::Url; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError}; /// An implementation of [`OAuth2AuthorizationGrantRepository`] for a PostgreSQL /// connection @@ -340,9 +340,8 @@ impl<'c> OAuth2AuthorizationGrantRepository for PgOAuth2AuthorizationGrantReposi "#, Uuid::from(id), ) - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -391,9 +390,8 @@ impl<'c> OAuth2AuthorizationGrantRepository for PgOAuth2AuthorizationGrantReposi code, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/oauth2/client.rs b/crates/storage-pg/src/oauth2/client.rs index e737645b..c9fccba6 100644 --- a/crates/storage-pg/src/oauth2/client.rs +++ b/crates/storage-pg/src/oauth2/client.rs @@ -37,7 +37,7 @@ use ulid::Ulid; use url::Url; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError}; /// An implementation of [`OAuth2ClientRepository`] for a PostgreSQL connection pub struct PgOAuth2ClientRepository<'c> { @@ -302,9 +302,8 @@ impl<'c> OAuth2ClientRepository for PgOAuth2ClientRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/oauth2/refresh_token.rs b/crates/storage-pg/src/oauth2/refresh_token.rs index ae723f7c..962aaeca 100644 --- a/crates/storage-pg/src/oauth2/refresh_token.rs +++ b/crates/storage-pg/src/oauth2/refresh_token.rs @@ -21,7 +21,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError}; /// An implementation of [`OAuth2RefreshTokenRepository`] for a PostgreSQL /// connection @@ -93,9 +93,8 @@ impl<'c> OAuth2RefreshTokenRepository for PgOAuth2RefreshTokenRepository<'c> { "#, Uuid::from(id), ) - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -130,9 +129,8 @@ impl<'c> OAuth2RefreshTokenRepository for PgOAuth2RefreshTokenRepository<'c> { refresh_token, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/oauth2/session.rs b/crates/storage-pg/src/oauth2/session.rs index df66b2c7..01667669 100644 --- a/crates/storage-pg/src/oauth2/session.rs +++ b/crates/storage-pg/src/oauth2/session.rs @@ -24,7 +24,6 @@ use uuid::Uuid; use crate::{ pagination::QueryBuilderExt, tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, - LookupResultExt, }; /// An implementation of [`OAuth2SessionRepository`] for a PostgreSQL connection @@ -109,9 +108,8 @@ impl<'c> OAuth2SessionRepository for PgOAuth2SessionRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(session) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/upstream_oauth2/link.rs b/crates/storage-pg/src/upstream_oauth2/link.rs index 0e14f3fd..b1ba3121 100644 --- a/crates/storage-pg/src/upstream_oauth2/link.rs +++ b/crates/storage-pg/src/upstream_oauth2/link.rs @@ -21,7 +21,7 @@ use sqlx::{PgConnection, QueryBuilder}; use ulid::Ulid; use uuid::Uuid; -use crate::{pagination::QueryBuilderExt, tracing::ExecuteExt, DatabaseError, LookupResultExt}; +use crate::{pagination::QueryBuilderExt, tracing::ExecuteExt, DatabaseError}; /// An implementation of [`UpstreamOAuthLinkRepository`] for a PostgreSQL /// connection @@ -87,9 +87,8 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()? + .fetch_optional(&mut *self.conn) + .await? .map(Into::into); Ok(res) @@ -129,9 +128,8 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> { subject, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()? + .fetch_optional(&mut *self.conn) + .await? .map(Into::into); Ok(res) diff --git a/crates/storage-pg/src/upstream_oauth2/provider.rs b/crates/storage-pg/src/upstream_oauth2/provider.rs index 7400943f..508b969b 100644 --- a/crates/storage-pg/src/upstream_oauth2/provider.rs +++ b/crates/storage-pg/src/upstream_oauth2/provider.rs @@ -26,7 +26,6 @@ use uuid::Uuid; use crate::{ pagination::QueryBuilderExt, tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, - LookupResultExt, }; /// An implementation of [`UpstreamOAuthProviderRepository`] for a PostgreSQL @@ -130,9 +129,8 @@ impl<'c> UpstreamOAuthProviderRepository for PgUpstreamOAuthProviderRepository<' Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let res = res .map(UpstreamOAuthProvider::try_from) diff --git a/crates/storage-pg/src/upstream_oauth2/session.rs b/crates/storage-pg/src/upstream_oauth2/session.rs index 5780ab8d..1f3edcb2 100644 --- a/crates/storage-pg/src/upstream_oauth2/session.rs +++ b/crates/storage-pg/src/upstream_oauth2/session.rs @@ -24,7 +24,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError}; /// An implementation of [`UpstreamOAuthSessionRepository`] for a PostgreSQL /// connection @@ -136,9 +136,8 @@ impl<'c> UpstreamOAuthSessionRepository for PgUpstreamOAuthSessionRepository<'c> Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/user/email.rs b/crates/storage-pg/src/user/email.rs index 3b5a9b2f..2168efa0 100644 --- a/crates/storage-pg/src/user/email.rs +++ b/crates/storage-pg/src/user/email.rs @@ -24,7 +24,6 @@ use uuid::Uuid; use crate::{ pagination::QueryBuilderExt, tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, - LookupResultExt, }; /// An implementation of [`UserEmailRepository`] for a PostgreSQL connection @@ -122,9 +121,8 @@ impl<'c> UserEmailRepository for PgUserEmailRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(user_email) = res else { return Ok(None); @@ -160,9 +158,8 @@ impl<'c> UserEmailRepository for PgUserEmailRepository<'c> { email, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(user_email) = res else { return Ok(None); @@ -509,9 +506,8 @@ impl<'c> UserEmailRepository for PgUserEmailRepository<'c> { Uuid::from(user_email.id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/user/mod.rs b/crates/storage-pg/src/user/mod.rs index 0554c8b2..468736e6 100644 --- a/crates/storage-pg/src/user/mod.rs +++ b/crates/storage-pg/src/user/mod.rs @@ -24,7 +24,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError}; mod email; mod password; @@ -99,9 +99,8 @@ impl<'c> UserRepository for PgUserRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; @@ -131,9 +130,8 @@ impl<'c> UserRepository for PgUserRepository<'c> { username, ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/user/password.rs b/crates/storage-pg/src/user/password.rs index 1dfd90d1..22ff6081 100644 --- a/crates/storage-pg/src/user/password.rs +++ b/crates/storage-pg/src/user/password.rs @@ -21,7 +21,7 @@ use sqlx::PgConnection; use ulid::Ulid; use uuid::Uuid; -use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError, LookupResultExt}; +use crate::{tracing::ExecuteExt, DatabaseError, DatabaseInconsistencyError}; /// An implementation of [`UserPasswordRepository`] for a PostgreSQL connection pub struct PgUserPasswordRepository<'c> { @@ -75,9 +75,8 @@ impl<'c> UserPasswordRepository for PgUserPasswordRepository<'c> { Uuid::from(user.id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) }; diff --git a/crates/storage-pg/src/user/session.rs b/crates/storage-pg/src/user/session.rs index 9e928830..01668425 100644 --- a/crates/storage-pg/src/user/session.rs +++ b/crates/storage-pg/src/user/session.rs @@ -121,9 +121,8 @@ impl<'c> BrowserSessionRepository for PgBrowserSessionRepository<'c> { Uuid::from(id), ) .traced() - .fetch_one(&mut *self.conn) - .await - .to_option()?; + .fetch_optional(&mut *self.conn) + .await?; let Some(res) = res else { return Ok(None) };