Use connection pool instead of actual connection to avoid having to connect to the database until necessary
This commit is contained in:
parent
3c08fad1a2
commit
08c36d33e4
|
@ -2,15 +2,15 @@ class ActiveRecord::Base
|
|||
class << self
|
||||
def establish_connection_with_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
|
||||
alias_method_chain :establish_connection, :activerecord_import
|
||||
end
|
||||
end
|
||||
|
||||
ActiveSupport.on_load(:active_record_connection_established) do |connection|
|
||||
if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection)
|
||||
ActiveSupport.on_load(:active_record_connection_established) do |connection_pool|
|
||||
if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection_pool)
|
||||
require File.join File.dirname(__FILE__), "activerecord-import/base"
|
||||
end
|
||||
ActiveRecord::Import.load_from_connection connection
|
||||
ActiveRecord::Import.load_from_connection_pool connection_pool
|
||||
end
|
||||
|
|
|
@ -12,12 +12,8 @@ module ActiveRecord::Import
|
|||
end
|
||||
|
||||
# Loads the import functionality for the passed in ActiveRecord connection
|
||||
def self.load_from_connection(connection)
|
||||
import_adapter = "ActiveRecord::Import::#{connection.class.name.demodulize}::InstanceMethods"
|
||||
unless connection.class.ancestors.map(&:name).include?(import_adapter)
|
||||
config = connection.instance_variable_get :@config
|
||||
require_adapter config[:adapter]
|
||||
end
|
||||
def self.load_from_connection_pool(connection_pool)
|
||||
require_adapter connection_pool.spec.config[:adapter]
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue