fresh_connection 3.1.2 → 3.1.4

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +190 -152
  3. data/fresh_connection.gemspec +3 -3
  4. data/lib/fresh_connection/access_control.rb +144 -21
  5. data/lib/fresh_connection/connection_manager.rb +6 -2
  6. data/lib/fresh_connection/connection_specification/rails_70.rb +48 -0
  7. data/lib/fresh_connection/connection_specification.rb +10 -2
  8. data/lib/fresh_connection/deprecator.rb +10 -0
  9. data/lib/fresh_connection/executor_hook.rb +4 -1
  10. data/lib/fresh_connection/extend/adapters/base_adapter.rb +116 -11
  11. data/lib/fresh_connection/extend/adapters/m2_adapter.rb +2 -11
  12. data/lib/fresh_connection/extend/adapters/pg_adapter.rb +2 -14
  13. data/lib/fresh_connection/extend/adapters/trilogy_adapter.rb +15 -0
  14. data/lib/fresh_connection/extend/ar_base.rb +115 -3
  15. data/lib/fresh_connection/extend/ar_connection_handler.rb +42 -9
  16. data/lib/fresh_connection/extend/ar_relation.rb +105 -13
  17. data/lib/fresh_connection/extend/ar_relation_merger.rb +1 -1
  18. data/lib/fresh_connection/extend/ar_statement_cache.rb +39 -2
  19. data/lib/fresh_connection/extend.rb +49 -3
  20. data/lib/fresh_connection/rails_replica.rb +559 -0
  21. data/lib/fresh_connection/railtie.rb +13 -1
  22. data/lib/fresh_connection/replica_connection_handler.rb +1 -0
  23. data/lib/fresh_connection/version.rb +1 -1
  24. data/lib/fresh_connection.rb +46 -2
  25. metadata +22 -19
  26. data/.travis.yml +0 -24
  27. data/Appraisals +0 -12
  28. data/benchmarks/benchmark.rb +0 -25
  29. data/bin/test +0 -24
  30. data/gemfiles/rails52.gemfile +0 -8
  31. data/gemfiles/rails60.gemfile +0 -7
  32. data/gemfiles/rails61.gemfile +0 -7
@@ -13,30 +13,69 @@ module FreshConnection
13
13
  FreshConnection::AccessControl.manage_access(
14
14
  model: self,
15
15
  replica_access: false,
16
+ force_master: true,
16
17
  &block
17
18
  )
18
19
  end
19
20
 
20
- def connection
21
- super.tap {|c| c.model_class = self }
21
+ if ActiveRecord::VERSION::MAJOR < 7 || (ActiveRecord::VERSION::MAJOR == 7 && ActiveRecord::VERSION::MINOR < 2)
22
+ def connection
23
+ if FreshConnection::AccessControl.managed? &&
24
+ FreshConnection::RailsReplica.in_reading_role?(self) &&
25
+ !FreshConnection::RailsReplica.use_rails_replica?(self)
26
+ return FreshConnection::RailsReplica.with_writing(self) { super.tap { |c| c.model_class = self } }
27
+ end
28
+
29
+ super.tap { |c| c.model_class = self }
30
+ end
22
31
  end
23
32
 
24
33
  def replica_connection
25
- __replica_handler.connection(replica_spec_name)
34
+ if FreshConnection::RailsReplica.use_rails_replica?(self)
35
+ pool = FreshConnection::RailsReplica.reading_pool_for(self)
36
+ pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection
37
+ else
38
+ __replica_handler.connection(replica_spec_name)
39
+ end
26
40
  end
27
41
 
28
42
  def clear_all_replica_connections!
29
43
  __replica_handler.clear_all_connections!
44
+ FreshConnection::RailsReplica.clear_all_connections!
30
45
  end
31
46
 
32
47
  def establish_fresh_connection(spec_name = nil)
