Module: ActiveRecordHostPool::DatabaseSwitch

Defined in:
lib/active_record_host_pool/connection_adapter_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record_host_pool/connection_adapter_mixin.rb', line 11

def self.included(base)
  base.class_eval do
    attr_reader(:_host_pool_current_database)

    def _host_pool_current_database=(database)
      @_host_pool_current_database = database
      @config[:database] = _host_pool_current_database if ActiveRecord::VERSION::MAJOR >= 5
    end

    alias_method :execute_without_switching, :execute
    alias_method :execute, :execute_with_switching

    alias_method :drop_database_without_no_switching, :drop_database
    alias_method :drop_database, :drop_database_with_no_switching

    alias_method :create_database_without_no_switching, :create_database
    alias_method :create_database, :create_database_with_no_switching

    alias_method :disconnect_without_host_pooling!, :disconnect!
    alias_method :disconnect!, :disconnect_with_host_pooling!
  end
end

Instance Method Details

#create_database_with_no_switching(*args) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/active_record_host_pool/connection_adapter_mixin.rb', line 50

def create_database_with_no_switching(*args)
  begin
    @_no_switch = true
    create_database_without_no_switching(*args)
  ensure
    @_no_switch = false
  end
end

#disconnect_with_host_pooling!Object



59
60
61
62
63
# File 'lib/active_record_host_pool/connection_adapter_mixin.rb', line 59

def disconnect_with_host_pooling!
  @_cached_current_database = nil
  @_cached_connection_object_id = nil
  disconnect_without_host_pooling!
end

#drop_database_with_no_switching(*args) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/active_record_host_pool/connection_adapter_mixin.rb', line 41

def drop_database_with_no_switching(*args)
  begin
    @_no_switch = true
    drop_database_without_no_switching(*args)
  ensure
    @_no_switch = false
  end
end

#execute_with_switching(*args) ⇒ Object



34
35
36
37
38
39
# File 'lib/active_record_host_pool/connection_adapter_mixin.rb', line 34

def execute_with_switching(*args)
  if _host_pool_current_database && ! @_no_switch
    _switch_connection
  end
  execute_without_switching(*args)
end