fresh_connection 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3236c1e50671a5ba2387ef7d09abd2fe182f6018
4
- data.tar.gz: 16e4b85ad933af49e471969d65547855b9b2a09c
3
+ metadata.gz: 755153c16ffcf02081f831ae593d7b15a23ea0d2
4
+ data.tar.gz: 45145aa7be5d694975dfbc46330bd921d158d246
5
5
  SHA512:
6
- metadata.gz: ffc51c4d326026040e127db6e4c56d7f67ef40c58b5d4cc9f39b60dc9c60713cab0553b7671b9ccf35dc0c996984e1fadc81bca6c9b2a2e348829c29cc7235db
7
- data.tar.gz: e12983fa297222b1aeb6ec63841820383c6d9f714e0f4b62135354a7b96298ffdea9feec6ca6f8ceb9df80b99edbc09c0314e5dce955668acb959673eb39a84d
6
+ metadata.gz: 8ed0b205aa9966468e9a39033b1726539d7bfc01acd66835f697f37739acc25369475b40163b5330e61cccf0abe26af388a8f32a6e072796a4f41739a163e993
7
+ data.tar.gz: 89c830adfcc482727dea49d243e694d9d6e29ba4071eb6576c20dcafe7d0f76a19d8f6f45d2a021c7e616e36fa256e3ad6f449df3d8713c525fff7b963e7af20
data/.gitignore CHANGED
@@ -3,6 +3,5 @@
3
3
  .ruby-version
4
4
  Gemfile.lock
5
5
  gemfiles/*.lock
6
- bundle
7
6
  log/*
8
7
  .*.sw[a-z]
data/.travis.yml CHANGED
@@ -5,7 +5,6 @@ before_script:
5
5
  - mysql -e 'create database fresh_connection_test_slave;'
6
6
  - "bundle exec ruby spec/prepare.rb"
7
7
  rvm:
8
- - 1.9.3
9
8
  - 2.0.0
10
9
  - 2.1
11
10
  - 2.2
data/README.md CHANGED
@@ -37,7 +37,6 @@ If you want to access to the master server, use read_master.
37
37
  ```ruby
38
38
  Article.where(:id => 1).read_master
39
39
  ```
40
-
41
40
  It is possible to use readonly(false) instead of read_master, but it will be depricated at future version.
42
41
 
43
42
  In transaction, All queries go to the master server.
@@ -146,11 +145,11 @@ AdminUser and Benefit access to ```admin_slave``` slave group.
146
145
  master_db_only!
147
146
  end
148
147
 
149
- If a model that always access to the master server is exist, You write ```master_db_only!``` in the model.
148
+ If a model that always access to the master server is exist, You write ```master_db_only!``` in the model.
150
149
  The model that master_db_only model's child is always access to master db.
151
150
 
152
151
  ### Slave Connection Manager
153
- Default slave connection manager is FreshConnection::ConnectionManager.
152
+ Default slave connection manager is FreshConnection::ConnectionManager.
154
153
  If you would like to change slave connection manager, assign yourself slave connection manager.
155
154
 
156
155
  #### config/initializers/fresh_connection.rb
@@ -185,15 +184,15 @@ Yourself Slave Connection Manager should be inherited FreshConnection::AbstractC
185
184
 
186
185
  ## Test
187
186
 
188
- I'm glad that you would do test!
189
- To run the test suite, you need mysql installed.
187
+ I'm glad that you would do test!
188
+ To run the test suite, you need mysql installed.
190
189
  How to setup your test environment.
191
190
 
192
191
  First of all, you setting the config of the test mysql server in ```spec/database.yml```
193
192
 
194
193
  ```bash
195
- bundle install --path bundle
196
- GEM_HOME=bundle/ruby/(your ruby version) gem install bundler --pre
194
+ bundle install --path .bundle
195
+ GEM_HOME=.bundle/ruby/(your ruby version) gem install bundler --pre
197
196
  bundle exec appraisal install
198
197
  ```
199
198
 
@@ -0,0 +1,31 @@
1
+ module FreshConnection
2
+ class ConnectionFactory
3
+ def initialize(group, modify_spec = {})
4
+ @group = group.to_sym
5
+ @modify_spec = modify_spec
6
+ end
7
+
8
+ def new_connection
9
+ ActiveRecord::Base.__send__(adapter_method, spec)
10
+ end
11
+
12
+ private
13
+
14
+ def adapter_method
15
+ @adapter_method ||= ar_spec.adapter_method
16
+ end
17
+
18
+ def spec
19
+ @spec ||= build_spec
20
+ end
21
+
22
+ def build_spec
23
+ config = ar_spec.config
24
+ config.merge(config[@group] || {}).merge(@modify_spec)
25
+ end
26
+
27
+ def ar_spec
28
+ ActiveRecord::Base.connection_pool.spec
29
+ end
30
+ end
31
+ end
@@ -1,10 +1,11 @@
1
1
  require 'fresh_connection/abstract_connection_manager'
2
+ require 'fresh_connection/connection_factory'
2
3
 
3
4
  module FreshConnection
4
5
  class ConnectionManager < AbstractConnectionManager
5
6
  def slave_connection
6
7
  synchronize do
7
- slave_connections[current_thread_id] ||= new_connection
8
+ slave_connections[current_thread_id] ||= connection_factory.new_connection
8
9
  end
9
10
  end
10
11
 
@@ -28,17 +29,8 @@ module FreshConnection
28
29
  @slave_connections ||= {}
29
30
  end
30
31
 
31
- def new_connection
32
- ActiveRecord::Base.send("mysql2_connection", spec)
33
- end
34
-
35
- def spec
36
- @spec ||= get_spec
37
- end
38
-
39
- def get_spec
40
- ret = ActiveRecord::Base.configurations[FreshConnection.env]
41
- ret.merge(ret[@slave_group] || {})
32
+ def connection_factory
33
+ @connection_factory ||= ConnectionFactory.new(@slave_group)
42
34
  end
43
35
  end
44
36
  end
@@ -1,3 +1,7 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+ require 'fresh_connection/slave_connection_handler'
3
+ require 'fresh_connection/access_control'
4
+
1
5
  module FreshConnection
2
6
  module Extend
3
7
  module ArBase
@@ -1,18 +1,22 @@
1
+ require 'active_support/deprecation'
2
+
1
3
  module FreshConnection
2
4
  module Extend
3
5
  module ArRelation
4
- def self.included(base)
5
- base.alias_method_chain :exec_queries, :fresh_connection
6
-
6
+ def self.prepended(base)
7
7
  if FreshConnection.rails_4?
8
- base.__send__(:include, ForRails4)
8
+ base.__send__(:prepend, ForRails4)
9
9
  elsif FreshConnection.rails_3?
10
- base.__send__(:include, ForRails3)
10
+ base.__send__(:prepend, ForRails3)
11
11
  end
12
12
  end
13
13
 
14
14
 
15
15
  def calculate(operation, column_name, options = {})
16
+ if options[:readonly] == false
17
+ ActiveSupport::Deprecation.warn(":readonly key has been deprecated.", caller)
18
+ end
19
+
16
20
  slave_access = enable_slave_access && options[:readonly] != false
17
21
  @klass.manage_access(slave_access) { super }
18
22
  end
@@ -21,13 +25,22 @@ module FreshConnection
21
25
  connection.open_transactions == 0 && @read_from_master.nil?
22
26
  end
23
27
 
28
+ def readonly(value = true)
29
+ if value == false
30
+ ActiveSupport::Deprecation.warn("readonly(false) has been deprecated. Use read_master instead", caller)
31
+ read_master
32
+ else
33
+ super
34
+ end
35
+ end
36
+
24
37
  private
25
38
 
26
- def exec_queries_with_fresh_connection
39
+ def exec_queries
27
40
  return @records if loaded?
28
41
 
29
42
  @klass.manage_access(enable_slave_access) do
30
- exec_queries_without_fresh_connection
43
+ super
31
44
  end
32
45
  end
33
46
 
@@ -36,10 +49,6 @@ module FreshConnection
36
49
  @klass.manage_access(enable_slave_access) { super }
37
50
  end
38
51
 
39
- def readonly(value = true)
40
- value == false ? read_master : super
41
- end
42
-
43
52
  def read_master
44
53
  spawn.read_master!
45
54
  end
@@ -69,10 +78,6 @@ module FreshConnection
69
78
  end
70
79
  end
71
80
 
72
- def readonly(value = true)
73
- value == false ? read_master : super
74
- end
75
-
76
81
  def read_master
77
82
  relation = clone
78
83
  relation.instance_variable_set("@read_from_master", true)
@@ -1,13 +1,9 @@
1
1
  module FreshConnection
2
2
  module Extend
3
3
  module ArStatementCache
4
- def self.included(base)
5
- base.alias_method_chain :execute, :fresh_connection
6
- end
7
-
8
- def execute_with_fresh_connection(params, klass, connection)
4
+ def execute(params, klass, connection)
9
5
  klass.manage_access(klass.all.enable_slave_access) do
10
- execute_without_fresh_connection(params, klass, connection)
6
+ super
11
7
  end
12
8
  end
13
9
  end
@@ -1,12 +1,8 @@
1
1
  module FreshConnection
2
2
  module Extend
3
3
  module ConnectionHandler
4
- def self.included(base)
5
- base.alias_method_chain :retrieve_connection, :fresh_connection
6
- end
7
-
8
- def retrieve_connection_with_fresh_connection(klass)
9
- c = retrieve_connection_without_fresh_connection(klass)
4
+ def retrieve_connection(klass)
5
+ c = super
10
6
  c.model_class = klass
11
7
  c
12
8
  end
@@ -1,10 +1,12 @@
1
+ require 'fresh_connection/access_control'
2
+
1
3
  module FreshConnection
2
4
  module Extend
3
5
  module Mysql2Adapter
4
6
  RETRY_LIMIT = 3
5
7
  private_constant :RETRY_LIMIT
6
8
 
7
- def self.included(base)
9
+ def self.prepended(base)
8
10
  base.__send__(:attr_writer, :model_class)
9
11
  end
10
12
 
@@ -8,20 +8,19 @@ ActiveSupport.on_load(:active_record) do
8
8
  require 'active_record/connection_adapters/mysql2_adapter'
9
9
 
10
10
  ActiveRecord::Base.extend FreshConnection::Extend::ArBase
11
-
12
- ActiveRecord::Relation.__send__(:include, FreshConnection::Extend::ArRelation)
11
+ ActiveRecord::Relation.__send__(:prepend, FreshConnection::Extend::ArRelation)
13
12
 
14
13
  ActiveRecord::ConnectionAdapters::ConnectionHandler.__send__(
15
- :include, FreshConnection::Extend::ConnectionHandler
14
+ :prepend, FreshConnection::Extend::ConnectionHandler
16
15
  )
17
16
 
18
17
  ActiveRecord::ConnectionAdapters::Mysql2Adapter.__send__(
19
- :include, FreshConnection::Extend::Mysql2Adapter
18
+ :prepend, FreshConnection::Extend::Mysql2Adapter
20
19
  )
21
20
 
22
21
  if defined?(ActiveRecord::StatementCache)
23
22
  require 'fresh_connection/extend/ar_statement_cache'
24
- ActiveRecord::StatementCache.__send__(:include, FreshConnection::Extend::ArStatementCache)
23
+ ActiveRecord::StatementCache.__send__(:prepend, FreshConnection::Extend::ArStatementCache)
25
24
  end
26
25
 
27
26
  ActiveRecord::Base.establish_fresh_connection
@@ -1,4 +1,4 @@
1
1
  module FreshConnection
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
4
4
 
@@ -1,22 +1,21 @@
1
- require 'active_record'
2
- require 'fresh_connection/access_control'
1
+ require 'active_support/deprecation'
3
2
  require 'fresh_connection/connection_manager'
4
- require 'fresh_connection/slave_connection_handler'
5
3
 
6
4
  module FreshConnection
7
- extend ActiveSupport::Autoload
8
-
9
- autoload :ConnectionManager
10
- autoload :SlaveConnectionHandler
11
-
12
5
  class << self
13
- attr_writer :connection_manager, :env
6
+ attr_writer :connection_manager
14
7
 
15
8
  def connection_manager
16
9
  @connection_manager || ConnectionManager
17
10
  end
18
11
 
12
+ def env=(e)
13
+ ActiveSupport::Deprecation.warn("FreshConnection.env= has been deprecated.", caller)
14
+ @env = e
15
+ end
16
+
19
17
  def env
18
+ ActiveSupport::Deprecation.warn("FreshConnection.env has been deprecated.", caller)
20
19
  @env || defined?(Rails) && Rails.env
21
20
  end
22
21
 
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,6 @@ ENV["RAILS_ENV"]="test"
2
2
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
3
  require 'fresh_connection'
4
4
 
5
- FreshConnection.env = "test"
6
5
  require File.join(File.dirname(__FILE__), "prepare.rb")
7
6
 
8
7
  Dir[File.join(File.dirname(__FILE__), "/support/**/*.rb")].each {|f| require f}
