From c74eec1f6c528f278ca7b889e225a38e102343df Mon Sep 17 00:00:00 2001 From: Alexey Volosenko Date: Wed, 22 Jul 2020 17:19:51 +0300 Subject: [PATCH] Updated logic to create multi-functional index with add_index method. Update schema dumper to use new hash syntax for create_extension method. --- .gitignore | 2 ++ .../connection_adapters/postgresql/schema_statements.rb | 5 +++-- lib/pg_saurus/schema_dumper/extension_methods.rb | 4 ++-- spec/active_record/schema_dumper_spec.rb | 4 ++-- ...25645_add_functional_index_with_longer_operator_string.rb | 2 +- spec/dummy/db/schema.rb | 4 ++-- .../connection_adapters/abstract/schema_statements_spec.rb | 4 ++-- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 9e9d7d7..0ad67c4 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ spec/dummy/log/*.log spec/dummy/tmp/ .idea +.generators +.rakeTasks TAGS ctags diff --git a/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb b/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb index 5d97d13..e4d35d9 100644 --- a/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/lib/core_ext/active_record/connection_adapters/postgresql/schema_statements.rb @@ -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: @@ -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 @@ -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 diff --git a/lib/pg_saurus/schema_dumper/extension_methods.rb b/lib/pg_saurus/schema_dumper/extension_methods.rb index 0e97bc3..4911db2 100644 --- a/lib/pg_saurus/schema_dumper/extension_methods.rb +++ b/lib/pg_saurus/schema_dumper/extension_methods.rb @@ -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 diff --git a/spec/active_record/schema_dumper_spec.rb b/spec/active_record/schema_dumper_spec.rb index aad4087..902a170 100644 --- a/spec/active_record/schema_dumper_spec.rb +++ b/spec/active_record/schema_dumper_spec.rb @@ -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 diff --git a/spec/dummy/db/migrate/20190320025645_add_functional_index_with_longer_operator_string.rb b/spec/dummy/db/migrate/20190320025645_add_functional_index_with_longer_operator_string.rb index 908e39b..060b77e 100644 --- a/spec/dummy/db/migrate/20190320025645_add_functional_index_with_longer_operator_string.rb +++ b/spec/dummy/db/migrate/20190320025645_add_functional_index_with_longer_operator_string.rb @@ -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 diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 408293e..d76041f 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -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" diff --git a/spec/lib/core_ext/connection_adapters/abstract/schema_statements_spec.rb b/spec/lib/core_ext/connection_adapters/abstract/schema_statements_spec.rb index a7e0c9d..7f2319f 100644 --- a/spec/lib/core_ext/connection_adapters/abstract/schema_statements_spec.rb +++ b/spec/lib/core_ext/connection_adapters/abstract/schema_statements_spec.rb @@ -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 @@ -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