Skip to content

Commit

Permalink
bug fixes: don't dump :case_sensitive => false for index with an expr…
Browse files Browse the repository at this point in the history
…ession that includes 'lower(name)', and properly dump multi-column case_insensitive indexes.

Closes SchemaPlus#84
  • Loading branch information
ronen committed Jan 17, 2013
1 parent c3a4496 commit adbe3ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,16 @@ def indexes(table_name, name = nil) #:nodoc:
SQL

column_names = columns.values_at(*index_keys).compact
# extract column name from the expression, for a
# case-insensitive
if md = expression.try(:match, /^lower\(\(?([^)]+)\)?(::text)?\)$/i)
column_names << md[1]
case_sensitive = true

# extract column names from the expression, for a
# case-insensitive index
if expression
rexp_lower = %r{\blower\(\(?([^)]+)(\)::text)?\)}
if expression.match /^(#{rexp_lower}(, )?)+$/
column_names = expression.scan(rexp_lower).map(&:first)
case_sensitive = false
end
end

# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
Expand All @@ -158,7 +164,7 @@ def indexes(table_name, name = nil) #:nodoc:
:unique => unique,
:orders => orders,
:conditions => conditions,
:case_sensitive => !(expression =~ /lower/i),
:case_sensitive => case_sensitive,
:kind => kind.downcase == "btree" ? nil : kind,
:expression => expression)
end
Expand Down
13 changes: 10 additions & 3 deletions spec/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ class ::Comment < ActiveRecord::Base ; end
if SchemaPlusHelpers.postgresql?

it "should define case insensitive index" do
with_index Post, :name => "posts_user_body_index", :expression => "USING btree (LOWER(body))" do
dump_posts.should match(to_regexp(%q{t.index ["body"], :name => "posts_user_body_index", :case_sensitive => false}))
with_index Post, [:body, :string_no_default], :case_sensitive => false do
dump_posts.should match(to_regexp(%q{t.index ["body", "string_no_default"], :name => "index_posts_on_body_and_string_no_default", :case_sensitive => false}))
end
end

Expand All @@ -180,6 +180,13 @@ class ::Comment < ActiveRecord::Base ; end
end
end

it "should not define :case_sensitive => false with non-trivial expression" do
with_index Post, :name => "posts_user_body_index", :expression => "BTRIM(LOWER(body))" do
dump_posts.should match(%r{#{to_regexp(%q{t.index :name => "posts_user_body_index", :expression => "btrim(lower(body))"})}$})
end
end


it "should define kind" do
with_index Post, :name => "posts_body_index", :expression => "USING hash (body)" do
dump_posts.should match(to_regexp(%q{t.index ["body"], :name => "posts_body_index", :kind => "hash"}))
Expand Down Expand Up @@ -277,7 +284,7 @@ def with_index(model, columns, options = {})
def determine_index_name(model, columns, options)
name = columns[:name] if columns.is_a?(Hash)
name ||= options[:name]
name ||= model.indexes.detect { |index| index.table == model.table_name.to_s && index.columns == Array(columns).collect(&:to_s) }.name
name ||= model.indexes.detect { |index| index.table == model.table_name.to_s && index.columns.sort == Array(columns).collect(&:to_s).sort }.name
name
end

Expand Down

0 comments on commit adbe3ea

Please sign in to comment.