Skip to content
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

Refactored code to depend on connection handler's #with_connection inste... #5

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def scope
# actually gets built.
def association_scope
if klass
@association_scope ||= AssociationScope.scope(self, klass.connection)
@association_scope ||= klass.with_connection { |conn| AssociationScope.scope(self, conn) }
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def delete_records(records, method)
stmt.from scope.klass.arel_table
stmt.wheres = arel.constraints

count = scope.klass.connection.delete(stmt, 'SQL', scope.bind_values)
count = scope.klass.with_connection { |conn| conn.delete(stmt, 'SQL', scope.bind_values) }
end
when :nullify
count = scope.update_all(source_reflection.foreign_key => nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def self.walk_tree(associations, hash)
# joins # => []
#
def initialize(base, associations, joins)
@alias_tracker = AliasTracker.create(base.connection, joins)
@alias_tracker = ActiveRecord::Base.with_connection { |conn| AliasTracker.create(conn, joins) }
@alias_tracker.aliased_name_for(base.table_name, base.table_name) # Updates the count for base.table_name to 1
tree = self.class.make_tree associations
@join_root = JoinBase.new base, build(tree, base)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def associated_records_by_owner(preloader)
if owner_keys.any?
# Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
# Make several smaller queries if necessary or make one query if the adapter supports it
sliced = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size)
sliced = owner_keys.each_slice(klass.with_connection { |conn| conn.in_clause_length } || owner_keys.size)

records = load_slices sliced
records.each do |record, owner_key|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def primary_key
# Returns a quoted version of the primary key name, used to construct
# SQL statements.
def quoted_primary_key
@quoted_primary_key ||= connection.quote_column_name(primary_key)
@quoted_primary_key ||= with_connection { |conn| conn.quote_column_name(primary_key) }
end

def reset_primary_key #:nodoc:
Expand All @@ -90,7 +90,7 @@ def get_primary_key(base_name) #:nodoc:
base_name.foreign_key
else
if ActiveRecord::Base != self && table_exists?
connection.schema_cache.primary_keys(table_name)
with_connection { |conn| conn.schema_cache.primary_keys(table_name) }
else
'id'
end
Expand Down
14 changes: 11 additions & 3 deletions activerecord/lib/active_record/connection_adapters/schema_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def primary_keys(table_name)
def table_exists?(name)
return @tables[name] if @tables.key? name

@tables[name] = connection.table_exists?(name)
@tables[name] = with_connection { |conn| conn.table_exists?(name) }
end

# Add internal cache for table with +table_name+.
Expand Down Expand Up @@ -90,7 +90,7 @@ def marshal_load(array)

def prepare_default_proc
@columns.default_proc = Proc.new do |h, table_name|
h[table_name] = connection.columns(table_name)
h[table_name] = with_connection { |conn| conn.columns(table_name) }
end

@columns_hash.default_proc = Proc.new do |h, table_name|
Expand All @@ -100,7 +100,15 @@ def prepare_default_proc
end

@primary_keys.default_proc = Proc.new do |h, table_name|
h[table_name] = table_exists?(table_name) ? connection.primary_key(table_name) : nil
h[table_name] = table_exists?(table_name) ? with_connection { |conn| conn.primary_key(table_name) } : nil
end
end

def with_connection(&block)
if @connection
block.call @connection
else
ActiveRecord::Base.with_connection &block
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def connection
retrieve_connection
end

def with_connection(&block)
connection_pool.with_connection &block
end

def connection_id
ActiveRecord::RuntimeRegistry.connection_id
end
Expand Down Expand Up @@ -146,7 +150,7 @@ def remove_connection(klass = self)
end

def clear_cache! # :nodoc:
connection.schema_cache.clear!
with_connection { |conn| conn.schema_cache.clear! }
end

delegate :clear_active_connections!, :clear_reloadable_connections!,
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/counter_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def reset_counters(id, *counters)
stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
arel_table[counter_name] => object.send(association).count
}, primary_key)
connection.update stmt
with_connection { |conn| conn.update stmt }
end
return true
end
Expand Down Expand Up @@ -73,7 +73,7 @@ def reset_counters(id, *counters)
def update_counters(id, counters)
updates = counters.map do |counter_name, value|
operator = value < 0 ? '-' : '+'
quoted_column = connection.quote_column_name(counter_name)
quoted_column = with_connection { |conn| conn.quote_column_name(counter_name) }
"#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/explain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def exec_explain(queries) # :nodoc:
bind_msg = bind.map {|col, val| [col.name, val]}.inspect
msg.last << " #{bind_msg}"
end
msg << connection.explain(sql, bind)
msg << with_connection { |conn| conn.explain(sql, bind) }
end.join("\n")
end.join("\n")

