Skip to content

Commit

Permalink
Use websearch_to_tsquery or (plainto_tsquery for Postgres < v11) …
Browse files Browse the repository at this point in the history
…for Dashboard search filter (#488)
  • Loading branch information
bensheldon committed Jan 19, 2022
1 parent b36a011 commit d142b4b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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

0 comments on commit d142b4b

Please sign in to comment.