master_slave 3.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,6 @@ module MasterSlave
5
5
  included do
6
6
  cattr_accessor :slave_connection_names
7
7
  self.slave_connection_names = []
8
-
9
- class << self
10
- alias_method :connection_without_master_slave, :connection
11
- alias_method :connection, :connection_with_master_slave
12
- end
13
8
  end
14
9
 
15
10
  module ClassMethods
@@ -31,18 +26,16 @@ module MasterSlave
31
26
  end
32
27
  end
33
28
 
34
- def connection_with_master_slave
29
+ def connection
35
30
  slave_block = MasterSlave::RuntimeRegistry.slave_block
36
31
  current_slave_name = MasterSlave::RuntimeRegistry.current_slave_name
37
32
 
38
- if defined?(Rails) && Rails.env.test?
39
- connection_without_master_slave
40
- elsif slave_block && current_slave_name
33
+ if slave_block && current_slave_name
41
34
  pool_name = MasterSlave::ConnectionHandler.connection_pool_name(current_slave_name)
42
35
  ar_proxy = MasterSlave::ConnectionHandler::ArProxy.new(pool_name)
43
36
  ActiveRecord::Base.connection_handler.retrieve_connection(ar_proxy)
44
37
  else
45
- connection_without_master_slave
38
+ super
46
39
  end
47
40
  end
48
41
 
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  module MasterSlave
3
2
  class ConnectionHandler
4
3
 
@@ -25,12 +24,10 @@ module MasterSlave
25
24
  def setup_connection
26
25
  ActiveRecord::Base.slave_connection_names ||= []
27
26
  MasterSlave.config.slave_names.each do |slave_name|
28
- # activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +128
29
27
  ActiveRecord::Base.slave_connection_names << slave_name.to_s.strip
30
- configuration = MasterSlave.config.slave_config(slave_name).symbolize_keys
31
-
32
- resolver = ActiveRecord::Base::ConnectionSpecification::Resolver.new(configuration, nil)
28
+ spec = MasterSlave.config.slave_config(slave_name).symbolize_keys
33
29
 
30
+ resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new spec, spec
34
31
  spec = resolver.spec
35
32
 
36
33
  unless ActiveRecord::Base.respond_to?(spec.adapter_method)
@@ -38,15 +35,8 @@ module MasterSlave
38
35
  end
39
36
 
40
37
  ar_proxy = ArProxy.new(connection_pool_name(slave_name))
41
-
42
- # activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +179
43
- # activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +424
44
- # remove_connection 时会调用方法内部 ar_proxy.name
45
38
  ActiveRecord::Base.remove_connection(ar_proxy)
46
-
47
- # activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +373
48
- # establish_connection 时,使用 ar_proxy.name
49
- ActiveRecord::Base.connection_handler.establish_connection ar_proxy.name, spec
39
+ ActiveRecord::Base.connection_handler.establish_connection ar_proxy, spec
50
40
  end
51
41
  end
52
42
  end
@@ -3,18 +3,18 @@ module MasterSlave
3
3
 
4
4
  config.after_initialize do
5
5
  if File.exist?(MasterSlave::Configuration.config_file)
6
- Rails.logger.info "\033[32mmaster_slave is on!\033[0m"
6
+ puts "\033[32mmaster_slave is on!\033[0m"
7
7
  ActiveRecord::Base.send :include, MasterSlave::Base
8
8
  MasterSlave::ConnectionHandler.setup
9
9
  else
10
- Rails.logger.error "\033[31mNo such file #{MasterSlave::Configuration.config_file}\033[0m"
11
- Rails.logger.error "\033[31mPlease execute `rails g master_slave:config`\033[0m"
12
- Rails.logger.error "\033[31mmaster_slave is off!\033[0m"
10
+ puts "\033[31mNo such file #{MasterSlave::Configuration.config_file}\033[0m"
11
+ puts "\033[31mPlease execute `rails g master_slave:config`\033[0m"
12
+ puts "\033[31mmaster_slave is off!\033[0m"
13
13
  end
14
14
  end
15
15
 
16
16
  generators do
17
- require File.expand_path('../../rails/generators/config_generator', __FILE__)
17
+ require File.join(__dir__, '../', 'rails/generators/config_generator')
18
18
  end
19
19
  end
20
20
  end
@@ -1,28 +1,4 @@
1
- begin
2
- require 'active_support/per_thread_registry'
3
- rescue LoadError
4
- module ActiveSupport
5
- module PerThreadRegistry
6
- protected
7
-
8
- def method_missing(name, *args, &block) # :nodoc:
9
- # Caches the method definition as a singleton method of the receiver.
10
- define_singleton_method(name) do |*a, &b|
11
- per_thread_registry_instance.public_send(name, *a, &b)
12
- end
13
-
14
- send(name, *args, &block)
15
- end
16
-
17
- private
18
-
19
- def per_thread_registry_instance
20
- Thread.current[name] ||= new
21
- end
22
- end
23
- end
24
- end
25
-
1
+ require 'active_support/per_thread_registry'
26
2
  module MasterSlave
27
3
  class RuntimeRegistry
28
4
  extend ActiveSupport::PerThreadRegistry
@@ -1,3 +1,3 @@
1
1
  module MasterSlave
2
- VERSION = '3.2.0'
2
+ VERSION = '4.0.0'
3
3
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: master_slave
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 4.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Tumayun, Zhangyuan
8
+ - Tumayun
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
@@ -16,17 +16,17 @@ dependencies:
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.2.0
21
+ version: 4.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ~>
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 3.2.0
29
+ version: 4.0.0
30
30
  description: mysql separate read and write.
31
31
  email: tumayun.2010@gmail.com
32
32
  executables: []