48
+ FreshConnection.deprecator.warn(
49
+ "establish_fresh_connection is deprecated and will be removed in FreshConnection 3.2.0. " \
50
+ "Please configure read replicas using Rails' native `connects_to` (database: { writing: ..., reading: ... }) instead."
51
+ )
52
+
33
53
  spec_name = spec_name.to_s
34
54
  spec_name = "replica" if spec_name.empty?
35
55
  @_replica_spec_name = spec_name
56
+ @_explicit_fresh_connection = true
36
57
 
37
58
  __replica_handler.refresh_connection(replica_spec_name)
38
59
  end
39
60
 
61
+ def explicit_fresh_connection?
62
+ conn_class = FreshConnection::RailsReplica.supported? ? FreshConnection::RailsReplica.connection_class_for(self) : ActiveRecord::Base
63
+ klass = self
64
+
65
+ loop do
66
+ if klass.instance_variable_defined?(:@_explicit_fresh_connection) &&
67
+ klass.instance_variable_get(:@_explicit_fresh_connection)
68
+ return true
69
+ end
70
+ break if klass == conn_class || klass == ActiveRecord::Base
71
+
72
+ klass = klass.superclass
73
+ break unless klass
74
+ end
75
+
76
+ false
77
+ end
78
+
40
79
  def master_db_only!
41
80
  @_fresh_connection_master_only = true
42
81
  end
@@ -47,6 +86,11 @@ module FreshConnection
47
86
  end
48
87
 
49
88
  def replica_spec_name
89
+ if FreshConnection::RailsReplica.use_rails_replica?(self)
90
+ pool = FreshConnection::RailsReplica.reading_pool_for(self)
91
+ return pool.db_config.name.to_s if pool && pool.respond_to?(:db_config)
92
+ end
93
+
50
94
  @_replica_spec_name ||= __search_replica_spec_name
51
95
  end
52
96
 
@@ -64,5 +108,73 @@ module FreshConnection
64
108
  FreshConnection::ReplicaConnectionHandler.instance
65
109
  end
66
110
  end
111
+
112
+ module ArQueryCache
113
+ def cache(&block)
114
+ if FreshConnection::RailsReplica.supported? && !FreshConnection.custom_connection_manager?
115
+ super { FreshConnection::RailsReplica.with_reading_pools_query_cache(enable: true, &block) }
116
+ else
117
+ super
118
+ end
119
+ end
120
+
121
+ if RUBY_VERSION >= "3.0"
122
+ def uncached(*args, **kwargs, &block)
123
+ if FreshConnection::RailsReplica.supported? && !FreshConnection.custom_connection_manager?
124
+ dirties = kwargs.fetch(:dirties, true)
125
+ super(*args, **kwargs) do
126
+ FreshConnection::RailsReplica.with_reading_pools_query_cache(enable: false, dirties: dirties, &block)
127
+ end
128
+ else
129
+ super(*args, **kwargs, &block)
130
+ end
131
+ end
132
+ else
133
+ def uncached(*args, &block)
134
+ if FreshConnection::RailsReplica.supported? && !FreshConnection.custom_connection_manager?
135
+ dirties = args.last.is_a?(Hash) && args.last.key?(:dirties) ? args.last[:dirties] : true
136
+ super(*args) do
137
+ FreshConnection::RailsReplica.with_reading_pools_query_cache(enable: false, dirties: dirties, &block)
138
+ end
139
+ else
140
+ super(*args, &block)
141
+ end
142
+ end
143
+ ruby2_keywords :uncached if respond_to?(:ruby2_keywords, true)
144
+ end
145
+ end
146
+
147
+ module ArConnectedTo
148
+ if RUBY_VERSION >= "3.0"
149
+ def connected_to(*args, **kwargs, &block)
150
+ FreshConnection::RailsReplica.with_user_connected_to(self, kwargs[:role]) do
151
+ super(*args, **kwargs, &block)
152
+ end
153
+ end
154
+ else
155
+ def connected_to(*args, &block)
156
+ role = args.last.is_a?(Hash) ? args.last[:role] : nil
157
+ FreshConnection::RailsReplica.with_user_connected_to(self, role) do
158
+ super(*args, &block)
159
+ end
160
+ end
161
+ ruby2_keywords :connected_to if respond_to?(:ruby2_keywords, true)
162
+ end
163
+ end
164
+
165
+ module ArCachedFindBy
166
+ private
167
+
168
+ if RUBY_VERSION >= "3.0"
169
+ def cached_find_by(*, **, &block)
170
+ all.manage_access { super }
171
+ end
172
+ else
173
+ def cached_find_by(*args, &block)
174
+ all.manage_access { super(*args, &block) }
175
+ end
176
+ ruby2_keywords :cached_find_by if respond_to?(:ruby2_keywords, true)
177
+ end
178
+ end
67
179
  end
