diff --git a/lib/admin_user_index_query.rb b/lib/admin_user_index_query.rb index 6572fa2ccf9..7f3c4fca62c 100644 --- a/lib/admin_user_index_query.rb +++ b/lib/admin_user_index_query.rb @@ -114,6 +114,10 @@ class AdminUserIndexQuery end def filter_by_search + if params[:email].present? + return @query.where('user_emails.email = ?', params[:email].downcase) + end + filter = params[:filter] if filter.present? filter.strip! diff --git a/spec/components/admin_user_index_query_spec.rb b/spec/components/admin_user_index_query_spec.rb index 4a11b97008d..284f6389ac9 100644 --- a/spec/components/admin_user_index_query_spec.rb +++ b/spec/components/admin_user_index_query_spec.rb @@ -188,6 +188,20 @@ describe AdminUserIndexQuery do expect(query.to_sql.downcase).not_to include("ilike") end + + it "can correctly bypass expensive ilike query" do + user = Fabricate(:user, email: 'sam2@Sam.com') + + query = AdminUserIndexQuery.new(email: 'Sam@sam.com').find_users_query + expect(query.count).to eq(0) + expect(query.to_sql.downcase).not_to include("ilike") + + query = AdminUserIndexQuery.new(email: 'Sam2@sam.com').find_users_query + expect(query.first.id).to eq(user.id) + expect(query.count).to eq(1) + expect(query.to_sql.downcase).not_to include("ilike") + + end end context "by email fragment" do