diff --git a/.ruby-version b/.ruby-version index 7bf4b6a..338a5b5 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.4.6 +2.6.6 diff --git a/Gemfile b/Gemfile index 9891d66..efc020e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source "https://rubygems.org" # To test against different rails versions with TravisCI -rails_version = ENV["RAILS_VERSION"] || "~> 5.2.3" +rails_version = ENV["RAILS_VERSION"] || "~> 6" # NOTE: This is a Gemfile for a gem. # Using "platforms" is contraindicated because they won't make it into @@ -12,8 +12,8 @@ version2x = (RUBY_VERSION =~ /^2\.\d/) # https://github.com/ged/ruby-pg/blob/master/History.rdoc # https://bitbucket.org/ged/ruby-pg/wiki/Home -# pg >= 1.0.0 doesn't work with Rails at the moment. It's a Rails bug. gem "pg" +gem "psych", "~> 3" gem "railties", rails_version gem "activemodel", rails_version 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 140660f..4321645 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 @@ -152,7 +152,7 @@ def add_index(table_name, column_name, options = {}) # -- zekefast 2012-09-25 if creation_method.present? && index_exists?(table_name, column_name, options) raise ::PgSaurus::IndexExistsError, - "Index #{index_name} for `#{table_name}.#{column_name}` " \ + "Index #{index.name} for `#{table_name}.#{column_name}` " \ "column can not be created concurrently, because such index already exists." end diff --git a/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb b/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb index 59347b6..1a5bc26 100644 --- a/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb +++ b/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb @@ -5,14 +5,15 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::TranslateException INSUFFICIENT_PRIVILEGE = "42501" # Intercept insufficient privilege PG::Error and raise active_record wrapped database exception - def translate_exception(exception, message) + def translate_exception(exception, message:, sql:, binds:) + return exception unless exception.respond_to?(:result) exception_result = exception.result case exception_result.try(:error_field, PG::Result::PG_DIAG_SQLSTATE) when INSUFFICIENT_PRIVILEGE exc_message = exception_result.try(:error_field, PG::Result::PG_DIAG_MESSAGE_PRIMARY) exc_message ||= message - ::ActiveRecord::InsufficientPrivilege.new(exc_message) + ::ActiveRecord::InsufficientPrivilege.new(exc_message, sql: sql, binds: binds) else super end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 408293e..61cf01d 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -2,11 +2,11 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. 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..4b486e8 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 @@ -4,14 +4,14 @@ describe '#add_index' do context "concurrently creates index" do let(:expected_query) do - 'CREATE INDEX CONCURRENTLY "index_users_on_phone_number" ON "users" ("phone_number")' + ["CREATE", "INDEX", "CONCURRENTLY", %("index_users_on_phone_number"), "ON", %("users"), %{("phone_number")}] end it 'concurrently creates index' do ActiveRecord::Migration.clear_queue expect(ActiveRecord::Base.connection).to receive(:execute) do |query| - query.should == expected_query + query.split(" ").should == expected_query end ActiveRecord::Migration.add_index :users, :phone_number, concurrently: true @@ -21,14 +21,14 @@ context "creates index for column with operator" do let(:expected_query) do - 'CREATE INDEX "index_users_on_phone_number_varchar_pattern_ops" ON "users" (phone_number varchar_pattern_ops)' + ["CREATE", "INDEX", %("index_users_on_phone_number_varchar_pattern_ops"), "ON", %("users"), %{(phone_number}, %{varchar_pattern_ops)}] end it 'creates index for column with operator' do ActiveRecord::Migration.clear_queue expect(ActiveRecord::Base.connection).to receive(:execute) do |query| - query.should == expected_query + query.split(" ").should == expected_query end ActiveRecord::Migration.add_index :users, "phone_number varchar_pattern_ops" @@ -38,15 +38,15 @@ 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)' + ["CREATE", "INDEX", %("index_users_on_lower_first_name_desc_nulls_last"), "ON", %("users"), + %{(lower(first_name)}, "DESC", "NULLS", "LAST)"] end it 'creates functional index for column with longer operator string' do ActiveRecord::Migration.clear_queue expect(ActiveRecord::Base.connection).to receive(:execute) do |query| - query.should == expected_query + query.split(" ").should == expected_query end ActiveRecord::Migration.add_index :users, "lower(first_name) DESC NULLS LAST" diff --git a/spec/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception_spec.rb b/spec/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception_spec.rb index 4e77222..29bb1bc 100644 --- a/spec/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception_spec.rb +++ b/spec/lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception_spec.rb @@ -15,7 +15,7 @@ end end - translated = connection.send(:translate_exception, exception, "") + translated = connection.send(:translate_exception, exception, message: "", sql: "", binds: []) expect(translated).to be_an_instance_of(ActiveRecord::InsufficientPrivilege) end end