Skip to content

Respect config.active_record.permanent_connection_checkout #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# Display versions.
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
::ActiveRecord::Base.connection.adapter_name}"
::ActiveRecord::Base.adapter_class::ADAPTER_NAME}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opted to use this constant even where we could lease a connection (i.e., here and tests) for consistency.

line = '=' * message.length
puts line, message, line

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# Display versions.
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
::ActiveRecord::Base.connection.adapter_name}"
::ActiveRecord::Base.adapter_class::ADAPTER_NAME}"
line = '=' * message.length
puts line, message, line

Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def type_for(attr)
relation = attr.arel_attribute.relation
name = attr.arel_attribute.name.to_s
table = relation.respond_to?(:table_name) ? relation.table_name : relation.name
schema_cache = self.klass.connection.schema_cache
schema_cache = self.klass.connection_pool.schema_cache
unless schema_cache.send(:data_source_exists?, table)
raise "No table named #{table} exists."
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module Constants
module_function
# replace % \ to \% \\
def escape_wildcards(unescaped)
case ActiveRecord::Base.connection.adapter_name
case ActiveRecord::Base.adapter_class::ADAPTER_NAME
when "Mysql2".freeze
# Necessary for MySQL
unescaped.to_s.gsub(/([\\%_])/, '\\\\\\1')
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def arel_predicate_for_attribute(attr)
end

def attr_value_for_attribute(attr)
return attr.attr if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
return attr.attr if ActiveRecord::Base.adapter_class::ADAPTER_NAME == "PostgreSQL"

predicate.case_insensitive ? attr.attr.lower : attr.attr
rescue
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/ransack_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module RansackHelper
def quote_table_name(table)
ActiveRecord::Base.connection.quote_table_name(table)
ActiveRecord::Base.lease_connection.quote_table_name(table)
end

def quote_column_name(column)
ActiveRecord::Base.connection.quote_column_name(column)
ActiveRecord::Base.lease_connection.quote_column_name(column)
end
end
4 changes: 2 additions & 2 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ module ActiveRecord
describe '#ransacker' do
# For infix tests
def self.sane_adapter?
case ::ActiveRecord::Base.connection.adapter_name
case ::ActiveRecord::Base.adapter_class::ADAPTER_NAME
when 'SQLite3', 'PostgreSQL'
true
else
Expand Down Expand Up @@ -379,7 +379,7 @@ def self.sane_adapter?
context 'searching by underscores' do
# when escaping is supported right in LIKE expression without adding extra expressions
def self.simple_escaping?
case ::ActiveRecord::Base.connection.adapter_name
case ::ActiveRecord::Base.adapter_class::ADAPTER_NAME
when 'Mysql2', 'PostgreSQL'
true
else
Expand Down
2 changes: 1 addition & 1 deletion spec/ransack/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ module Ransack
end
end

it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
it "PG's sort option", if: ::ActiveRecord::Base.adapter_class::ADAPTER_NAME == "PostgreSQL" do
default = Ransack.options.clone

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_first }
Expand Down
24 changes: 12 additions & 12 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module Ransack

