Commit a88cfb3c authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch 'dblessing_rapid_action_identities_queries' into 'master'

Ensure identity queries use lower query to match index

See merge request gitlab-org/gitlab!64945
parents b8465e4a 5ce9d4d8
......@@ -434,6 +434,7 @@ class User < ApplicationRecord
scope :by_id_and_login, ->(id, login) { where(id: id).where('username = LOWER(:login) OR email = LOWER(:login)', login: login) }
scope :dormant, -> { active.where('last_activity_on <= ?', MINIMUM_INACTIVE_DAYS.day.ago.to_date) }
scope :with_no_activity, -> { active.where(last_activity_on: nil) }
scope :by_provider_and_extern_uid, ->(provider, extern_uid) { joins(:identities).merge(Identity.with_extern_uid(provider, extern_uid)) }
def preferred_language
read_attribute('preferred_language') ||
......@@ -558,10 +559,6 @@ class User < ApplicationRecord
end
end
def for_github_id(id)
joins(:identities).merge(Identity.with_extern_uid(:github, id))
end
# Find a User by their primary email or any associated secondary email
def find_by_any_email(email, confirmed: false)
return unless email
......
......@@ -63,10 +63,7 @@ module Gitlab
return users[username] if users.key?(username)
users[username] = User.select(:id)
.joins(:identities)
.find_by("identities.extern_uid = ? AND identities.provider = 'bitbucket'", username)
.try(:id)
users[username] = User.by_provider_and_extern_uid(:bitbucket, username).select(:id).first&.id
end
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -138,7 +138,7 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def query_id_for_github_id(id)
User.for_github_id(id).pluck(:id).first
User.by_provider_and_extern_uid(:github, id).select(:id).first&.id
end
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -56,8 +56,8 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
def gitlab_user_id(project, gitlab_id)
user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
(user && user.id) || project.creator_id
user_id = User.by_provider_and_extern_uid(:gitlab, gitlab_id).select(:id).first&.id
user_id || project.creator_id
end
# rubocop: enable CodeReuse/ActiveRecord
end
......
......@@ -35,12 +35,7 @@ module Gitlab
def find_by_external_uid
return unless id
identities = ::Identity.arel_table
User.select(:id)
.joins(:identities)
.find_by(identities[:provider].eq(:github).and(identities[:extern_uid].eq(id)))
.try(:id)
User.by_provider_and_extern_uid(:github, id).select(:id).first&.id
end
# rubocop: enable CodeReuse/ActiveRecord
end
......
......@@ -5860,4 +5860,17 @@ RSpec.describe User do
end
end
end
describe '.by_provider_and_extern_uid' do
it 'calls Identity model scope to ensure case-insensitive query', :aggregate_failures do
expected_user = create(:user)
create(:identity, extern_uid: 'some-other-name-id', provider: :github)
create(:identity, extern_uid: 'my_github_id', provider: :gitlab)
create(:identity)
create(:identity, user: expected_user, extern_uid: 'my_github_id', provider: :github)
expect(Identity).to receive(:with_extern_uid).and_call_original
expect(described_class.by_provider_and_extern_uid(:github, 'my_github_id')).to match_array([expected_user])
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment