Commit 9842eb91 authored by Toon Claes's avatar Toon Claes

Merge branch '211877-find-by-instead-of-where-first' into 'master'

Use find_by instead of where + first

Closes #211877

See merge request gitlab-org/gitlab!27914
parents 75c3b3e9 9693677a
...@@ -46,7 +46,7 @@ class KeysFinder ...@@ -46,7 +46,7 @@ class KeysFinder
return keys unless params[:fingerprint].present? return keys unless params[:fingerprint].present?
raise InvalidFingerprint unless valid_fingerprint_param? raise InvalidFingerprint unless valid_fingerprint_param?
keys.where(fingerprint_query).first # rubocop: disable CodeReuse/ActiveRecord keys.find_by(fingerprint_query) # rubocop: disable CodeReuse/ActiveRecord
end end
def valid_fingerprint_param? def valid_fingerprint_param?
......
...@@ -22,7 +22,7 @@ class UnsubscribesController < ApplicationController ...@@ -22,7 +22,7 @@ class UnsubscribesController < ApplicationController
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def get_user def get_user
@email = Base64.urlsafe_decode64(params[:email]) @email = Base64.urlsafe_decode64(params[:email])
User.where(email: @email).first User.find_by(email: @email)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
...@@ -84,7 +84,7 @@ module Geo ...@@ -84,7 +84,7 @@ module Geo
last_processed = Geo::EventLogState.last_processed last_processed = Geo::EventLogState.last_processed
return first unless last_processed return first unless last_processed
where('id > ?', last_processed.event_id).first find_by('id > ?', last_processed.event_id)
end end
def self.event_classes def self.event_classes
......
...@@ -102,7 +102,7 @@ class GeoNode < ApplicationRecord ...@@ -102,7 +102,7 @@ class GeoNode < ApplicationRecord
# Tries to find a GeoNode by oauth_application_id, returning nil if none could be found. # Tries to find a GeoNode by oauth_application_id, returning nil if none could be found.
def find_by_oauth_application_id(oauth_application_id) def find_by_oauth_application_id(oauth_application_id)
where(oauth_application_id: oauth_application_id).take find_by(oauth_application_id: oauth_application_id)
end end
private private
......
...@@ -92,11 +92,11 @@ class MergeTrain < ApplicationRecord ...@@ -92,11 +92,11 @@ class MergeTrain < ApplicationRecord
def first_in_train_from(merge_request_ids) def first_in_train_from(merge_request_ids)
merge_request = MergeRequest.find(merge_request_ids.first) merge_request = MergeRequest.find(merge_request_ids.first)
all_active_mrs_in_train(merge_request.target_project_id, merge_request.target_branch).where(id: merge_request_ids).first all_active_mrs_in_train(merge_request.target_project_id, merge_request.target_branch).find_by(id: merge_request_ids)
end end
def last_complete_mr_in_train(target_project_id, target_branch) def last_complete_mr_in_train(target_project_id, target_branch)
MergeRequest.where(id: last_complete_merge_train(target_project_id, target_branch)).take MergeRequest.find_by(id: last_complete_merge_train(target_project_id, target_branch))
end end
def sha_exists_in_history?(target_project_id, target_branch, newrev, limit: 20) def sha_exists_in_history?(target_project_id, target_branch, newrev, limit: 20)
......
...@@ -77,7 +77,7 @@ module EE ...@@ -77,7 +77,7 @@ module EE
# replace any existing Kerberos identity for the user # replace any existing Kerberos identity for the user
return unless ldap_user.kerberos_principal.present? return unless ldap_user.kerberos_principal.present?
kerberos_identity = user.identities.where(provider: :kerberos).first kerberos_identity = user.identities.find_by(provider: :kerberos)
return if kerberos_identity && kerberos_identity.extern_uid == ldap_user.kerberos_principal return if kerberos_identity && kerberos_identity.extern_uid == ldap_user.kerberos_principal
kerberos_identity ||= ::Identity.new(provider: :kerberos, user: user) kerberos_identity ||= ::Identity.new(provider: :kerberos, user: user)
......
...@@ -7,8 +7,8 @@ describe API::MavenPackages do ...@@ -7,8 +7,8 @@ describe API::MavenPackages do
let(:project) { create(:project, :public, namespace: group) } let(:project) { create(:project, :public, namespace: group) }
let(:package) { create(:maven_package, project: project) } let(:package) { create(:maven_package, project: project) }
let(:maven_metadatum) { package.maven_metadatum } let(:maven_metadatum) { package.maven_metadatum }
let(:package_file) { package.package_files.where('file_name like ?', '%.xml').first } let(:package_file) { package.package_files.find_by('file_name like ?', '%.xml') }
let(:jar_file) { package.package_files.where('file_name like ?', '%.jar').first } let(:jar_file) { package.package_files.find_by('file_name like ?', '%.jar') }
let(:personal_access_token) { create(:personal_access_token, user: user) } let(:personal_access_token) { create(:personal_access_token, user: user) }
let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') } let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') }
let(:headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } } let(:headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } }
......
...@@ -96,7 +96,7 @@ describe Epics::IssuePromoteService do ...@@ -96,7 +96,7 @@ describe Epics::IssuePromoteService do
let(:issue) { create(:issue, project: project, description: "description with mention to #{user.to_reference}") } let(:issue) { create(:issue, project: project, description: "description with mention to #{user.to_reference}") }
it 'only saves user mentions with actual mentions' do it 'only saves user mentions with actual mentions' do
expect(epic.user_mentions.where(note_id: nil).first.mentioned_users_ids).to match_array([user.id]) expect(epic.user_mentions.find_by(note_id: nil).mentioned_users_ids).to match_array([user.id])
expect(epic.user_mentions.where.not(note_id: nil).first.mentioned_users_ids).to match_array([user.id]) expect(epic.user_mentions.where.not(note_id: nil).first.mentioned_users_ids).to match_array([user.id])
expect(epic.user_mentions.where.not(note_id: nil).count).to eq 1 expect(epic.user_mentions.where.not(note_id: nil).count).to eq 1
expect(epic.user_mentions.count).to eq 2 expect(epic.user_mentions.count).to eq 2
......
...@@ -45,7 +45,7 @@ module Gitlab ...@@ -45,7 +45,7 @@ module Gitlab
reverts_for_type('namespace') do |path_before_rename, current_path| reverts_for_type('namespace') do |path_before_rename, current_path|
matches_path = MigrationClasses::Route.arel_table[:path].matches(current_path) matches_path = MigrationClasses::Route.arel_table[:path].matches(current_path)
namespace = MigrationClasses::Namespace.joins(:route) namespace = MigrationClasses::Namespace.joins(:route)
.where(matches_path).first&.becomes(MigrationClasses::Namespace) .find_by(matches_path)&.becomes(MigrationClasses::Namespace)
if namespace if namespace
perform_rename(namespace, current_path, path_before_rename) perform_rename(namespace, current_path, path_before_rename)
......
...@@ -37,7 +37,7 @@ module Gitlab ...@@ -37,7 +37,7 @@ module Gitlab
reverts_for_type('project') do |path_before_rename, current_path| reverts_for_type('project') do |path_before_rename, current_path|
matches_path = MigrationClasses::Route.arel_table[:path].matches(current_path) matches_path = MigrationClasses::Route.arel_table[:path].matches(current_path)
project = MigrationClasses::Project.joins(:route) project = MigrationClasses::Project.joins(:route)
.where(matches_path).first .find_by(matches_path)
if project if project
perform_rename(project, current_path, path_before_rename) perform_rename(project, current_path, path_before_rename)
......
...@@ -62,7 +62,7 @@ module Gitlab ...@@ -62,7 +62,7 @@ module Gitlab
end end
def find_object def find_object
klass.where(where_clause).first klass.find_by(where_clause)
end end
def where_clause def where_clause
......
...@@ -19,7 +19,7 @@ module Gitlab ...@@ -19,7 +19,7 @@ module Gitlab
@exported_members.inject(missing_keys_tracking_hash) do |hash, member| @exported_members.inject(missing_keys_tracking_hash) do |hash, member|
if member['user'] if member['user']
old_user_id = member['user']['id'] old_user_id = member['user']['id']
existing_user = User.where(find_user_query(member)).first existing_user = User.find_by(find_user_query(member))
hash[old_user_id] = existing_user.id if existing_user && add_team_member(member, existing_user) hash[old_user_id] = existing_user.id if existing_user && add_team_member(member, existing_user)
else else
add_team_member(member) add_team_member(member)
......
...@@ -67,7 +67,7 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, schema: 201802081 ...@@ -67,7 +67,7 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, schema: 201802081
it 'does not add hashed files to the untracked_files_for_uploads table' do it 'does not add hashed files to the untracked_files_for_uploads table' do
described_class.new.perform described_class.new.perform
hashed_file_path = get_uploads(project2, 'Project').where(uploader: 'FileUploader').first.path hashed_file_path = get_uploads(project2, 'Project').find_by(uploader: 'FileUploader').path
expect(untracked_files_for_uploads.where("path like '%#{hashed_file_path}%'").exists?).to be_falsey expect(untracked_files_for_uploads.where("path like '%#{hashed_file_path}%'").exists?).to be_falsey
end end
......
...@@ -123,7 +123,7 @@ describe Gitlab::ImportExport::Project::TreeRestorer do ...@@ -123,7 +123,7 @@ describe Gitlab::ImportExport::Project::TreeRestorer do
end end
it 'preserves updated_at on issues' do it 'preserves updated_at on issues' do
issue = Issue.where(description: 'Aliquam enim illo et possimus.').first issue = Issue.find_by(description: 'Aliquam enim illo et possimus.')
expect(issue.reload.updated_at.to_s).to eq('2016-06-14 15:02:47 UTC') expect(issue.reload.updated_at.to_s).to eq('2016-06-14 15:02:47 UTC')
end end
...@@ -170,7 +170,7 @@ describe Gitlab::ImportExport::Project::TreeRestorer do ...@@ -170,7 +170,7 @@ describe Gitlab::ImportExport::Project::TreeRestorer do
end end
context 'event at forth level of the tree' do context 'event at forth level of the tree' do
let(:event) { Event.where(action: 6).first } let(:event) { Event.find_by(action: 6) }
it 'restores the event' do it 'restores the event' do
expect(event).not_to be_nil expect(event).not_to be_nil
...@@ -440,7 +440,7 @@ describe Gitlab::ImportExport::Project::TreeRestorer do ...@@ -440,7 +440,7 @@ describe Gitlab::ImportExport::Project::TreeRestorer do
end end
it 'restores external pull request for the restored pipeline' do it 'restores external pull request for the restored pipeline' do
pipeline_with_external_pr = @project.ci_pipelines.where(source: 'external_pull_request_event').first pipeline_with_external_pr = @project.ci_pipelines.find_by(source: 'external_pull_request_event')
expect(pipeline_with_external_pr.external_pull_request).to be_persisted expect(pipeline_with_external_pr.external_pull_request).to be_persisted
end end
......
...@@ -26,7 +26,7 @@ describe GenerateMissingRoutes do ...@@ -26,7 +26,7 @@ describe GenerateMissingRoutes do
described_class.new.up described_class.new.up
route = routes.where(source_type: 'Project').take route = routes.find_by(source_type: 'Project')
expect(route.source_id).to eq(project.id) expect(route.source_id).to eq(project.id)
expect(route.path).to eq("gitlab/gitlab-ce-#{project.id}") expect(route.path).to eq("gitlab/gitlab-ce-#{project.id}")
...@@ -37,7 +37,7 @@ describe GenerateMissingRoutes do ...@@ -37,7 +37,7 @@ describe GenerateMissingRoutes do
described_class.new.up described_class.new.up
route = routes.where(source_type: 'Namespace').take route = routes.find_by(source_type: 'Namespace')
expect(route.source_id).to eq(namespace.id) expect(route.source_id).to eq(namespace.id)
expect(route.path).to eq("gitlab-#{namespace.id}") expect(route.path).to eq("gitlab-#{namespace.id}")
......
...@@ -89,11 +89,11 @@ describe MigrateAutoDevOpsDomainToClusterDomain do ...@@ -89,11 +89,11 @@ describe MigrateAutoDevOpsDomainToClusterDomain do
end end
def find_cluster_project(project_id) def find_cluster_project(project_id)
cluster_projects_table.where(project_id: project_id).first cluster_projects_table.find_by(project_id: project_id)
end end
def find_cluster(cluster_id) def find_cluster(cluster_id)
clusters_table.where(id: cluster_id).first clusters_table.find_by(id: cluster_id)
end end
def project_auto_devops_with_domain def project_auto_devops_with_domain
......
...@@ -18,16 +18,16 @@ describe NullifyUsersRole do ...@@ -18,16 +18,16 @@ describe NullifyUsersRole do
it 'nullifies the role of the user with updated_at < 2019-11-05 12:08:00 and a role of 0' do it 'nullifies the role of the user with updated_at < 2019-11-05 12:08:00 and a role of 0' do
expect(users.where(role: nil).count).to eq(1) expect(users.where(role: nil).count).to eq(1)
expect(users.where(role: nil).first.email).to eq('1') expect(users.find_by(role: nil).email).to eq('1')
end end
it 'leaves the user with role of 1' do it 'leaves the user with role of 1' do
expect(users.where(role: 1).count).to eq(1) expect(users.where(role: 1).count).to eq(1)
expect(users.where(role: 1).first.email).to eq('2') expect(users.find_by(role: 1).email).to eq('2')
end end
it 'leaves the user with updated_at > 2019-11-05 12:08:00' do it 'leaves the user with updated_at > 2019-11-05 12:08:00' do
expect(users.where(role: 0).count).to eq(1) expect(users.where(role: 0).count).to eq(1)
expect(users.where(role: 0).first.email).to eq('3') expect(users.find_by(role: 0).email).to eq('3')
end end
end end
...@@ -39,9 +39,9 @@ describe ScheduleToArchiveLegacyTraces do ...@@ -39,9 +39,9 @@ describe ScheduleToArchiveLegacyTraces do
expect(File.exist?(legacy_trace_path(@build_failed))).to be_falsy expect(File.exist?(legacy_trace_path(@build_failed))).to be_falsy
expect(File.exist?(legacy_trace_path(@builds_canceled))).to be_falsy expect(File.exist?(legacy_trace_path(@builds_canceled))).to be_falsy
expect(File.exist?(legacy_trace_path(@build_running))).to be_truthy expect(File.exist?(legacy_trace_path(@build_running))).to be_truthy
expect(File.exist?(archived_trace_path(job_artifacts.where(job_id: @build_success.id).first))).to be_truthy expect(File.exist?(archived_trace_path(job_artifacts.find_by(job_id: @build_success.id)))).to be_truthy
expect(File.exist?(archived_trace_path(job_artifacts.where(job_id: @build_failed.id).first))).to be_truthy expect(File.exist?(archived_trace_path(job_artifacts.find_by(job_id: @build_failed.id)))).to be_truthy
expect(File.exist?(archived_trace_path(job_artifacts.where(job_id: @builds_canceled.id).first))).to be_truthy expect(File.exist?(archived_trace_path(job_artifacts.find_by(job_id: @builds_canceled.id)))).to be_truthy
expect(job_artifacts.where(job_id: @build_running.id)).not_to be_exist expect(job_artifacts.where(job_id: @build_running.id)).not_to be_exist
end end
end end
...@@ -23,9 +23,9 @@ describe API::Jobs do ...@@ -23,9 +23,9 @@ describe API::Jobs do
json_job['artifacts'].each do |artifact| json_job['artifacts'].each do |artifact|
expect(artifact).not_to be_nil expect(artifact).not_to be_nil
file_type = Ci::JobArtifact.file_types[artifact['file_type']] file_type = Ci::JobArtifact.file_types[artifact['file_type']]
expect(artifact['size']).to eq(second_job.job_artifacts.where(file_type: file_type).first.size) expect(artifact['size']).to eq(second_job.job_artifacts.find_by(file_type: file_type).size)
expect(artifact['filename']).to eq(second_job.job_artifacts.where(file_type: file_type).first.filename) expect(artifact['filename']).to eq(second_job.job_artifacts.find_by(file_type: file_type).filename)
expect(artifact['file_format']).to eq(second_job.job_artifacts.where(file_type: file_type).first.file_format) expect(artifact['file_format']).to eq(second_job.job_artifacts.find_by(file_type: file_type).file_format)
end end
end end
end end
......
...@@ -16,7 +16,7 @@ describe Emails::CreateService do ...@@ -16,7 +16,7 @@ describe Emails::CreateService do
it 'creates an email with additional attributes' do it 'creates an email with additional attributes' do
expect { service.execute(confirmation_token: 'abc') }.to change { Email.count }.by(1) expect { service.execute(confirmation_token: 'abc') }.to change { Email.count }.by(1)
expect(Email.where(opts).first.confirmation_token).to eq 'abc' expect(Email.find_by(opts).confirmation_token).to eq 'abc'
end end
it 'has the right user association' do it 'has the right user association' do
......
...@@ -100,7 +100,7 @@ describe Issues::MoveService do ...@@ -100,7 +100,7 @@ describe Issues::MoveService do
context 'when issue has notes with mentions' do context 'when issue has notes with mentions' do
it 'saves user mentions with actual mentions for new issue' do it 'saves user mentions with actual mentions for new issue' do
expect(new_issue.user_mentions.where(note_id: nil).first.mentioned_users_ids).to match_array([user.id]) expect(new_issue.user_mentions.find_by(note_id: nil).mentioned_users_ids).to match_array([user.id])
expect(new_issue.user_mentions.where.not(note_id: nil).first.mentioned_users_ids).to match_array([user.id]) expect(new_issue.user_mentions.where.not(note_id: nil).first.mentioned_users_ids).to match_array([user.id])
expect(new_issue.user_mentions.where.not(note_id: nil).count).to eq 1 expect(new_issue.user_mentions.where.not(note_id: nil).count).to eq 1
expect(new_issue.user_mentions.count).to eq 2 expect(new_issue.user_mentions.count).to eq 2
......
...@@ -321,9 +321,9 @@ describe Projects::ForkService do ...@@ -321,9 +321,9 @@ describe Projects::ForkService do
Projects::UpdateRepositoryStorageService.new(project).execute('test_second_storage') Projects::UpdateRepositoryStorageService.new(project).execute('test_second_storage')
fork_after_move = fork_project(project) fork_after_move = fork_project(project)
pool_repository_before_move = PoolRepository.joins(:shard) pool_repository_before_move = PoolRepository.joins(:shard)
.where(source_project: project, shards: { name: 'default' }).first .find_by(source_project: project, shards: { name: 'default' })
pool_repository_after_move = PoolRepository.joins(:shard) pool_repository_after_move = PoolRepository.joins(:shard)
.where(source_project: project, shards: { name: 'test_second_storage' }).first .find_by(source_project: project, shards: { name: 'test_second_storage' })
expect(fork_before_move.pool_repository).to eq(pool_repository_before_move) expect(fork_before_move.pool_repository).to eq(pool_repository_before_move)
expect(fork_after_move.pool_repository).to eq(pool_repository_after_move) expect(fork_after_move.pool_repository).to eq(pool_repository_after_move)
......
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