mirror of https://github.com/rails/rails
Merge pull request #51353 from Shopify/get-rid-lease-connection
Eliminate remaining uses of `lease_connection` inside Active Record
This commit is contained in:
commit
7c68c5210c
|
@ -37,6 +37,7 @@ module ActiveRecord
|
|||
chain << [reflection, table]
|
||||
end
|
||||
|
||||
base_klass.with_connection do |connection|
|
||||
# The chain starts with the target table, but we want to end with it here (makes
|
||||
# more sense in this context), so we reverse
|
||||
chain.reverse_each do |reflection, table|
|
||||
|
@ -65,12 +66,13 @@ module ActiveRecord
|
|||
|
||||
if others && !others.empty?
|
||||
joins.concat arel.join_sources
|
||||
append_constraints(joins.last, others)
|
||||
append_constraints(connection, joins.last, others)
|
||||
end
|
||||
|
||||
# The current table in this iteration becomes the foreign table in the next
|
||||
foreign_table, foreign_klass = table, klass
|
||||
end
|
||||
end
|
||||
|
||||
joins
|
||||
end
|
||||
|
@ -88,10 +90,10 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
private
|
||||
def append_constraints(join, constraints)
|
||||
def append_constraints(connection, join, constraints)
|
||||
if join.is_a?(Arel::Nodes::StringJoin)
|
||||
join_string = Arel::Nodes::And.new(constraints.unshift join.left)
|
||||
join.left = Arel.sql(base_klass.lease_connection.visitor.compile(join_string))
|
||||
join.left = Arel.sql(connection.visitor.compile(join_string))
|
||||
else
|
||||
right = join.right
|
||||
right.expr = Arel::Nodes::And.new(constraints.unshift right.expr)
|
||||
|
|
|
@ -178,7 +178,10 @@ module ActiveRecord
|
|||
def can_use_fast_cache_version?(timestamp)
|
||||
timestamp.is_a?(String) &&
|
||||
cache_timestamp_format == :usec &&
|
||||
self.class.lease_connection.default_timezone == :utc &&
|
||||
# FIXME: checking out a connection for this is wasteful
|
||||
# we should store/cache this information in the schema cache
|
||||
# or similar.
|
||||
self.class.with_connection(&:default_timezone) == :utc &&
|
||||
!updated_at_came_from_user?
|
||||
end
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ module ActiveRecord
|
|||
arel_table = arel_table.alias(table_name) if arel_table.name != table_name
|
||||
TableMetadata.new(association_klass, arel_table, reflection)
|
||||
else
|
||||
type_caster = TypeCaster::Connection.new(klass, table_name)
|
||||
type_caster = TypeCaster::Connection.new(klass)
|
||||
arel_table = Arel::Table.new(table_name, type_caster: type_caster)
|
||||
TableMetadata.new(nil, arel_table, reflection)
|
||||
end
|
||||
|
|
|
@ -393,8 +393,8 @@ module ActiveRecord
|
|||
# This method is available within the context of an ActiveRecord::Base
|
||||
# instance.
|
||||
def with_transaction_returning_status
|
||||
self.class.with_connection do |connection|
|
||||
status = nil
|
||||
connection = self.class.lease_connection
|
||||
ensure_finalize = !connection.transaction_open?
|
||||
|
||||
connection.transaction do
|
||||
|
@ -406,6 +406,7 @@ module ActiveRecord
|
|||
end
|
||||
status
|
||||
end
|
||||
end
|
||||
|
||||
def trigger_transactional_callbacks? # :nodoc:
|
||||
(@_new_record_before_last_commit || _trigger_update_callback) && persisted? ||
|
||||
|
@ -496,7 +497,9 @@ module ActiveRecord
|
|||
# Add the record to the current transaction so that the #after_rollback and #after_commit
|
||||
# callbacks can be called.
|
||||
def add_to_transaction(ensure_finalize = true)
|
||||
self.class.lease_connection.add_transaction_record(self, ensure_finalize)
|
||||
self.class.with_connection do |connection|
|
||||
connection.add_transaction_record(self, ensure_finalize)
|
||||
end
|
||||
end
|
||||
|
||||
def has_transactional_callbacks?
|
||||
|
|
|
@ -3,9 +3,8 @@
|
|||
module ActiveRecord
|
||||
module TypeCaster
|
||||
class Connection # :nodoc:
|
||||
def initialize(klass, table_name)
|
||||
def initialize(klass)
|
||||
@klass = klass
|
||||
@table_name = table_name
|
||||
end
|
||||
|
||||
def type_cast_for_database(attr_name, value)
|
||||
|
@ -14,20 +13,8 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def type_for_attribute(attr_name)
|
||||
schema_cache = @klass.schema_cache
|
||||
|
||||
if schema_cache.data_source_exists?(table_name)
|
||||
column = schema_cache.columns_hash(table_name)[attr_name.to_s]
|
||||
if column
|
||||
type = @klass.lease_connection.lookup_cast_type_from_column(column)
|
||||
end
|
||||
end
|
||||
|
||||
type || Type.default_value
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :table_name
|
||||
@klass.type_for_attribute(attr_name) || Type.default_value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -110,16 +110,20 @@ module ActiveRecord
|
|||
|
||||
def build_relation(klass, attribute, value)
|
||||
relation = klass.unscoped
|
||||
comparison = relation.bind_attribute(attribute, value) do |attr, bind|
|
||||
# TODO: Add case-sensitive / case-insensitive operators to Arel
|
||||
# to no longer need to checkout a connection here.
|
||||
comparison = klass.with_connection do |connection|
|
||||
relation.bind_attribute(attribute, value) do |attr, bind|
|
||||
return relation.none! if bind.unboundable?
|
||||
|
||||
if !options.key?(:case_sensitive) || bind.nil?
|
||||
klass.lease_connection.default_uniqueness_comparison(attr, bind)
|
||||
connection.default_uniqueness_comparison(attr, bind)
|
||||
elsif options[:case_sensitive]
|
||||
klass.lease_connection.case_sensitive_comparison(attr, bind)
|
||||
connection.case_sensitive_comparison(attr, bind)
|
||||
else
|
||||
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
||||
klass.lease_connection.case_insensitive_comparison(attr, bind)
|
||||
connection.case_insensitive_comparison(attr, bind)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -147,8 +147,9 @@ module Arel # :nodoc: all
|
|||
# Maybe we should just use `Table.engine`? :'(
|
||||
def to_sql(engine = Table.engine)
|
||||
collector = Arel::Collectors::SQLString.new
|
||||
collector = engine.lease_connection.visitor.accept self, collector
|
||||
collector.value
|
||||
engine.with_connection do |connection|
|
||||
connection.visitor.accept(self, collector).value
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_attribute
|
||||
|
|
|
@ -52,8 +52,9 @@ module Arel # :nodoc: all
|
|||
|
||||
def to_sql(engine = Table.engine)
|
||||
collector = Arel::Collectors::SQLString.new
|
||||
collector = engine.lease_connection.visitor.accept @ast, collector
|
||||
collector.value
|
||||
engine.with_connection do |connection|
|
||||
engine.lease_connection.visitor.accept(@ast, collector).value
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_copy(other)
|
||||
|
|
|
@ -129,6 +129,10 @@ module FakeRecord
|
|||
@connection_pool = ConnectionPool.new
|
||||
end
|
||||
|
||||
def with_connection(...)
|
||||
connection_pool.with_connection(...)
|
||||
end
|
||||
|
||||
def lease_connection
|
||||
connection_pool.lease_connection
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue