active_record_host_pool 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c38ebceb4621733f8beab2187309bdd60bbf207
4
- data.tar.gz: 3eb2efdc2264d85149ba5aec4e975e844a7b2344
3
+ metadata.gz: 845aaec086857a96f06d01811462e9470f325c12
4
+ data.tar.gz: 471aff8ae49c1d0e8b4954537b8f11c0b9bdfcef
5
5
  SHA512:
6
- metadata.gz: 5a8dd74cc7cc9a89c220169d9f1999098bdb1a598e07be23a0aaa5fd2e510183e279c6533959fdad9099dabbda320e23dbc28a1a5ffece68547300eb40de1110
7
- data.tar.gz: 64782949403041135a1cdcae145374cff83f6e347d825ef797e661535d30d857897076d1844b383fa44436d1407b908ee51bfc9fed636d9ee0ca04a6ebc173a6
6
+ metadata.gz: 38e24829caad45f4a924c1862dd6396056e1d52a2ac9336b16a48e48e001597c50fb9389d57df92cefbadab4789c61a618551207850c153ba6b6b3136186b1df
7
+ data.tar.gz: cc2a4d67f037f23219bf3b181c0a52629df43e7f31602c9abce2241e7b7c43d898f8eeb6001a4c60d3a667f16957287fd95e4ed57c5b8ce08eea0480bfcb238e
@@ -5,4 +5,7 @@ require 'active_record/connection_adapters/abstract_adapter'
5
5
  require 'active_record_host_pool/connection_proxy'
6
6
  require 'active_record_host_pool/pool_proxy'
7
7
  require 'active_record_host_pool/connection_adapter_mixin'
8
+ require 'active_record_host_pool/version'
8
9
 
10
+ module ActiveRecordHostPool
11
+ end
@@ -7,52 +7,41 @@ end
7
7
 
8
8
  module ActiveRecordHostPool
9
9
  module DatabaseSwitch
10
- def self.included(base)
10
+ def self.prepended(base)
11
11
  base.class_eval do
12
12
  attr_accessor(:_host_pool_current_database)
13
- alias_method_chain :execute, :switching
14
- alias_method_chain :exec_stmt, :switching if private_instance_methods.map(&:to_sym).include?(:exec_stmt)
15
- alias_method_chain :drop_database, :no_switching
16
- alias_method_chain :create_database, :no_switching
17
- alias_method_chain :disconnect!, :host_pooling
18
13
  end
19
14
  end
20
15
 
21
- def execute_with_switching(*args)
16
+ def execute(*args)
22
17
  if _host_pool_current_database && ! @_no_switch
23
18
  _switch_connection
24
19
  end
25
- execute_without_switching(*args)
20
+ super
26
21
  end
27
22
 
28
- def exec_stmt_with_switching(sql, name, binds, &block)
29
- if _host_pool_current_database && ! @_no_switch
30
- _switch_connection
31
- end
32
- exec_stmt_without_switching(sql, name, binds, &block)
33
- end
34
-
35
- def drop_database_with_no_switching(*args)
23
+ def drop_database(*args)
36
24
  begin
37
25
  @_no_switch = true
38
- drop_database_without_no_switching(*args)
26
+ super
39
27
  ensure
40
28
  @_no_switch = false
41
29
  end
42
30
  end
43
31
 
44
- def create_database_with_no_switching(*args)
32
+ def create_database(*args)
45
33
  begin
46
34
  @_no_switch = true
47
- create_database_without_no_switching(*args)
35
+ super
48
36
  ensure
49
37
  @_no_switch = false
50
38
  end
51
39
  end
52
40
 
53
- def disconnect_with_host_pooling!
41
+ def disconnect!
54
42
  @_cached_current_database = nil
55
- disconnect_without_host_pooling!
43
+ @_cached_connection_object_id = nil
44
+ super
56
45
  end
57
46
 
58
47
  private
@@ -62,6 +51,7 @@ module ActiveRecordHostPool
62
51
  log("select_db #{_host_pool_current_database}", "SQL") do