68
180
  end
@@ -3,25 +3,58 @@
3
3
  module FreshConnection
4
4
  module Extend
5
5
  module ArConnectionHandler
6
- private def resolve_pool_config(*args)
7
- pool_config = super
6
+ if RUBY_VERSION >= "3.0"
7
+ private def resolve_pool_config(*, **)
8
+ pool_config = super
9
+ __extend_adapter_for_pool_config(pool_config)
10
+ pool_config
11
+ end
12
+ else
13
+ private def resolve_pool_config(*args)
14
+ pool_config = super(*args)
15
+ __extend_adapter_for_pool_config(pool_config)
16
+ pool_config
17
+ end
18
+ ruby2_keywords :resolve_pool_config if respond_to?(:ruby2_keywords, true)
19
+ end
20
+
21
+ private
8
22
 
9
- case pool_config.db_config.adapter.to_s
23
+ def __extend_adapter_for_pool_config(pool_config)
24
+ db_config = pool_config.db_config
25
+ adapter_name = db_config.adapter.to_s
26
+
27
+ case adapter_name
10
28
  when "mysql", "mysql2"
29
+ __load_adapter_class(db_config)
11
30
  require 'fresh_connection/extend/adapters/m2_adapter'
12
- __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::Mysql2Adapter, M2Adapter)
13
- when "postgresql"
31
+ if defined?(::ActiveRecord::ConnectionAdapters::Mysql2Adapter)
32
+ __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::Mysql2Adapter, M2Adapter)
33
+ end
34
+ when "trilogy"
35
+ __load_adapter_class(db_config)
36
+ require 'fresh_connection/extend/adapters/trilogy_adapter'
37
+ if defined?(::ActiveRecord::ConnectionAdapters::TrilogyAdapter)
38
+ __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::TrilogyAdapter, TrilogyAdapter)
39
+ end
40
+ when "postgresql", "postgis"
41
+ __load_adapter_class(db_config)
14
42
  require 'fresh_connection/extend/adapters/pg_adapter'
15
- __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, PgAdapter)
16
- else
17
- raise NotImplementedError, "This adapter('#{pool_config.db_config.adapter}') is not supported. If you specified the mysql or postgres adapter, it's probably a bug in FreshConnection. Please teach me (https://github.com/tsukasaoishi/fresh_connection/issues/new)"
43
+ if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
44
+ __extend_adapter_by_fc(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, PgAdapter)
45
+ end
18
46
  end
47
+ end
19
48
 
20
- pool_config
49
+ def __load_adapter_class(db_config)
50
+ db_config.adapter_class if db_config.respond_to?(:adapter_class)
51
+ rescue LoadError, StandardError
52
+ nil
21
53
  end
22
54
 
23
55
  def __extend_adapter_by_fc(klass, extend_adapter)
24
56
  return if klass.include?(extend_adapter)
57
+
25
58
  klass.prepend BaseAdapter
26
59
  klass.prepend extend_adapter
27
60
  end
