fresh_connection 3.0.0 → 3.0.1.rc1

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
  SHA256:
3
- metadata.gz: 4b67bc1788b59977212ab408786a2c50de89e59834dcb7eabfd45fd5a8b556aa
4
- data.tar.gz: '0555593d5a5e54322e767ea4475d86263ea87c48d2aa618156ace835e5881389'
3
+ metadata.gz: eebb00ba4ca2e191f447c993f17e4f4f8977c5b3c55fe02656ba49b7c1432f29
4
+ data.tar.gz: 9d5e1ccd79082c4f6d726660f253d4668501db8c994ab317f16d6ff7e2f920c7
5
5
  SHA512:
6
- metadata.gz: e20fade2abed85117b4dc3e55c7188ee85cdf723a0a65a270f3b53105812ff416d0d4afe94a366f87fd24dc51352d332d766b1a7622d974ec19426b0008e4087
7
- data.tar.gz: 3de5c92aac34aa8fa73f556e53e0063857020724578779d8eb0f197865baf230e09c348666f49685281971d2239eb41ad7449fc2d783f16026a250f21a1d14f1
6
+ metadata.gz: 1bef0f2714efabf655bc0053a5b9e9996be94889384ff991faebcaf304d43b26bc8faf6cb3c6a5e5d475f9d2f3549b58d0b6c344fcff16cf26640ba0386736c8
7
+ data.tar.gz: 724ec092bf65e49f6832b7d78683c585ca7f2a3052658f50b36a5428e595a2dd12057543813aff4e99902d7183a2bd86ea48a976dd5b67419b86874c95978557
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FreshConnection
4
+ module Extend
5
+ module BaseAdapter
6
+ def self.prepended(base)
7
+ base.send :attr_writer, :model_class
8
+ end
9
+
10
+ def log(*args)
11
+ args[1] = "[#{__replica_spec_name}] #{args[1]}" if __replica_spec_name
12
+ super
13
+ end
14
+
15
+ def select_all(*args)
16
+ __change_connection { super }
17
+ end
18
+
19
+ def select_value(*args)
20
+ __change_connection { super }
21
+ end
22
+
23
+ private
24
+
25
+ def __replica_spec_name
26
+ return nil if !defined?(@model_class) || !@model_class
27
+ return nil unless FreshConnection::AccessControl.replica_access?
28
+ @model_class.replica_spec_name
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
+ require 'fresh_connection/extend/adapters/base_adapter'
2
3
 
3
4
  module FreshConnection
4
5
  module Extend
5
6
  module M2Adapter
6
7
  private
7
8
 
8
- def change_connection
9
+ def __change_connection
9
10
  return yield unless FreshConnection::AccessControl.replica_access?
10
11
 
11
12
  master_connection = @connection
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
+ require 'fresh_connection/extend/adapters/base_adapter'
2
3
 
3
4
  module FreshConnection
4
5
  module Extend
5
6
  module PgAdapter
6
7
  private
7
8
 
8
- def change_connection
9
+ def __change_connection
9
10
  return yield unless FreshConnection::AccessControl.replica_access?
10
11
 
11
12
  master_connection = @connection
@@ -34,7 +34,7 @@ module FreshConnection
34
34
  spec_name = "replica" if spec_name.empty?
35
35
  @_replica_spec_name = spec_name
36
36
 
37
- __replica_handler.establish_connection(replica_spec_name)
37
+ __replica_handler.refresh_connection(replica_spec_name)
38
38
  end
39
39
 
40
40
  def master_db_only!
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FreshConnection
4
+ module Extend
5
+ module ArResolver
6
+ def spec(*args)
7
+ specification = super
8
+
9
+ case specification.config[:adapter].to_s
10
+ when "mysql2"
11
+ require 'fresh_connection/extend/adapters/m2_adapter'
12
+ __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::Mysql2Adapter, M2Adapter)
13
+ when "postgresql"
14
+ require 'fresh_connection/extend/adapters/pg_adapter'
15
+ __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, PgAdapter)
16
+ end
17
+
18
+ specification
19
+ end
20
+
21
+ def __extend_adapter_by_fc(klass, extend_adapter)
22
+ return if klass.include?(extend_adapter)
23
+ klass.prepend BaseAdapter
24
+ klass.prepend extend_adapter
25
+ end
26
+ end
27
+ end
28
+ end
@@ -6,11 +6,13 @@ ActiveSupport.on_load(:active_record) do
6
6
  require 'fresh_connection/extend/ar_relation'
