Skip to content

Commit

Permalink
feat: use dummy instance id to improve performance on refresh token q…
Browse files Browse the repository at this point in the history
…ueries (supabase#1454)

## What kind of change does this PR introduce?

Use the nil instance ID so that we can leverage the compound index on
instance_id and user_id when performing search and deletion


Aims to address supabase#1449 and supabase#1398. 

We note that `LogoutAllRefreshTokens` was marked as deprecated and may
be possible to remove if all access tokens now have a `sessionId`.
Additionally, `FindTokenBySessionID` can likely be replaced by using
`FindCurrentlyActiveRefreshToken`.

We will revisit these in a week or two but they are out of scope for
this PR.

Co-authored-by: joel <joel@joels-MacBook-Pro.local>
  • Loading branch information
J0 and joel authored Mar 4, 2024
1 parent 39a167b commit ed6d4a2
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/models/refresh_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func RevokeTokenFamily(tx *storage.Connection, token *RefreshToken) error {

func FindTokenBySessionID(tx *storage.Connection, sessionId *uuid.UUID) (*RefreshToken, error) {
refreshToken := &RefreshToken{}
err := tx.Q().Where("session_id = ?", sessionId).Order("created_at asc").First(refreshToken)
err := tx.Q().Where("instance_id = ? and session_id = ?", uuid.Nil, sessionId).Order("created_at asc").First(refreshToken)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, RefreshTokenNotFoundError{}
Expand Down Expand Up @@ -168,5 +168,5 @@ func createRefreshToken(tx *storage.Connection, user *User, oldToken *RefreshTok
// Deprecated. For backward compatibility, some access tokens may not have a sessionId. Use models.Logout instead.
// LogoutAllRefreshTokens deletes all sessions for a user.
func LogoutAllRefreshTokens(tx *storage.Connection, userId uuid.UUID) error {
return tx.RawQuery("DELETE FROM "+(&pop.Model{Value: RefreshToken{}}).TableName()+" WHERE user_id = ?", userId).Exec()
return tx.RawQuery("DELETE FROM "+(&pop.Model{Value: RefreshToken{}}).TableName()+" WHERE instance_id = ? and user_id = ?", uuid.Nil, userId).Exec()
}

0 comments on commit ed6d4a2

Please sign in to comment.