Skip to content

Commit

Permalink
PostgreSQL 8.3 and earlier do not support :prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
nertzy committed Oct 22, 2011
1 parent ae64dd5 commit e5d721e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
6 changes: 6 additions & 0 deletions lib/pg_search/features/tsearch.rb
Expand Up @@ -11,6 +11,12 @@ def initialize(query, options, columns, model, normalizer)
@model = model
@columns = columns
@normalizer = normalizer

if @options[:prefix] && @model.connection.send(:postgresql_version) < 80400
raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
Sorry, {:using => {:tsearch => {:prefix => true}}} only works in PostgreSQL 8.4 and above.")
MESSAGE
end
end

def conditions
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_search/normalizer.rb
Expand Up @@ -8,7 +8,7 @@ def add_normalization(original_sql)
normalized_sql = original_sql
if @config.ignore.include?(:accents)
if @config.postgresql_version < 90000
raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE)
raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
Sorry, {:ignoring => :accents} only works in PostgreSQL 9.0 and above.
#{@config.inspect}
MESSAGE
Expand Down
64 changes: 36 additions & 28 deletions spec/pg_search_spec.rb
Expand Up @@ -316,36 +316,44 @@
end

context "using tsearch" do
context "with :prefix => true" do
before do
ModelWithPgSearch.class_eval do
pg_search_scope :search_title_with_prefixes,
:against => :title,
:using => {
:tsearch => {:prefix => true}
}
end
before do
ModelWithPgSearch.class_eval do
pg_search_scope :search_title_with_prefixes,
:against => :title,
:using => {
:tsearch => {:prefix => true}
}
end
end

it "returns rows that match the query and that are prefixed by the query" do
included = ModelWithPgSearch.create!(:title => 'prefix')
excluded = ModelWithPgSearch.create!(:title => 'postfix')

results = ModelWithPgSearch.search_title_with_prefixes("pre")
results.should == [included]
results.should_not include(excluded)
if ActiveRecord::Base.connection.send(:postgresql_version) < 80400
it "is unsupported in PostgreSQL 8.3 and earlier" do
lambda do
ModelWithPgSearch.search_title_with_prefixes("abcd\303\251f")
end.should raise_exception(PgSearch::NotSupportedForPostgresqlVersion)
end
else
context "with :prefix => true" do
it "returns rows that match the query and that are prefixed by the query" do
included = ModelWithPgSearch.create!(:title => 'prefix')
excluded = ModelWithPgSearch.create!(:title => 'postfix')

results = ModelWithPgSearch.search_title_with_prefixes("pre")
results.should == [included]
results.should_not include(excluded)
end

it "returns rows that match the query when the query has a hyphen" do
included = [
ModelWithPgSearch.create!(:title => 'foo bar'),
ModelWithPgSearch.create!(:title => 'foo-bar')
]
excluded = ModelWithPgSearch.create!(:title => 'baz quux')
it "returns rows that match the query when the query has a hyphen" do
included = [
ModelWithPgSearch.create!(:title => 'foo bar'),
ModelWithPgSearch.create!(:title => 'foo-bar')
]
excluded = ModelWithPgSearch.create!(:title => 'baz quux')

results = ModelWithPgSearch.search_title_with_prefixes("foo-bar")
results.should =~ included
results.should_not include(excluded)
results = ModelWithPgSearch.search_title_with_prefixes("foo-bar")
results.should =~ included
results.should_not include(excluded)
end
end
end

Expand Down Expand Up @@ -536,15 +544,15 @@
pg_search_scope :with_tsearch,
:against => :title,
:using => [
[:tsearch, {:prefix => true}]
[:tsearch, {:dictionary => 'english'}]
]

pg_search_scope :with_trigram, :against => :title, :using => :trigram

pg_search_scope :with_tsearch_and_trigram_using_array,
:against => :title,
:using => [
[:tsearch, {:prefix => true}],
[:tsearch, {:dictionary => 'english'}],
:trigram
]

Expand All @@ -561,7 +569,7 @@
ModelWithPgSearch.with_tsearch_and_trigram_using_array(trigram_query).should == [record]

# matches tsearch only
tsearch_query = "til"
tsearch_query = "tiles"
ModelWithPgSearch.with_tsearch(tsearch_query).should include(record)
ModelWithPgSearch.with_trigram(tsearch_query).should_not include(record)
ModelWithPgSearch.with_tsearch_and_trigram_using_array(tsearch_query).should == [record]
Expand Down

0 comments on commit e5d721e

Please sign in to comment.