Expand Down
62 changes: 32 additions & 30 deletions activerecord/lib/active_record/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,46 +504,48 @@ def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}
}

unless files_to_read.empty?
connection.disable_referential_integrity do
fixtures_map = {}

fixture_sets = files_to_read.map do |fs_name|
klass = class_names[fs_name]
conn = klass ? klass.connection : connection
fixtures_map[fs_name] = new( # ActiveRecord::FixtureSet.new
conn,
fs_name,
klass,
::File.join(fixtures_directory, fs_name))
end
ActiveRecord::Base.with_connection do |conn|
conn.disable_referential_integrity do
fixtures_map = {}

fixture_sets = files_to_read.map do |fs_name|
klass = class_names[fs_name]
klass_conn = klass ? klass.connection : conn
fixtures_map[fs_name] = new( # ActiveRecord::FixtureSet.new
klass_conn,
fs_name,
klass,
::File.join(fixtures_directory, fs_name))
end

all_loaded_fixtures.update(fixtures_map)
all_loaded_fixtures.update(fixtures_map)

connection.transaction(:requires_new => true) do
fixture_sets.each do |fs|
conn = fs.model_class.respond_to?(:connection) ? fs.model_class.connection : connection
table_rows = fs.table_rows
conn.transaction(:requires_new => true) do
fixture_sets.each do |fs|
model_conn = fs.model_class.respond_to?(:connection) ? fs.model_class.connection : conn
table_rows = fs.table_rows

table_rows.keys.each do |table|
conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete'
end
table_rows.keys.each do |table|
model_conn.delete "DELETE FROM #{model_conn.quote_table_name(table)}", 'Fixture Delete'
end

table_rows.each do |fixture_set_name, rows|
rows.each do |row|
conn.insert_fixture(row, fixture_set_name)
table_rows.each do |fixture_set_name, rows|
rows.each do |row|
model_conn.insert_fixture(row, fixture_set_name)
end
end
end
end

# Cap primary key sequences to max(pk).
if connection.respond_to?(:reset_pk_sequence!)
fixture_sets.each do |fs|
connection.reset_pk_sequence!(fs.table_name)
# Cap primary key sequences to max(pk).
if conn.respond_to?(:reset_pk_sequence!)
fixture_sets.each do |fs|
conn.reset_pk_sequence!(fs.table_name)
end
end
end
end

cache_fixtures(connection, fixtures_map)
cache_fixtures(conn, fixtures_map)
end
end
end
cached_fixtures(connection, fixture_set_names)
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/locking/optimistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def update_record(attribute_names = @attributes.keys) #:nodoc:
self.class.primary_key
)

affected_rows = self.class.connection.update stmt
affected_rows = self.class.with_connection { |conn| conn.update stmt }

unless affected_rows == 1
raise ActiveRecord::StaleObjectError.new(self, "update")
Expand Down Expand Up @@ -120,7 +120,7 @@ def relation_for_destroy
if locking_enabled?
column_name = self.class.locking_column
column = self.class.columns_hash[column_name]
substitute = self.class.connection.substitute_at(column, relation.bind_values.length)
substitute = self.class.with_connection { |conn| conn.substitute_at(column, relation.bind_values.length) }

relation = relation.where(self.class.arel_table[column_name].eq(substitute))
relation.bind_values << [column, self[column_name].to_i]
Expand Down Expand Up @@ -154,7 +154,7 @@ def locking_column
# Quote the column name used for optimistic locking.
def quoted_locking_column
ActiveSupport::Deprecation.warn "ActiveRecord::Base.quoted_locking_column is deprecated and will be removed in Rails 4.2 or later."
connection.quote_column_name(locking_column)
with_connection { |conn| conn.quote_column_name(locking_column) }
end

