Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use websearch_to_tsquery or (plainto_tsquery for Postgres < v11) for Dashboard search filter #488

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/good_job/filterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ module Filterable
next if query.blank?

tsvector = "(to_tsvector('english', serialized_params) || to_tsvector('english', id::text) || to_tsvector('english', COALESCE(error, '')::text))"
where("#{tsvector} @@ to_tsquery(?)", query)
.order(sanitize_sql_for_order([Arel.sql("ts_rank(#{tsvector}, to_tsquery(?))"), query]) => 'DESC')
to_tsquery_function = database_supports_websearch_to_tsquery? ? 'websearch_to_tsquery' : 'plainto_tsquery'
where("#{tsvector} @@ #{to_tsquery_function}(?)", query)
.order(sanitize_sql_for_order([Arel.sql("ts_rank(#{tsvector}, #{to_tsquery_function}(?))"), query]) => 'DESC')
end)
end

class_methods do
def database_supports_websearch_to_tsquery?
return @_database_supports_websearch_to_tsquery if defined?(@_database_supports_websearch_to_tsquery)

@_database_supports_websearch_to_tsquery = connection.postgresql_version >= 110000
end
end
end
end
8 changes: 8 additions & 0 deletions spec/engine/filters/good_job/jobs_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
it 'returns a limited set of results' do
expect(filter.records.size).to eq 1
end

describe 'Ruby namespaced query' do
before { params[:query] = 'ExampleJob::DeadError' }

it 'returns a limited set of results' do
expect(filter.records.size).to eq 1
end
end
end
end

Expand Down
6 changes: 5 additions & 1 deletion spec/lib/good_job/filterable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

RSpec.describe GoodJob::Filterable do
let(:model_class) { GoodJob::Execution }
let!(:execution) { model_class.create(active_job_id: SecureRandom.uuid, queue_name: "default", serialized_params: { example_key: 'example_value' }, error: "ExampleError: a message") }
let!(:execution) { model_class.create(active_job_id: SecureRandom.uuid, queue_name: "default", serialized_params: { example_key: 'example_value' }, error: "ExampleJob::ExampleError: a message") }

describe '.search_test' do
it 'searches serialized params' do
Expand All @@ -18,6 +18,10 @@
expect(model_class.search_text('ExampleError')).to include(execution)
end

it 'searches strings with colons' do
expect(model_class.search_text('ExampleJob::ExampleError')).to include(execution)
end

it 'filters out non-matching records' do
expect(model_class.search_text('ghost')).to be_empty
end
Expand Down