@@ -90,21 +90,23 @@ describe FreshConnection do
90
90
  end
91
91
 
92
92
  it "specify readonly(false)" do
93
- data = [
94
- Address.readonly(false).first.prefecture,
95
- Address.includes(:user).readonly(false).first.user.name,
96
- Tel.readonly(false).first.number,
97
- Tel.includes(:user).readonly(false).first.user.name,
98
- @user.tels.readonly(false).first.number,
99
- User.where(id: 1).includes(:tels).readonly(false).first.tels.first.number,
100
- User.where(id: 1).includes(:address).readonly(false).first.address.prefecture,
101
- User.where(id: 1).joins(:address).where("addresses.user_id = 1").readonly(false).first.name,
102
- User.where(id: 1).readonly(false).pluck(:name).first
103
- ]
93
+ ActiveSupport::Deprecation.silence do
94
+ data = [
95
+ Address.readonly(false).first.prefecture,
96
+ Address.includes(:user).readonly(false).first.user.name,
97
+ Tel.readonly(false).first.number,
98
+ Tel.includes(:user).readonly(false).first.user.name,
99
+ @user.tels.readonly(false).first.number,
100
+ User.where(id: 1).includes(:tels).readonly(false).first.tels.first.number,
101
+ User.where(id: 1).includes(:address).readonly(false).first.address.prefecture,
102
+ User.where(id: 1).joins(:address).where("addresses.user_id = 1").readonly(false).first.name,
103
+ User.where(id: 1).readonly(false).pluck(:name).first
104
+ ]
104
105
 
105
- expect(data).to be_all{|n| n.include?("master")}
106
- expect(User.where(name: "Other").readonly(false).count).to eq(1)
107
- expect(User.where(name: "Other").count(:readonly => false)).to eq(1)
106
+ expect(data).to be_all{|n| n.include?("master")}
107
+ expect(User.where(name: "Other").readonly(false).count).to eq(1)
108
+ expect(User.where(name: "Other").count(:readonly => false)).to eq(1)
109
+ end
108
110
  end
109
111
  end
110
112
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fresh_connection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsukasa OISHI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-02 00:00:00.000000000 Z
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -142,6 +142,7 @@ files:
142
142
  - lib/fresh_connection.rb
143
143
  - lib/fresh_connection/abstract_connection_manager.rb
144
144
  - lib/fresh_connection/access_control.rb
145
+ - lib/fresh_connection/connection_factory.rb
145
146
  - lib/fresh_connection/connection_manager.rb
146
147
  - lib/fresh_connection/extend.rb
147
148
  - lib/fresh_connection/extend/ar_base.rb