For impersonating access tokens, store impersonatee in the new column
This way, we don't need an index on both columns
This commit is contained in:
parent
69bff7bb77
commit
9f04fd9019
8 changed files with 38 additions and 45 deletions
|
@ -6,7 +6,7 @@ impl Database {
|
|||
pub async fn create_access_token(
|
||||
&self,
|
||||
user_id: UserId,
|
||||
impersonator_id: Option<UserId>,
|
||||
impersonated_user_id: Option<UserId>,
|
||||
access_token_hash: &str,
|
||||
max_access_token_count: usize,
|
||||
) -> Result<AccessTokenId> {
|
||||
|
@ -15,28 +15,20 @@ impl Database {
|
|||
|
||||
let token = access_token::ActiveModel {
|
||||
user_id: ActiveValue::set(user_id),
|
||||
impersonator_id: ActiveValue::set(impersonator_id),
|
||||
impersonated_user_id: ActiveValue::set(impersonated_user_id),
|
||||
hash: ActiveValue::set(access_token_hash.into()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&*tx)
|
||||
.await?;
|
||||
|
||||
let existing_token_filter = if let Some(impersonator_id) = impersonator_id {
|
||||
access_token::Column::ImpersonatorId.eq(impersonator_id)
|
||||
} else {
|
||||
access_token::Column::UserId
|
||||
.eq(user_id)
|
||||
.and(access_token::Column::ImpersonatorId.is_null())
|
||||
};
|
||||
|
||||
access_token::Entity::delete_many()
|
||||
.filter(
|
||||
access_token::Column::Id.in_subquery(
|
||||
Query::select()
|
||||
.column(access_token::Column::Id)
|
||||
.from(access_token::Entity)
|
||||
.cond_where(existing_token_filter)
|
||||
.and_where(access_token::Column::UserId.eq(user_id))
|
||||
.order_by(access_token::Column::Id, sea_orm::Order::Desc)
|
||||
.limit(10000)
|
||||
.offset(max_access_token_count as u64)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue