Skip to content

Query only distinct OAuth access tokens by application ID

Drew Blessing requested to merge dblessing_oauth_apps_fix2 into master

What does this MR do and why?

Describe in detail what your merge request does and why.

Follow-up to !90740 (merged)

With recent changes all access tokens for a given application were being displayed rather than just the latest one, as previously. This included expired tokens or duplicate tokens from a re-authorization. With this change only the latest token will be displayed for each application as it was prior to !90740 (merged).

I also opened a follow-up issue where we can discuss potential changes to this view in the long-term - #366458

Database

SELECT distinct on(application_id) * FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."resource_owner_id" = 1 AND "oauth_access_tokens"."revoked_at" IS NULL ORDER BY "oauth_access_tokens"."application_id" DESC, "oauth_access_tokens"."created_at" DESC

Migrate

main: == 20220630202329 AddPartialIndexOnOauthAccessTokensRevokedAtWithOrder: migrating
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:oauth_access_tokens, [:resource_owner_id, :application_id, :created_at], {:name=>"partial_index_user_id_app_id_created_at_token_not_revoked", :where=>"revoked_at IS NULL", :algorithm=>:concurrently})
main:    -> 0.0037s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0002s
main: -- add_index(:oauth_access_tokens, [:resource_owner_id, :application_id, :created_at], {:name=>"partial_index_user_id_app_id_created_at_token_not_revoked", :where=>"revoked_at IS NULL", :algorithm=>:concurrently})
main:    -> 0.0019s
main: -- execute("RESET statement_timeout")
main:    -> 0.0002s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:oauth_access_tokens, [:resource_owner_id, :created_at], {:name=>"partial_index_resource_owner_id_created_at_token_not_revoked", :algorithm=>:concurrently})
main:    -> 0.0016s
main: -- remove_index(:oauth_access_tokens, {:name=>"partial_index_resource_owner_id_created_at_token_not_revoked", :algorithm=>:concurrently, :column=>[:resource_owner_id, :created_at]})
main:    -> 0.0029s
main: == 20220630202329 AddPartialIndexOnOauthAccessTokensRevokedAtWithOrder: migrated (0.0160s)

Rollback

main: == 20220630202329 AddPartialIndexOnOauthAccessTokensRevokedAtWithOrder: reverting
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:oauth_access_tokens, [:resource_owner_id, :created_at], {:name=>"partial_index_resource_owner_id_created_at_token_not_revoked", :algorithm=>:concurrently})
main:    -> 0.0043s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0001s
main: -- add_index(:oauth_access_tokens, [:resource_owner_id, :created_at], {:name=>"partial_index_resource_owner_id_created_at_token_not_revoked", :algorithm=>:concurrently})
main:    -> 0.0016s
main: -- execute("RESET statement_timeout")
main:    -> 0.0002s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:oauth_access_tokens, [:resource_owner_id, :application_id, :created_at], {:name=>"partial_index_user_id_app_id_created_at_token_not_revoked", :algorithm=>:concurrently})
main:    -> 0.0017s
main: -- remove_index(:oauth_access_tokens, {:name=>"partial_index_user_id_app_id_created_at_token_not_revoked", :algorithm=>:concurrently, :column=>[:resource_owner_id, :application_id, :created_at]})
main:    -> 0.0023s
main: == 20220630202329 AddPartialIndexOnOauthAccessTokensRevokedAtWithOrder: reverted (0.0154s)

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Drew Blessing

Merge request reports