7
7
  require 'fresh_connection/extend/ar_relation_merger'
8
8
  require 'fresh_connection/extend/ar_statement_cache'
9
- require 'fresh_connection/extend/ar_abstract_adapter'
9
+ require 'fresh_connection/extend/ar_resolver'
10
10
 
11
11
  ActiveRecord::Base.extend FreshConnection::Extend::ArBase
12
12
  ActiveRecord::Relation.prepend FreshConnection::Extend::ArRelation
13
13
  ActiveRecord::Relation::Merger.prepend FreshConnection::Extend::ArRelationMerger
14
14
  ActiveRecord::StatementCache.prepend FreshConnection::Extend::ArStatementCache
15
- ActiveRecord::ConnectionAdapters::AbstractAdapter.extend FreshConnection::Extend::ArAbstractAdapter
15
+ ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.prepend(
16
+ FreshConnection::Extend::ArResolver
17
+ )
16
18
  end
@@ -12,19 +12,12 @@ module FreshConnection
12
12
  end
13
13
  end
14
14
 
15
- def establish_connection(spec_name)
16
- spec_name = spec_name.to_s
17
- remove_connection(spec_name)
18
-
19
- message_bus = ActiveSupport::Notifications.instrumenter
20
- payload = {
21
- connection_id: object_id,
22
- spec_name: spec_name
23
- }
24
-
25
- message_bus.instrument("!connection.active_record", payload) do
26
- owner_to_pool[spec_name] = FreshConnection.connection_manager.new(spec_name)
27
- end
15
+ def refresh_all
16
+ owner_to_pool.clear
17
+ end
18
+
19
+ def refresh_connection(spec_name)
20
+ remove_connection(spec_name.to_s)
28
21
  end
29
22
 
30
23
  def connection(spec_name)
@@ -64,7 +57,17 @@ module FreshConnection
64
57
 
65
58
  def detect_connection_manager(spec_name)
66
59
  owner_to_pool.fetch(spec_name.to_s) do
67
- establish_connection(spec_name)
60
+ refresh_connection(spec_name)
61
+
62
+ message_bus = ActiveSupport::Notifications.instrumenter
63
+ payload = {
64
+ connection_id: object_id,
65
+ spec_name: spec_name
66
+ }
67
+
68
+ message_bus.instrument("!connection.active_record", payload) do
69
+ FreshConnection.connection_manager.new(spec_name)
70
+ end
68
71
  end
69
72
  end
70
73
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FreshConnection
4
- VERSION = "3.0.0"
4
+ VERSION = "3.0.1.rc1"
5
5
  end
6
6
 
@@ -1,23 +1,28 @@
1
1
  # frozen_string_literal: true
2
- require 'fresh_connection/connection_manager'
3
2
 
4
3
  module FreshConnection
5
4
  class << self
6
- attr_writer :connection_manager
7
-
8
5
  def connection_manager
9
6
  if defined?(@connection_manager)
10
7
  @connection_manager
11
8
  else
9
+ require 'fresh_connection/connection_manager'
12
10
  ConnectionManager
13
11
  end
14
12
  end
15
13
 
14
+ def connection_manager=(mgr)
15
+ FreshConnection::ReplicaConnectionHandler.instance.refresh_all
16
+ @connection_manager = mgr
17
+ end
18
+
16
19
  def rails_52?