# Reset the column used for optimistic locking back to the +lock_version+ default.
Expand Down
30 changes: 23 additions & 7 deletions activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,14 @@ class << self
attr_accessor :delegate # :nodoc:
attr_accessor :disable_ddl_transaction # :nodoc:

def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)
def check_pending!(connection = nil)
if connection
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)
else
with_connection do |conn|
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(conn)
end
end
end

def load_schema_if_pending!
Expand Down Expand Up @@ -635,6 +641,14 @@ def connection
@connection || ActiveRecord::Base.connection
end

def with_connection(&block)
if @connection
block.call @connection
else
ActiveRecord::Base.with_connection &block
end
end

def method_missing(method, *arguments, &block)
arg_list = arguments.map{ |a| a.inspect } * ', '

Expand All @@ -645,8 +659,10 @@ def method_missing(method, *arguments, &block)
arguments[1] = proper_table_name(arguments.second, table_name_options) if method == :rename_table
end
end
return super unless connection.respond_to?(method)
connection.send(method, *arguments, &block)
with_connection do |conn|
return super unless conn.respond_to?(method)
conn.send(method, *arguments, &block)
end
end
end

Expand Down Expand Up @@ -907,7 +923,7 @@ def move(direction, migrations_paths, steps)
end

def initialize(direction, migrations, target_version = nil)
raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
raise StandardError.new("This database does not yet support migrations") unless Base.with_connection { |conn| conn.supports_migrations? }

@direction = direction
@target_version = target_version
Expand All @@ -916,7 +932,7 @@ def initialize(direction, migrations, target_version = nil)

validate(@migrations)

Base.connection.initialize_schema_migrations_table
ActiveRecord::Base.with_connection { |conn| conn.initialize_schema_migrations_table }
end

def current_version
Expand Down Expand Up @@ -1042,7 +1058,7 @@ def ddl_transaction(migration)
end

def use_transaction?(migration)
!migration.disable_ddl_transaction && Base.connection.supports_ddl_transactions?
!migration.disable_ddl_transaction && ActiveRecord::Base.with_connection { |conn| conn.supports_ddl_transactions? }
end
end
end
24 changes: 14 additions & 10 deletions activerecord/lib/active_record/model_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def table_name=(value)

# Returns a quoted version of the table name, used to construct SQL statements.
def quoted_table_name
@quoted_table_name ||= connection.quote_table_name(table_name)
@quoted_table_name ||= with_connection { |conn| conn.quote_table_name(table_name) }
end

# Computes the table name, (re)sets it internally, and returns it.
Expand Down Expand Up @@ -182,7 +182,7 @@ def sequence_name

def reset_sequence_name #:nodoc:
@explicit_sequence_name = false
@sequence_name = connection.default_sequence_name(table_name, primary_key)
@sequence_name = with_connection { |conn| conn.default_sequence_name(table_name, primary_key) }
end

# Sets the name of the sequence to use when generating ids to the given
Expand All @@ -206,15 +206,17 @@ def sequence_name=(value)

# Indicates whether the table associated with this class exists
def table_exists?
connection.schema_cache.table_exists?(table_name)
with_connection { |conn| conn.schema_cache.table_exists?(table_name) }
end

# Returns an array of column objects for the table associated with this class.
def columns
@columns ||= connection.schema_cache.columns(table_name).map do |col|
col = col.dup
col.primary = (col.name == primary_key)
col
@columns ||= with_connection do |conn|
conn.schema_cache.columns(table_name).map do |col|
col = col.dup
col.primary = (col.name == primary_key)
col
end
end
end

Expand Down Expand Up @@ -293,9 +295,11 @@ def content_columns
# end
# end
def reset_column_information
connection.clear_cache!
undefine_attribute_methods
connection.schema_cache.clear_table_cache!(table_name) if table_exists?
with_connection do |conn|
conn.clear_cache!
undefine_attribute_methods
conn.schema_cache.clear_table_cache!(table_name) if table_exists?
end

@arel_engine = nil
@column_defaults = nil
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def destroy_row
def relation_for_destroy
pk = self.class.primary_key
column = self.class.columns_hash[pk]
substitute = self.class.connection.substitute_at(column, 0)
substitute = self.class.with_connection { |conn| conn.substitute_at(column, 0) }

relation = self.class.unscoped.where(
self.class.arel_table[pk].eq(substitute))
Expand Down
Loading