janus-ar 0.15.2 → 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,144 +1,148 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record/connection_adapters/abstract_adapter'
4
- require 'active_record/connection_adapters/trilogy_adapter'
5
- require_relative '../../janus'
6
-
7
- module ActiveRecord
8
- module ConnectionHandling
9
- def janus_trilogy_connection(config)
10
- ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter.new(config)
11
- end
12
- end
13
- end
14
-
15
- module ActiveRecord
16
- class Base
17
- def self.janus_trilogy_adapter_class
18
- ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter
19
- end
20
- end
21
- end
22
-
23
- module ActiveRecord
24
- module ConnectionAdapters
25
- class JanusTrilogyAdapter < ActiveRecord::ConnectionAdapters::TrilogyAdapter
26
- FOUND_ROWS = 'FOUND_ROWS'
27
-
28
- attr_reader :config
29
-
30
- class << self
31
- def dbconsole(config, options = {})
32
- connection_config = Janus::DbConsoleConfig.new(config)
33
-
34
- super(connection_config, options)
35
- end
36
- end
37
-
38
- def initialize(*args)
39
- args[0][:janus]['replica']['database'] = args[0][:database]
40
- args[0][:janus]['primary']['database'] = args[0][:database]
41
-
42
- @replica_config = args[0][:janus]['replica'].symbolize_keys
43
- args[0] = args[0][:janus]['primary'].symbolize_keys
44
-
45
- super(*args)
46
- @connection_parameters ||= args[0]
47
- update_config
48
- end
49
-
50
- def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
51
- case where_to_send?(sql)
52
- when :all
53
- send_to_replica(sql, connection: :all, method: :execute)
54
- super
55
- when :replica
56
- send_to_replica(sql, connection: :replica, method: :execute)
57
- else
58
- Janus::Context.stick_to_primary if write_query?(sql)
59
- Janus::Context.used_connection(:primary)
60
- super
61
- end
62
- end
63
-
64
- def execute(sql)
65
- case where_to_send?(sql)
66
- when :all
67
- send_to_replica(sql, connection: :all, method: :execute)
68
- super(sql)
69
- when :replica
70
- send_to_replica(sql, connection: :replica, method: :execute)
71
- else
72
- Janus::Context.stick_to_primary if write_query?(sql)
73
- Janus::Context.used_connection(:primary)
74
- super(sql)
75
- end
76
- end
77
-
78
- def execute_and_free(sql, name = nil, async: false)
79
- case where_to_send?(sql)
80
- when :all
81
- send_to_replica(sql, connection: :all, method: :execute)
82
- super(sql, name, async:)
83
- when :replica
84
- send_to_replica(sql, connection: :replica, method: :execute)
85
- else
86
- Janus::Context.stick_to_primary if write_query?(sql)
87
- Janus::Context.used_connection(:primary)
88
- super(sql, name, async:)
89
- end
90
- end
91
-
92
- def connect!(...)
93
- replica_connection.connect!(...)
94
- super
95
- end
96
-
97
- def reconnect!(...)
98
- replica_connection.reconnect!(...)
99
- super
100
- end
101
-
102
- def disconnect!(...)
103
- replica_connection.disconnect!(...)
104
- super
105
- end
106
-
107
- def clear_cache!(...)
108
- replica_connection.clear_cache!(...)
109
- super
110
- end
111
-
112
- def replica_connection
113
- @replica_connection ||= ActiveRecord::ConnectionAdapters::TrilogyAdapter.new(@replica_config)
114
- end
115
-
116
- private
117
-
118
- def where_to_send?(sql)
119
- Janus::QueryDirector.new(sql, open_transactions).where_to_send?
120
- end
121
-
122
- def send_to_replica(sql, connection: nil, method: :exec_query)
123
- Janus::Context.used_connection(connection) if connection
124
- if method == :execute
125
- replica_connection.execute(sql)
126
- elsif method == :raw_execute
127
- replica_connection.execute(sql)
128
- else
129
- replica_connection.exec_query(sql)
130
- end
131
- end
132
-
133
- def update_config
134
- @config[:flags] ||= 0
135
-
136
- if @config[:flags].is_a? Array
137
- @config[:flags].push FOUND_ROWS
138
- else
139
- @config[:flags] |= ::Janus::Client::FOUND_ROWS
140
- end
141
- end
142
- end
143
- end
144
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record/connection_adapters/abstract_adapter'
4
+ require 'active_record/connection_adapters/trilogy_adapter'
5
+ require_relative '../../../janus-ar'
6
+
7
+ module ActiveRecord
8
+ module ConnectionHandling
9
+ def janus_trilogy_connection(config)
10
+ ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter.new(config)
11
+ end
12
+ end
13
+ end
14
+
15
+ module ActiveRecord
16
+ class Base
17
+ def self.janus_trilogy_adapter_class
18
+ ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter
19
+ end
20
+ end
21
+ end
22
+
23
+ module ActiveRecord
24
+ module ConnectionAdapters
25
+ class JanusTrilogyAdapter < ActiveRecord::ConnectionAdapters::TrilogyAdapter
26
+ FOUND_ROWS = 'FOUND_ROWS'
27
+
28
+ attr_reader :config
29
+
30
+ class << self
31
+ def dbconsole(config, options = {})
32
+ connection_config = Janus::DbConsoleConfig.new(config)
33
+
34
+ super(connection_config, options)
35
+ end
36
+ end
37
+
38
+ def initialize(*args)
39
+ args[0][:janus]['replica']['database'] = args[0][:database]
40
+ args[0][:janus]['primary']['database'] = args[0][:database]
41
+
42
+ @replica_config = args[0][:janus]['replica'].symbolize_keys
43
+ args[0] = args[0][:janus]['primary'].symbolize_keys
44
+
45
+ super(*args)
46
+ @connection_parameters ||= args[0]
47
+ update_config
48
+ end
49
+
50
+ def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
51
+ case where_to_send?(sql)
52
+ when :all
53
+ send_to_replica(sql, connection: :all, method: :execute)
54
+ super
55
+ when :replica
56
+ send_to_replica(sql, connection: :replica, method: :execute)
57
+ else
58
+ Janus::Context.stick_to_primary if write_query?(sql)
59
+ Janus::Context.used_connection(:primary)
60
+ super
61
+ end
62
+ end
63
+
64
+ def execute(sql)
65
+ case where_to_send?(sql)
66
+ when :all
67
+ send_to_replica(sql, connection: :all, method: :execute)
68
+ super(sql)
69
+ when :replica
70
+ send_to_replica(sql, connection: :replica, method: :execute)
71
+ else
72
+ Janus::Context.stick_to_primary if write_query?(sql)
73
+ Janus::Context.used_connection(:primary)
74
+ super(sql)
75
+ end
76
+ end
77
+
78
+ def execute_and_free(sql, name = nil, async: false)
79
+ case where_to_send?(sql)
80
+ when :all
81
+ send_to_replica(sql, connection: :all, method: :execute)
82
+ super(sql, name, async:)
83
+ when :replica
84
+ send_to_replica(sql, connection: :replica, method: :execute)
85
+ else
86
+ Janus::Context.stick_to_primary if write_query?(sql)
87
+ Janus::Context.used_connection(:primary)
88
+ super(sql, name, async:)
89
+ end
90
+ end
91
+
92
+ def with_connection(_args = {})
93
+ self
94
+ end
95
+
96
+ def connect!(...)
97
+ replica_connection.connect!(...)
98
+ super
99
+ end
100
+
101
+ def reconnect!(...)
102
+ replica_connection.reconnect!(...)
103
+ super
104
+ end
105
+
106
+ def disconnect!(...)
107
+ replica_connection.disconnect!(...)
108
+ super
109
+ end
110
+
111
+ def clear_cache!(...)
112
+ replica_connection.clear_cache!(...)
113
+ super
114
+ end
115
+
116
+ def replica_connection
117
+ @replica_connection ||= ActiveRecord::ConnectionAdapters::TrilogyAdapter.new(@replica_config)
118
+ end
119
+
120
+ private
121
+
122
+ def where_to_send?(sql)
123
+ Janus::QueryDirector.new(sql, open_transactions).where_to_send?
124
+ end
125
+
126
+ def send_to_replica(sql, connection: nil, method: :exec_query)
127
+ Janus::Context.used_connection(connection) if connection
128
+ if method == :execute
129
+ replica_connection.execute(sql)
130
+ elsif method == :raw_execute
131
+ replica_connection.execute(sql)
132
+ else
133
+ replica_connection.exec_query(sql)
134
+ end
135
+ end
136
+
137
+ def update_config
138
+ @config[:flags] ||= 0
139
+
140
+ if @config[:flags].is_a? Array
141
+ @config[:flags].push FOUND_ROWS
142
+ else
143
+ @config[:flags] |= ::Janus::Client::FOUND_ROWS
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
- module Janus
3
- class Client
4
- FOUND_ROWS = 2
5
- end
6
- end
1
+ # frozen_string_literal: true
2
+ module Janus
3
+ class Client
4
+ FOUND_ROWS = 2
5
+ end
6
+ end
@@ -1,80 +1,80 @@
1
- # frozen_string_literal: true
2
-
3
- module Janus
4
- class Context
5
- THREAD_KEY = :janus_ar_context
6
-
7
- # Stores the staged data with an expiration time based on the current time,
8
- # and clears any expired entries. Returns true if any changes were made to
9
- # the current store
10
- def initialize(primary: false, expiry: nil)
11
- @primary = primary
12
- @expiry = expiry
13
- @last_used_connection = :primary
14
- end
15
-
16
- def stick_to_primary
17
- @primary = true
18
- end
19
-
20
- def potential_write
21
- stick_to_primary
22
- end
23
-
24
- def release_all
25
- @primary = false
26
- @expiry = nil
27
- @last_used_connection = nil
28
- end
29
-
30
- def use_primary?
31
- @primary
32
- end
33
-
34
- def used_connection(connection)
35
- @last_used_connection = connection
36
- end
37
-
38
- attr_reader :last_used_connection
39
-
40
- class << self
41
- def stick_to_primary
42
- current.stick_to_primary
43
- end
44
-
45
- def release_all
46
- current.release_all
47
- end
48
-
49
- def used_connection(connection)
50
- current.used_connection(connection)
51
- end
52
-
53
- def use_primary?
54
- current.use_primary?
55
- end
56
-
57
- def last_used_connection
58
- current.last_used_connection
59
- end
60
-
61
- protected
62
-
63
- def current
64
- fetch(THREAD_KEY) { new }
65
- end
66
-
67
- def fetch(key)
68
- get(key) || set(key, yield)
69
- end
70
-
71
- def get(key)
72
- Thread.current.thread_variable_get(key)
73
- end
74
-
75
- def set(key, value)
76
- Thread.current.thread_variable_set(key, value)
77
- end
78
- end
79
- end
80
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Janus
4
+ class Context
5
+ THREAD_KEY = :janus_ar_context
6
+
7
+ # Stores the staged data with an expiration time based on the current time,
8
+ # and clears any expired entries. Returns true if any changes were made to
9
+ # the current store
10
+ def initialize(primary: false, expiry: nil)
11
+ @primary = primary
12
+ @expiry = expiry
13
+ @last_used_connection = :primary
14
+ end
15
+
16
+ def stick_to_primary
17
+ @primary = true
18
+ end
19
+
20
+ def potential_write
21
+ stick_to_primary
22
+ end
23
+
24
+ def release_all
25
+ @primary = false
26
+ @expiry = nil
27
+ @last_used_connection = nil
28
+ end
29
+
30
+ def use_primary?
31
+ @primary
32
+ end
33
+
34
+ def used_connection(connection)
35
+ @last_used_connection = connection
36
+ end
37
+
38
+ attr_reader :last_used_connection
39
+
40
+ class << self
41
+ def stick_to_primary
42
+ current.stick_to_primary
43
+ end
44
+
45
+ def release_all
46
+ current.release_all
47
+ end
48
+
49
+ def used_connection(connection)
50
+ current.used_connection(connection)
51
+ end
52
+
53
+ def use_primary?
54
+ current.use_primary?
55
+ end
56
+
57
+ def last_used_connection
58
+ current.last_used_connection
59
+ end
60
+
61
+ protected
62
+
63
+ def current
64
+ fetch(THREAD_KEY) { new }
65
+ end
66
+
67
+ def fetch(key)
68
+ get(key) || set(key, yield)
69
+ end
70
+
71
+ def get(key)
72
+ Thread.current.thread_variable_get(key)
73
+ end
74
+
75
+ def set(key, value)
76
+ Thread.current.thread_variable_set(key, value)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,17 +1,17 @@
1
- # frozen_string_literal: true
2
-
3
- module Janus
4
- class DbConsoleConfig
5
- def initialize(config)
6
- @_config = config.configuration_hash
7
- end
8
-
9
- def configuration_hash
10
- @_config[:janus]['replica'].symbolize_keys
11
- end
12
-
13
- def database
14
- @_config[:database]
15
- end
16
- end
17
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Janus
4
+ class DbConsoleConfig
5
+ def initialize(config)
6
+ @_config = config.configuration_hash
7
+ end
8
+
9
+ def configuration_hash
10
+ @_config[:janus]['replica'].symbolize_keys
11
+ end
12
+
13
+ def database
14
+ @_config[:database]
15
+ end
16
+ end
17
+ end
@@ -1,15 +1,15 @@
1
- # frozen_string_literal: true
2
-
3
- module Janus
4
- module Logging
5
- class Logger
6
- class << self
7
- def log(message, format = :info)
8
- logger&.send(format, "[Janus] #{message}")
9
- end
10
-
11
- attr_accessor :logger
12
- end
13
- end
14
- end
15
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Janus
4
+ module Logging
5
+ class Logger
6
+ class << self
7
+ def log(message, format = :info)
8
+ logger&.send(format, "[Janus] #{message}")
9
+ end
10
+
11
+ attr_accessor :logger
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,27 +1,27 @@
1
- # frozen_string_literal: true
2
-
3
- module Janus
4
- module Logging
5
- module Subscriber
6
- IGNORE_PAYLOAD_NAMES = %w(SCHEMA EXPLAIN).freeze
7
-
8
- def sql(event)
9
- name = event.payload[:name]
10
- unless IGNORE_PAYLOAD_NAMES.include?(name)
11
- name = [current_wrapper_name(event), name].compact.join(' ')
12
- event.payload[:name] = name
13
- end
14
- super(event)
15
- end
16
-
17
- protected
18
-
19
- def current_wrapper_name(_event)
20
- connection = Janus::Context.last_used_connection
21
- return nil unless connection
22
-
23
- "[#{connection}]"
24
- end
25
- end
26
- end
27
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Janus
4
+ module Logging
5
+ module Subscriber
6
+ IGNORE_PAYLOAD_NAMES = %w(SCHEMA EXPLAIN).freeze
7
+
8
+ def sql(event)
9
+ name = event.payload[:name]
10
+ unless IGNORE_PAYLOAD_NAMES.include?(name)
11
+ name = [current_wrapper_name(event), name].compact.join(' ')
12
+ event.payload[:name] = name
13
+ end
14
+ super(event)
15
+ end
16
+
17
+ protected
18
+
19
+ def current_wrapper_name(_event)
20
+ connection = Janus::Context.last_used_connection
21
+ return nil unless connection
22
+
23
+ "[#{connection}]"
24
+ end
25
+ end
26
+ end
27
+ end