17
20
  [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [5, 2]
18
21
  end
19
22
  end
20
23
  end
21
24
 
25
+ require 'fresh_connection/abstract_connection_manager'
26
+ require 'fresh_connection/connection_specification'
22
27
  require 'fresh_connection/extend'
23
28
  require 'fresh_connection/railtie' if defined?(Rails)
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: 3.0.0
4
+ version: 3.0.1.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsukasa OISHI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-20 00:00:00.000000000 Z
11
+ date: 2018-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -220,17 +220,17 @@ files:
220
220
  - lib/fresh_connection.rb
221
221
  - lib/fresh_connection/abstract_connection_manager.rb
222
222
  - lib/fresh_connection/access_control.rb
223
- - lib/fresh_connection/check_adapter.rb
224
223
  - lib/fresh_connection/connection_manager.rb
225
224
  - lib/fresh_connection/connection_specification.rb
226
225
  - lib/fresh_connection/executor_hook.rb
227
226
  - lib/fresh_connection/extend.rb
227
+ - lib/fresh_connection/extend/adapters/base_adapter.rb
228
228
  - lib/fresh_connection/extend/adapters/m2_adapter.rb
229
229
  - lib/fresh_connection/extend/adapters/pg_adapter.rb
230
- - lib/fresh_connection/extend/ar_abstract_adapter.rb
231
230
  - lib/fresh_connection/extend/ar_base.rb
232
231
  - lib/fresh_connection/extend/ar_relation.rb
233
232
  - lib/fresh_connection/extend/ar_relation_merger.rb
233
+ - lib/fresh_connection/extend/ar_resolver.rb
234
234
  - lib/fresh_connection/extend/ar_statement_cache.rb
235
235
  - lib/fresh_connection/railtie.rb
236
236
  - lib/fresh_connection/replica_connection_handler.rb
@@ -251,9 +251,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
251
251
  version: '2.2'
252
252
  required_rubygems_version: !ruby/object:Gem::Requirement
253
253
  requirements:
254
- - - ">="
254
+ - - ">"
255
255
  - !ruby/object:Gem::Version
256
- version: '0'
256
+ version: 1.3.1
257
257
  requirements: []
258
258
  rubyforge_project:
259
259
  rubygems_version: 2.7.6
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module FreshConnection
4
- module CheckAdapter
5
- class << self
6
- def check(klass)
7
- if mysql?(klass)
8
- :mysql
9
- elsif postgresql?(klass)
10
- :postgresql
11
- end
12
- end
13
-
14
- def mysql?(klass)
15
- if defined?(::ActiveRecord::ConnectionAdapters::Mysql2Adapter)
16
- klass == ::ActiveRecord::ConnectionAdapters::Mysql2Adapter
17
- else
18
- false
19
- end
20
- end
21
-
22
- def postgresql?(klass)
23
- if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
24
- klass == ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
25
- else
26
- false
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'fresh_connection/check_adapter'
3
-
4
- module FreshConnection
5
- module Extend
6
- module ArAbstractAdapter
7
- def inherited(klass)
8
- case FreshConnection::CheckAdapter.check(klass)
9
- when :mysql
10
- klass.prepend BaseAdapter
11
- require 'fresh_connection/extend/adapters/m2_adapter'
12
- klass.prepend M2Adapter
13
- when :postgresql
14
- klass.prepend BaseAdapter
15
- require 'fresh_connection/extend/adapters/pg_adapter'
16
- klass.prepend PgAdapter
17
- end
18
- end
19
- end
20
-
21
- module BaseAdapter
22
- def self.prepended(base)
23
- base.send :attr_writer, :model_class
24
- end
25
-
26
- def log(*args)
27
- args[1] = "[#{__replica_spec_name}] #{args[1]}" if __replica_spec_name
28
- super
29
- end
30
-
31
- def select_all(*args)
32
- change_connection { super }
33
- end
34
-
35
- def select_value(*args)
36
- change_connection { super }
37
- end
38
-
39
- private
40
-
41
- def __replica_spec_name
42
- return nil if !defined?(@model_class) || !@model_class
43
- @model_class.replica_spec_name
44
- end
45
- end
46
- end
47
- end