@@ -3,16 +3,77 @@
3
3
  module FreshConnection
4
4
  module Extend
5
5
  module ArRelation
6
- def calculate(*)
7
- manage_access { super }
8
- end
6
+ if RUBY_VERSION >= "3.0"
7
+ def calculate(*, **, &block)
8
+ manage_access { super }
9
+ end
9
10
 
10
- def exists?(*)
11
- manage_access { super }
12
- end
11
+ def exists?(*, **, &block)
12
+ manage_access { super }
13
+ end
14
+
15
+ def pluck(*, **, &block)
16
+ manage_access { super }
17
+ end
18
+
19
+ def ids(*, **, &block)
20
+ manage_access { super }
21
+ end
22
+
23
+ if ActiveRecord::Relation.method_defined?(:load_async)
24
+ def load_async(*, **, &block)
25
+ if FreshConnection::RailsReplica.use_rails_replica?(klass)
26
+ # Return nil from manage_access so Rails' connected_to does not force synchronous `.load` on the returned Relation.
27
+ result = nil
28
+ manage_access do
29
+ result = super
30
+ nil
31
+ end
32
+ result
33
+ else
34
+ super
35
+ end
36
+ end
37
+ end
38
+ else
39
+ def calculate(*args, &block)
40
+ manage_access { super(*args, &block) }
41
+ end
42
+
43
+ def exists?(*args, &block)
44
+ manage_access { super(*args, &block) }
45
+ end
46
+
47
+ def pluck(*args, &block)
48
+ manage_access { super(*args, &block) }
49
+ end
50
+
51
+ def ids(*args, &block)
52
+ manage_access { super(*args, &block) }
53
+ end
13
54
 
14
- def pluck(*)
15
- manage_access { super }
55
+ if ActiveRecord::Relation.method_defined?(:load_async)
56
+ def load_async(*args, &block)
57
+ if FreshConnection::RailsReplica.use_rails_replica?(klass)
58
+ result = nil
59
+ manage_access do
60
+ result = super(*args, &block)
61
+ nil
62
+ end
63
+ result
64
+ else
65
+ super(*args, &block)
66
+ end
67
+ end
68
+ end
69
+
70
+ if respond_to?(:ruby2_keywords, true)
71
+ ruby2_keywords :calculate
72
+ ruby2_keywords :exists?
73
+ ruby2_keywords :pluck
74
+ ruby2_keywords :ids
75
+ ruby2_keywords :load_async if method_defined?(:load_async)
76
+ end
16
77
  end
17
78
 
18
79
  def read_master
@@ -29,26 +90,57 @@ module FreshConnection
29
90
  end
30
91
 
31
92
  def read_master_value=(value)
32
- raise ImmutableRelation if @loaded
93
+ if respond_to?(:assert_mutability!, true)
94
+ assert_mutability!
95
+ elsif @loaded
96
+ raise(
97
+ defined?(ActiveRecord::UnmodifiableRelation) ? ActiveRecord::UnmodifiableRelation : ActiveRecord::ImmutableRelation
98
+ )
99
+ end
33
100
  @values[:read_master] = value
34
101
  end
35
102
 
36
103
  def manage_access(replica_access: enable_replica_access, &block)
37
104
  FreshConnection::AccessControl.manage_access(
38
- model: @klass,
105
+ model: klass,
39
106
  replica_access: replica_access,
107
+ force_master: force_master_access?,
40
108
  &block
41
109
  )
42
110
  end
43
111
 
44
112
  private
45
113
 
46
- def exec_queries
47
- manage_access { super }
114
+ def exec_queries(&block)
115
+ manage_access { super(&block) }
116
+ end
117
+
118
+ if ActiveRecord::Relation.private_method_defined?(:compute_cache_version)
119
+ if RUBY_VERSION >= "3.0"
120
+ def compute_cache_version(*, **, &block)
121
+ manage_access { super }
122
+ end
123
+ else
124
+ def compute_cache_version(*args, &block)
125
+ manage_access { super(*args, &block) }
126
+ end
127
+ ruby2_keywords :compute_cache_version if respond_to?(:ruby2_keywords, true)
128
+ end
129
+ end
130
+
131
+ def force_master_access?
132
+ return true if read_master_value
133
+ return true if respond_to?(:lock_value) && lock_value
134
+ return true if FreshConnection::RailsReplica.explicitly_connected_to_writing?(klass)
135
+
136
+ false
48
137
  end
49
138
 
50
139
  def enable_replica_access
51
- connection.open_transactions.zero? && !read_master_value
140
+ return false if force_master_access?
141
+ return false if FreshConnection::AccessControl.in_transaction?(klass)
142
+
143
+ true
52
144
  end
53
145
  end
54
146
  end
@@ -6,7 +6,7 @@ module FreshConnection
6
6
  private
7
7
 
8
8
  def merge_single_values
9
- relation.read_master_value = values[:read_master] unless relation.read_master_value
9
+ relation.read_master_value = values[:read_master] if values[:read_master] && !relation.read_master_value
10
10
  super
11
11
  end
12
12
  end
@@ -3,8 +3,45 @@
3
3
  module FreshConnection
4
4
  module Extend
5
5
  module ArStatementCache
6
- def execute(params, connection, &block)
7
- klass.all.manage_access { super }
6
+ if RUBY_VERSION >= "3.0"
7
+ def execute(params, connection, **kwargs, &block)
8
+ model = (defined?(@model) && @model) || klass
9
+ if FreshConnection::AccessControl.managed? && FreshConnection::AccessControl.current_model == model
10
+ return super(params, connection, **kwargs, &block)
11
+ end
12
+
13
+ model.all.manage_access do
14
+ if FreshConnection::RailsReplica.use_rails_replica?(model)
15
+ if model.respond_to?(:with_connection)
16
+ model.with_connection { |conn| super(params, conn, **kwargs, &block) }
17
+ else
18
+ super(params, model.connection, **kwargs, &block)
19
+ end
20
+ else
21
+ super(params, connection, **kwargs, &block)
22
+ end
23
+ end
24
+ end
25
+ else
26
+ def execute(params, connection, *rest, &block)
27
+ model = (defined?(@model) && @model) || klass
28
+ if FreshConnection::AccessControl.managed? && FreshConnection::AccessControl.current_model == model
29
+ return super(params, connection, *rest, &block)
30
+ end
31
+
32
+ model.all.manage_access do
33
+ if FreshConnection::RailsReplica.use_rails_replica?(model)
34
+ if model.respond_to?(:with_connection)
35
+ model.with_connection { |conn| super(params, conn, *rest, &block) }
36
+ else
37
+ super(params, model.connection, *rest, &block)
38
+ end
39
+ else
40
+ super(params, connection, *rest, &block)
41
+ end
42
+ end
43
+ end
44
+ ruby2_keywords :execute if respond_to?(:ruby2_keywords, true)
8
45
  end
9
46
  end
10
47
  end
@@ -2,8 +2,19 @@
2
2
  require 'active_support'
3
3
 
4
4
  ActiveSupport.on_load(:active_record) do
5
- if respond_to?(:connection_handlers) && connection_handlers.empty?
6
- self.connection_handlers = { writing_role => ActiveRecord::Base.default_connection_handler }
5
+ if ActiveRecord::VERSION::MAJOR < 7
6
+ FreshConnection.deprecator.warn(
7
+ "Support for ActiveRecord < 7.0 (current: #{ActiveRecord::VERSION::STRING}) is deprecated " \
8
+ "and will be removed in FreshConnection 3.2.0. Please upgrade to Rails 7.0 or later."
9
+ )
10
+ end
11
+
12
+ if (ActiveRecord::VERSION::MAJOR == 6 && ActiveRecord::VERSION::MINOR == 0) ||
13
+ FreshConnection::RailsReplica.send(:legacy_connection_handling?)
14
+ if respond_to?(:connection_handlers) && connection_handlers.empty?
15
+ w_role = ActiveRecord.respond_to?(:writing_role) ? ActiveRecord.writing_role : writing_role
16
+ self.connection_handlers = { w_role => ActiveRecord::Base.default_connection_handler }
17
+ end
7
18
  end