63
52
  clear_cache! if respond_to?(:clear_cache!)
64
53
  raw_connection.select_db(_host_pool_current_database)
54
+ @config[:database] = _host_pool_current_database if ActiveRecord::VERSION::MAJOR >= 5
65
55
  end
66
56
  @_cached_current_database = _host_pool_current_database
67
57
  @_cached_connection_object_id = @connection.object_id
@@ -78,18 +68,16 @@ end
78
68
  module ActiveRecord
79
69
  module ConnectionAdapters
80
70
  class ConnectionHandler
81
- def establish_connection(name, spec)
71
+ def establish_connection(owner, spec)
82
72
  if ActiveRecord::VERSION::MAJOR >= 4
83
- owner = name
84
-
85
73
  @class_to_pool.clear
86
74
  raise RuntimeError, "Anonymous class is not allowed." unless owner.name
87
75
  owner_to_pool[owner.name] = ActiveRecordHostPool::PoolProxy.new(spec)
88
76
  elsif ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 2
89
77
  @connection_pools[spec] ||= ActiveRecordHostPool::PoolProxy.new(spec)
90
- @class_to_pool[name] = @connection_pools[spec]
78
+ @class_to_pool[owner] = @connection_pools[spec]
91
79
  else
92
- @connection_pools[name] = ActiveRecordHostPool::PoolProxy.new(spec)
80
+ @connection_pools[owner] = ActiveRecordHostPool::PoolProxy.new(spec)
93
81
  end
94
82
  end
95
83
  end
@@ -98,5 +86,5 @@ end
98
86
 
99
87
  ["MysqlAdapter", "Mysql2Adapter"].each do |k|
100
88
  next unless ActiveRecord::ConnectionAdapters.const_defined?(k)
101
- ActiveRecord::ConnectionAdapters.const_get(k).class_eval { include ActiveRecordHostPool::DatabaseSwitch }
89
+ ActiveRecord::ConnectionAdapters.const_get(k).class_eval { prepend ActiveRecordHostPool::DatabaseSwitch }
102
90
  end
@@ -15,7 +15,7 @@ module ActiveRecordHostPool
15
15
  def initialize(spec)
16
16
  super(spec)
17
17
  @spec = spec
18
- @config = spec.config.with_indifferent_access
18
+ @config = spec.config
19
19
  end
20
20
 
21
21
  def __getobj__
@@ -24,7 +24,7 @@ module ActiveRecordHostPool
24
24
 
25
25
  def __setobj__(spec)
26
26
  @spec = spec
27
- @config = spec.config.with_indifferent_access
27
+ @config = spec.config
28
28
  @_pool_key = nil
29
29
  end
30
30
 
@@ -0,0 +1,3 @@
1
+ module ActiveRecordHostPool
2
+ VERSION = "0.9.0"
3
+ end
data/test/database.yml CHANGED
@@ -1,39 +1,40 @@
1
+ <% mysql = URI(ENV['MYSQL_URL'] || 'mysql://root@127.0.0.1:3306') %>
1
2
  test_host_1_db_1:
2
3
  adapter: mysql2
3
4
  encoding: utf8
4
5
  database: arhp_test_1
5
- username: root
6
- password:
7
- host: 127.0.0.1
6
+ username: <%= mysql.user %>
7
+ password: <%= mysql.password %>
8
+ host: <%= mysql.host %>
8
9
  reconnect: true
9
10
 
10
11
  test_host_1_db_2:
11
12
  adapter: mysql2
12
13
  encoding: utf8
13
14
  database: arhp_test_2
14
- username: root
15
- password:
16
- host: 127.0.0.1
15
+ username: <%= mysql.user %>
16
+ password: <%= mysql.password %>
17
+ host: <%= mysql.host %>
17
18
  reconnect: true
18
19
 
19
20
  test_host_2_db_3:
20
21
  adapter: mysql2
21
22
  encoding: utf8
22
23
  database: arhp_test_3
23
- username: root
24
- password:
25
- host: 127.0.0.1
26
- port: 3306
24
+ username: <%= mysql.user %>
25
+ password: <%= mysql.password %>
26
+ host: <%= mysql.host %>
27
+ port: <%= mysql.port %>
27
28
  reconnect: true
28
29
 
29
30
  test_host_2_db_4:
30
31
  adapter: mysql2
31
32
  encoding: utf8
32
33
  database: arhp_test_4
33
- username: root
34
- password:
35
- host: 127.0.0.1
36
- port: 3306
34
+ username: <%= mysql.user %>
35
+ password: <%= mysql.password %>
36
+ host: <%= mysql.host %>
37
+ port: <%= mysql.port %>
37
38
  reconnect: true
38
39
 
39
40
  test_host_2_db_5:
@@ -42,15 +43,15 @@ test_host_2_db_5:
42
43
  database: arhp_test_4
43
44
  username: travis
44
45
  password:
45
- host: 127.0.0.1
46
- port: 3306
46
+ host: <%= mysql.host %>
47
+ port: <%= mysql.port %>
47
48
  reconnect: true
48
49
 
49
50
  test_host_1_db_not_there:
50
51
  adapter: mysql2
51
52
  encoding: utf8
52
53
  database: arhp_test_no_create
53
- username: root
54
- password:
55
- host: 127.0.0.1
54
+ username: <%= mysql.user %>
55
+ password: <%= mysql.password %>
56
+ host: <%= mysql.host %>
56
57
  reconnect: true
data/test/helper.rb CHANGED
@@ -4,28 +4,25 @@ require 'minitest/autorun'
4
4
  require 'active_record_host_pool'
5
5
  require 'logger'
6
6
  require 'mocha/setup'
7
+ require 'erb'
7
8
 
8
9
  RAILS_ENV = "test"
9
10
 
10
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/test.log")
11
+ Minitest::Test = MiniTest::Unit::TestCase unless defined?(::Minitest::Test)
11
12
 
12
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
13
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/test.log")
13
14
 
14
- if ENV["BUNDLE_GEMFILE"] =~ /mysql2/
15
- config.each do |k, v|
16
- v['adapter'] = 'mysql2'
17
- end
18
- end
15
+ config_content = IO.read(File.dirname(__FILE__) + '/database.yml')
16
+ config_content = ERB.new(config_content).result
17
+ config = YAML.load(config_content)
19
18
 
20
19
  ActiveRecord::Base.configurations = config
21
20
 
22
21
  module ARHPTestSetup
23
22
  private
24
23
  def arhp_create_databases
25
- ActiveRecord::Base.configurations.each do |name, conf|
26
- next if name =~ /not_there/
27
- `echo "drop DATABASE IF EXISTS #{conf['database']}" | mysql --user=#{conf['username']}`
28
- `echo "create DATABASE #{conf['database']}" | mysql --user=#{conf['username']}`
24
+ for_each_database do |name, conf|
25
+ run_mysql_command(conf, "CREATE DATABASE #{conf['database']}")
29
26
  ActiveRecord::Base.establish_connection(name.to_sym)
30
27
  ActiveRecord::Migration.verbose = false
31
28
  load(File.dirname(__FILE__) + "/schema.rb")
@@ -33,8 +30,8 @@ module ARHPTestSetup
33
30
  end
34
31
 
35
32
  def arhp_drop_databases
36
- ActiveRecord::Base.configurations.each do |name, conf|
37
- ActiveRecord::Base.connection.execute("DROP DATABASE if exists #{conf['database']}")
33
+ for_each_database do |name, conf|
34
+ run_mysql_command(conf, "DROP DATABASE IF EXISTS #{conf['database']}")
38
35
  end
39
36
  end
40
37
 
@@ -68,6 +65,28 @@ module ARHPTestSetup
68
65
  EOL
69
66
  end
70
67
 
