Check account membership on DB level.

This commit is contained in:
2026-10-11 14:03:39 +02:00
parent 3faea141dd
commit 793f71e32e
12 changed files with 261 additions and 44 deletions
+28
View File
@@ -62,6 +62,34 @@ where
.map_err(DatabaseError::from)
}
/// Like [`get_by_id`], but also locks the account row until the end of the transaction, so the
/// executor must be a transaction. Callers locking the same account are serialized, while
/// inserting members into the account is not blocked.
pub async fn get_by_id_locking<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id, name
FROM accounts
WHERE id = $2
AND id IN (SELECT id FROM accessible_accounts($1))
FOR NO KEY UPDATE
"#,
user_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn get_by_ids<'e, E>(
executor: E,
user_id: Uuid,