describe 'cont' do
it_has_behavior 'wildcard escaping', :name_cont,
(case ActiveRecord::Base.connection.adapter_name
(case ActiveRecord::Base.adapter_class::ADAPTER_NAME
when "PostGIS", "PostgreSQL"
/"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
when "Mysql2"
Expand All @@ -177,7 +177,7 @@ module Ransack

describe 'not_cont' do
it_has_behavior 'wildcard escaping', :name_not_cont,
(case ActiveRecord::Base.connection.adapter_name
(case ActiveRecord::Base.adapter_class::ADAPTER_NAME
when "PostGIS", "PostgreSQL"
/"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
when "Mysql2"
Expand All @@ -197,7 +197,7 @@ module Ransack

describe 'i_cont' do
it_has_behavior 'wildcard escaping', :name_i_cont,
(case ActiveRecord::Base.connection.adapter_name
(case ActiveRecord::Base.adapter_class::ADAPTER_NAME
when "PostGIS"
/LOWER\("people"."name"\) ILIKE '%\\%\\.\\_\\\\%'/
when "PostgreSQL"
Expand All @@ -219,7 +219,7 @@ module Ransack

describe 'not_i_cont' do
it_has_behavior 'wildcard escaping', :name_not_i_cont,
(case ActiveRecord::Base.connection.adapter_name
(case ActiveRecord::Base.adapter_class::ADAPTER_NAME
when "PostGIS"
/LOWER\("people"."name"\) NOT ILIKE '%\\%\\.\\_\\\\%'/
when "PostgreSQL"
Expand Down Expand Up @@ -324,14 +324,14 @@ module Ransack
@s.awesome_true = true
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} = #{
ActiveRecord::Base.connection.quoted_true}/
ActiveRecord::Base.lease_connection.quoted_true}/
end

it 'generates an inequality condition for boolean true' do
@s.awesome_true = false
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} != #{
ActiveRecord::Base.connection.quoted_true}/
ActiveRecord::Base.lease_connection.quoted_true}/
end
end

Expand All @@ -340,14 +340,14 @@ module Ransack
@s.awesome_not_true = true
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} != #{
ActiveRecord::Base.connection.quoted_true}/
ActiveRecord::Base.lease_connection.quoted_true}/
end

it 'generates an equality condition for boolean true' do
@s.awesome_not_true = false
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} = #{
ActiveRecord::Base.connection.quoted_true}/
ActiveRecord::Base.lease_connection.quoted_true}/
end
end

Expand All @@ -356,14 +356,14 @@ module Ransack
@s.awesome_false = true
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} = #{
ActiveRecord::Base.connection.quoted_false}/
ActiveRecord::Base.lease_connection.quoted_false}/
end

it 'generates an inequality condition for boolean false' do
@s.awesome_false = false
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} != #{
ActiveRecord::Base.connection.quoted_false}/
ActiveRecord::Base.lease_connection.quoted_false}/
end
end

Expand All @@ -372,14 +372,14 @@ module Ransack
@s.awesome_not_false = true
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} != #{
ActiveRecord::Base.connection.quoted_false}/
ActiveRecord::Base.lease_connection.quoted_false}/
end

it 'generates an equality condition for boolean false' do
@s.awesome_not_false = false
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
expect(@s.result.to_sql).to match /#{field} = #{
ActiveRecord::Base.connection.quoted_false}/
ActiveRecord::Base.lease_connection.quoted_false}/
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def remove_quotes_and_backticks(str)
end.to raise_error(Ransack::InvalidSearchError, "Invalid argument (Integer) supplied to sorts=")
end

it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
it "PG's sort option", if: ::ActiveRecord::Base.adapter_class::ADAPTER_NAME == "PostgreSQL" do
default = Ransack.options.clone

s = Search.new(Person, s: 'name asc')
Expand All @@ -687,7 +687,7 @@ def remove_quotes_and_backticks(str)
Ransack.options = default
end

it "PG's sort option with double name", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
it "PG's sort option with double name", if: ::ActiveRecord::Base.adapter_class::ADAPTER_NAME == "PostgreSQL" do
default = Ransack.options.clone

s = Search.new(Person, s: 'doubled_name asc')
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

config.before(:suite) do
message = "Running Ransack specs with #{
ActiveRecord::Base.connection.adapter_name
ActiveRecord::Base.adapter_class::ADAPTER_NAME
}, Active Record #{::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION
} and Ruby #{RUBY_VERSION}"
line = '=' * message.length
Expand Down
4 changes: 3 additions & 1 deletion spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
)
end

ActiveRecord.permanent_connection_checkout = :disallowed

# This is just a test app with no sensitive data, so we explicitly allowlist all
# attributes and associations for search. In general, end users should
# explicitly authorize each model, but this shows a way to configure the
Expand Down Expand Up @@ -382,7 +384,7 @@ class OperationHistory < Base
module Schema
def self.create
s = ::ActiveRecord::Schema.new
s.instance_variable_set(:@connection, SubDB::Base.connection)
s.instance_variable_set(:@connection, SubDB::Base.lease_connection)
s.verbose = false
s.define({}) do
create_table :operation_histories, force: true do |t|
Expand Down