68
+ def for_each_database
69
+ ActiveRecord::Base.configurations.each do |name, conf|
70
+ next if name =~ /not_there/
71
+ next if conf['username'] == 'travis'
72
+ yield(name, conf)
73
+ end
74
+ end
75
+
76
+ def run_mysql_command(conf, command)
77
+ @mysql_command ||= begin
78
+ commands = [
79
+ 'mysql',
80
+ "--user=#{conf['username']}"
81
+ ]
82
+ commands << "--host=#{conf['host']}" if conf['host'].present?
83
+ commands << "--port=#{conf['port']}" if conf['port'].present?
84
+ commands << " --password=#{conf['password']} 2> /dev/null" if conf['password'].present?
85
+ commands.join(' ')
86
+ end
87
+ `echo "#{command}" | #{@mysql_command}`
88
+ end
89
+
71
90
  def current_database(klass)
72
91
  klass.connection.select_value("select DATABASE()")
73
92
  end
data/test/test_arhp.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  require File.expand_path('helper', File.dirname(__FILE__))
2
2
 
3
- class ActiveRecordHostPoolTest < MiniTest::Unit::TestCase
3
+ class ActiveRecordHostPoolTest < Minitest::Test
4
4
  include ARHPTestSetup
5
5
  def setup
6
+ arhp_drop_databases
6
7
  arhp_create_databases
7
8
  arhp_create_models
8
9
  end
9
10
 
11
+ def teardown
12
+ arhp_drop_databases
13
+ end
14
+
10
15
  def test_models_with_matching_hosts_should_share_a_connection
11
16
  assert(Test1.connection.raw_connection == Test2.connection.raw_connection)
12
17
  assert(Test3.connection.raw_connection == Test4.connection.raw_connection)
@@ -91,7 +96,7 @@ class ActiveRecordHostPoolTest < MiniTest::Unit::TestCase
91
96
  Test1.first
92
97
 
93
98
  # which is the "default" DB to connect to?
94
- first_db = Test1.connection.unproxied.instance_variable_get("@connection_options")[3]
99
+ first_db = Test1.connection.unproxied.instance_variable_get(:@_cached_current_database)
95
100
  puts "\nOk, we started on #{first_db}" if debug_me
96
101
 
97
102
  switch_to_klass = case first_db
@@ -100,7 +105,7 @@ class ActiveRecordHostPoolTest < MiniTest::Unit::TestCase
100
105
  when "arhp_test_1"
101
106
  Test2
102
107
  end
103
- expected_database = switch_to_klass.connection.instance_variable_get("@database")
108
+ expected_database = switch_to_klass.connection.instance_variable_get(:@database)
104
109
 
105
110
  # switch to the other database
106
111
  switch_to_klass.first
@@ -118,10 +123,6 @@ class ActiveRecordHostPoolTest < MiniTest::Unit::TestCase
118
123
  assert_equal expected_database, current_database(switch_to_klass)
119
124
  end
120
125
 
121
- def teardown
122
- arhp_drop_databases
123
- end
124
-
125
126
  private
126
127
 
127
128
  def action_should_use_correct_database(action, sql)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_host_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Osheroff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2012-06-11 00:00:00.000000000 Z
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 3.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '0'
29
+ version: 3.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.1'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: wwtd
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -53,6 +59,7 @@ files:
53
59
  - lib/active_record_host_pool/connection_adapter_mixin.rb
54
60
  - lib/active_record_host_pool/connection_proxy.rb
55
61
  - lib/active_record_host_pool/pool_proxy.rb
62
+ - lib/active_record_host_pool/version.rb
56
63
  - test/database.yml
57
64
  - test/helper.rb
58
65
  - test/schema.rb
@@ -77,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
84
  version: '0'
78
85
  requirements: []
79
86
  rubyforge_project:
80
- rubygems_version: 2.2.2
87
+ rubygems_version: 2.5.0
81
88
  signing_key:
82
89
  specification_version: 4
83
90
  summary: Allow ActiveRecord to share a connection to multiple databases on the same