You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-29 22:01:14 +03:00
mas-storage-pg: use fetch_optional instead of fetch_one and matching on the error
This commit is contained in:
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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<Option<Self::Output>, sqlx::Error>;
|
||||
}
|
||||
|
||||
impl<T> LookupResultExt for Result<T, sqlx::Error> {
|
||||
type Output = T;
|
||||
|
||||
fn to_option(self) -> Result<Option<Self::Output>, 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;
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
@ -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) };
|
||||
|
||||
|
Reference in New Issue
Block a user