diff options
author | Stan Hu <stanhu@gmail.com> | 2019-09-10 03:05:55 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-09-10 07:51:57 +0300 |
commit | 08c3e59aeed34ad71e74afb674ddda7327fdc3a7 (patch) | |
tree | 544d9fa244519ac8b18b29ff8abf47bfdd40b30a /spec/models/oauth_access_token_spec.rb | |
parent | ffa5328c39f195d3253e586569fc2474d3aa6860 (diff) |
Optimize /admin/applications so that it does not timeout
On our dev instance, /admin/applications as not loading because:
1. There was an unindexed query by `application_id`.
2. There was an expensive query that attempted to load 1 million
unique entries via ActiveRecord just to find the unique count.
We fix the first issue by adding an index for that column.
We fix the second issue with a simple SELECT COUNT(DISTINCT
resource_owner_id) SQL query.
In addition, we add pagination to avoid loading more than 20
applications at once.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/67228
Diffstat (limited to 'spec/models/oauth_access_token_spec.rb')
-rw-r--r-- | spec/models/oauth_access_token_spec.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/spec/models/oauth_access_token_spec.rb b/spec/models/oauth_access_token_spec.rb new file mode 100644 index 00000000000..0a1c576a5e7 --- /dev/null +++ b/spec/models/oauth_access_token_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe OauthAccessToken do + let(:user) { create(:user) } + let(:app_one) { create(:oauth_application) } + let(:app_two) { create(:oauth_application) } + let(:app_three) { create(:oauth_application) } + let(:tokens) { described_class.all } + + before do + create(:oauth_access_token, application_id: app_one.id) + create_list(:oauth_access_token, 2, resource_owner: user, application_id: app_two.id) + end + + it 'returns unique owners' do + expect(tokens.count).to eq(3) + expect(tokens.distinct_resource_owner_counts([app_one])).to eq({ app_one.id => 1 }) + expect(tokens.distinct_resource_owner_counts([app_two])).to eq({ app_two.id => 1 }) + expect(tokens.distinct_resource_owner_counts([app_three])).to eq({}) + expect(tokens.distinct_resource_owner_counts([app_one, app_two])) + .to eq({ + app_one.id => 1, + app_two.id => 1 + }) + end +end |