Skip to content

Commit

Permalink
resolve specs
Browse files Browse the repository at this point in the history
[RAILS-6]
  • Loading branch information
maneyko committed Aug 8, 2021
1 parent 70c9f04 commit 1a37ee3
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.6
2.6.6
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a37ee3

Please sign in to comment.