8
19
 
9
20
  require 'fresh_connection/extend/ar_base'
@@ -12,11 +23,28 @@ ActiveSupport.on_load(:active_record) do
12
23
  require 'fresh_connection/extend/ar_statement_cache'
13
24
 
14
25
  ActiveRecord::Base.extend FreshConnection::Extend::ArBase
26
+ ActiveRecord::Base.singleton_class.prepend FreshConnection::Extend::ArQueryCache
27
+ if FreshConnection::RailsReplica.supported? && ActiveRecord::Base.respond_to?(:connected_to)
28
+ ActiveRecord::Base.singleton_class.prepend FreshConnection::Extend::ArConnectedTo
29
+ end
30
+ if ActiveRecord::Base.respond_to?(:cached_find_by, true)
31
+ ActiveRecord::Base.singleton_class.prepend FreshConnection::Extend::ArCachedFindBy
32
+ end
15
33
  ActiveRecord::Relation.prepend FreshConnection::Extend::ArRelation
16
34
  ActiveRecord::Relation::Merger.prepend FreshConnection::Extend::ArRelationMerger
17
35
  ActiveRecord::StatementCache.prepend FreshConnection::Extend::ArStatementCache
18
36
 
19
- if ActiveRecord::VERSION::MAJOR == 6 && ActiveRecord::VERSION::MINOR == 1
37
+ if ActiveRecord::VERSION::MAJOR >= 7
38
+ require 'fresh_connection/extend/ar_connection_handler'
39
+ ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(
40
+ FreshConnection::Extend::ArConnectionHandler
41
+ )
42
+
43
+ require 'fresh_connection/connection_specification/rails_70'
44
+ FreshConnection::ConnectionSpecification.include(
45
+ FreshConnection::ConnectionSpecification::Rails70
46
+ )
47
+ elsif ActiveRecord::VERSION::MAJOR == 6 && ActiveRecord::VERSION::MINOR == 1
20
48
  require 'fresh_connection/extend/ar_connection_handler'
21
49
  ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(
22
50
  FreshConnection::Extend::ArConnectionHandler
@@ -37,4 +65,22 @@ ActiveSupport.on_load(:active_record) do
37
65
  FreshConnection::ConnectionSpecification::Rails60
38
66
  )
39
67
  end
68
+
69
+ # Extend any supported adapters that were already loaded before `require "fresh_connection"`
70
+ {
71
+ "Mysql2Adapter" => ['fresh_connection/extend/adapters/m2_adapter', :M2Adapter],
72
+ "TrilogyAdapter" => ['fresh_connection/extend/adapters/trilogy_adapter', :TrilogyAdapter],
73
+ "PostgreSQLAdapter" => ['fresh_connection/extend/adapters/pg_adapter', :PgAdapter]
74
+ }.each do |adapter_const, (req_path, ext_const)|
75
+ next if ActiveRecord::ConnectionAdapters.autoload?(adapter_const)
76
+ next unless ActiveRecord::ConnectionAdapters.const_defined?(adapter_const, false)
77
+
78
+ require req_path
79
+ klass = ActiveRecord::ConnectionAdapters.const_get(adapter_const, false)
80
+ ext = FreshConnection::Extend.const_get(ext_const, false)
81
+ unless klass.include?(ext)
82
+ klass.prepend FreshConnection::Extend::BaseAdapter
83
+ klass.prepend ext
84
+ end
85
+ end
40
86
  end