Skip to content

Commit

Permalink
Updated logic to create multi-functional index with add_index method.
Browse files Browse the repository at this point in the history
Update schema dumper to use new hash syntax for create_extension method.
  • Loading branch information
avolosenko committed Jul 22, 2020
1 parent 9fad77a commit c74eec1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ spec/dummy/log/*.log
spec/dummy/tmp/

.idea
.generators
.rakeTasks

TAGS
ctags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def tables(name = nil)
WHERE schemaname = ANY (ARRAY['public'])
SQL
end

# Returns an array of indexes for the given table.
#
# == Patch 1:
Expand Down Expand Up @@ -173,6 +173,7 @@ def add_index(table_name, column_name, options = {})
statements << index_options if index_options.present?

sql = statements.join(' ')

execute(sql)
end

Expand Down Expand Up @@ -250,7 +251,7 @@ def quoted_columns_for_index(column_names, **options)
column_name, operator_name = split_column_name(name)

result_name = if column_name =~ FUNCTIONAL_INDEX_REGEXP
_name = "#{$1}(#{$2}#{quote_column_name($3)})"
_name = column_name.gsub(/\b#{$3}\b/, quote_column_name($3))
_name += " #{operator_name}"
_name
else
Expand Down
4 changes: 2 additions & 2 deletions lib/pg_saurus/schema_dumper/extension_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def dump_extensions(stream)
extensions = @connection.pg_extensions
commands = extensions.map do |extension_name, options|
result = [%Q|create_extension "#{extension_name}"|]
result << %Q|:schema_name => "#{options[:schema_name]}"| unless options[:schema_name] == 'public'
result << %Q|:version => "#{options[:version]}"|
result << %Q|schema_name: "#{options[:schema_name]}"| unless options[:schema_name] == 'public'
result << %Q|version: "#{options[:version]}"|
result.join(', ')
end

Expand Down
4 changes: 2 additions & 2 deletions spec/active_record/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@

context "Extensions" do
it 'dumps loaded extension modules' do
@dump.should =~ /create_extension "fuzzystrmatch", :version => "\d+\.\d+"/
@dump.should =~ /create_extension "btree_gist", :schema_name => "demography", :version => "\d+\.\d+"/
@dump.should =~ /create_extension "fuzzystrmatch", version: "\d+\.\d+"/
@dump.should =~ /create_extension "btree_gist", schema_name: "demography", version: "\d+\.\d+"/
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddFunctionalIndexWithLongerOperatorString < ActiveRecord::Migration[5.2]
def change
add_index :pets, ["lower(name) DESC NULLS LAST"]
add_index :pets, ["trim(lower(name)) DESC NULLS LAST"]
end
end
4 changes: 2 additions & 2 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
create_schema_if_not_exists "later"
create_schema_if_not_exists "latest"

create_extension "fuzzystrmatch", :version => "1.1"
create_extension "btree_gist", :schema_name => "demography", :version => "1.2"
create_extension "fuzzystrmatch", version: "1.1"
create_extension "btree_gist", schema_name: "demography", version: "1.2"

# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
context "for functional index with longer operator string" do
let(:expected_query) do
'CREATE INDEX "index_users_on_lower_first_name_desc_nulls_last" ' \
'ON "users" (lower(first_name) DESC NULLS LAST)'
'ON "users" (trim(lower(first_name)) DESC NULLS LAST)'
end

it 'creates functional index for column with longer operator string' do
Expand All @@ -49,7 +49,7 @@
query.should == expected_query
end

ActiveRecord::Migration.add_index :users, "lower(first_name) DESC NULLS LAST"
ActiveRecord::Migration.add_index :users, "trim(lower(first_name)) DESC NULLS LAST"
ActiveRecord::Migration.process_postponed_queries
end
end
Expand Down

0 comments on commit c74eec1

Please sign in to comment.