Use connection pool instead of actual connection to avoid having to connect to the database until necessary

This commit is contained in:
Nat Budin 2011-10-10 13:07:10 -04:00
parent 3c08fad1a2
commit 08c36d33e4
2 changed files with 6 additions and 10 deletions

View file

@ -2,15 +2,15 @@ class ActiveRecord::Base
class << self class << self
def establish_connection_with_activerecord_import(*args) def establish_connection_with_activerecord_import(*args)
establish_connection_without_activerecord_import(*args) establish_connection_without_activerecord_import(*args)
ActiveSupport.run_load_hooks(:active_record_connection_established, connection) ActiveSupport.run_load_hooks(:active_record_connection_established, connection_pool)
end end
alias_method_chain :establish_connection, :activerecord_import alias_method_chain :establish_connection, :activerecord_import
end end
end end
ActiveSupport.on_load(:active_record_connection_established) do |connection| ActiveSupport.on_load(:active_record_connection_established) do |connection_pool|
if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection) if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection_pool)
require File.join File.dirname(__FILE__), "activerecord-import/base" require File.join File.dirname(__FILE__), "activerecord-import/base"
end end
ActiveRecord::Import.load_from_connection connection ActiveRecord::Import.load_from_connection_pool connection_pool
end end

View file

@ -12,12 +12,8 @@ module ActiveRecord::Import
end end
# Loads the import functionality for the passed in ActiveRecord connection # Loads the import functionality for the passed in ActiveRecord connection
def self.load_from_connection(connection) def self.load_from_connection_pool(connection_pool)
import_adapter = "ActiveRecord::Import::#{connection.class.name.demodulize}::InstanceMethods" require_adapter connection_pool.spec.config[:adapter]
unless connection.class.ancestors.map(&:name).include?(import_adapter)
config = connection.instance_variable_get :@config
require_adapter config[:adapter]
end
end end
end end