janus-ar 0.15.2 → 0.15.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,80 +1,80 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter do
4
- subject { described_class.new(config) }
5
-
6
- it { expect(described_class::FOUND_ROWS).to eq 'FOUND_ROWS' }
7
-
8
- let(:database) { 'test' }
9
- let(:primary_config) do
10
- {
11
- 'username' => 'primary',
12
- 'password' => 'primary_password',
13
- 'host' => '127.0.0.1',
14
- 'ssl' => true,
15
- 'ssl_mode' => 'REQUIRED',
16
- 'tls_min_version' => Trilogy::TLS_VERSION_12,
17
- }
18
- end
19
- let(:replica_config) do
20
- {
21
- 'username' => 'replica',
22
- 'password' => 'replica_password',
23
- 'host' => '127.0.0.1',
24
- 'pool' => 500,
25
- 'ssl' => true,
26
- 'ssl_mode' => 'REQUIRED',
27
- 'tls_min_version' => Trilogy::TLS_VERSION_12,
28
- }
29
- end
30
- let(:config) do
31
- {
32
- database:,
33
- adapter: 'janus_trilogy',
34
- janus: {
35
- 'primary' => primary_config,
36
- 'replica' => replica_config,
37
- },
38
- }
39
- end
40
-
41
- describe 'Configuration' do
42
- it 'creates primary connection as expected' do
43
- config = primary_config.dup.freeze
44
- expect(subject.config).to eq config.merge('database' => database,
45
- 'flags' => ::Janus::Client::FOUND_ROWS).symbolize_keys
46
- end
47
-
48
- it 'creates replica connection as expected' do
49
- config = replica_config.dup.freeze
50
- expect(
51
- subject.replica_connection.instance_variable_get(:@config)
52
- ).to eq config.merge('database' => database).symbolize_keys
53
- end
54
-
55
- context 'Rails sets empty database for server connection' do
56
- let(:database) { nil }
57
-
58
- it 'creates primary connection as expected' do
59
- config = primary_config.dup.freeze
60
- expect(subject.config).to eq config.merge(
61
- 'database' => nil,
62
- 'flags' => ::Janus::Client::FOUND_ROWS
63
- ).symbolize_keys
64
- end
65
-
66
- it 'creates replica connection as expected' do
67
- config = replica_config.dup.freeze
68
- expect(
69
- subject.replica_connection.instance_variable_get(:@config)
70
- ).to eq config.merge('database' => nil).symbolize_keys
71
- end
72
- end
73
- end
74
-
75
- describe 'Integration tests' do
76
- let(:table_name) { 'table_name_trilogy' }
77
-
78
- it_behaves_like 'a mysql like server'
79
- end
80
- end
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter do
4
+ subject { described_class.new(config) }
5
+
6
+ it { expect(described_class::FOUND_ROWS).to eq 'FOUND_ROWS' }
7
+
8
+ let(:database) { 'test' }
9
+ let(:primary_config) do
10
+ {
11
+ 'username' => 'primary',
12
+ 'password' => 'primary_password',
13
+ 'host' => '127.0.0.1',
14
+ 'ssl' => true,
15
+ 'ssl_mode' => 'REQUIRED',
16
+ 'tls_min_version' => Trilogy::TLS_VERSION_12,
17
+ }
18
+ end
19
+ let(:replica_config) do
20
+ {
21
+ 'username' => 'replica',
22
+ 'password' => 'replica_password',
23
+ 'host' => '127.0.0.1',
24
+ 'pool' => 500,
25
+ 'ssl' => true,
26
+ 'ssl_mode' => 'REQUIRED',
27
+ 'tls_min_version' => Trilogy::TLS_VERSION_12,
28
+ }
29
+ end
30
+ let(:config) do
31
+ {
32
+ database:,
33
+ adapter: 'janus_trilogy',
34
+ janus: {
35
+ 'primary' => primary_config,
36
+ 'replica' => replica_config,
37
+ },
38
+ }
39
+ end
40
+
41
+ describe 'Configuration' do
42
+ it 'creates primary connection as expected' do
43
+ config = primary_config.dup.freeze
44
+ expect(subject.config).to eq config.merge('database' => database,
45
+ 'flags' => ::Janus::Client::FOUND_ROWS).symbolize_keys
46
+ end
47
+
48
+ it 'creates replica connection as expected' do
49
+ config = replica_config.dup.freeze
50
+ expect(
51
+ subject.replica_connection.instance_variable_get(:@config)
52
+ ).to eq config.merge('database' => database).symbolize_keys
53
+ end
54
+
55
+ context 'Rails sets empty database for server connection' do
56
+ let(:database) { nil }
57
+
58
+ it 'creates primary connection as expected' do
59
+ config = primary_config.dup.freeze
60
+ expect(subject.config).to eq config.merge(
61
+ 'database' => nil,
62
+ 'flags' => ::Janus::Client::FOUND_ROWS
63
+ ).symbolize_keys
64
+ end
65
+
66
+ it 'creates replica connection as expected' do
67
+ config = replica_config.dup.freeze
68
+ expect(
69
+ subject.replica_connection.instance_variable_get(:@config)
70
+ ).to eq config.merge('database' => nil).symbolize_keys
71
+ end
72
+ end
73
+ end
74
+
75
+ describe 'Integration tests' do
76
+ let(:table_name) { 'table_name_trilogy' }
77
+
78
+ it_behaves_like 'a mysql like server'
79
+ end
80
+ end
@@ -1,7 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
- require 'janus/client'
4
-
5
- RSpec.describe Janus::Client do
6
- it { expect(described_class::FOUND_ROWS).to eq 2 }
7
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'janus/client'
4
+
5
+ RSpec.describe Janus::Client do
6
+ it { expect(described_class::FOUND_ROWS).to eq 2 }
7
+ end
@@ -1,46 +1,46 @@
1
- # frozen_string_literal: true
2
-
3
- require 'janus/context'
4
-
5
- RSpec.describe Janus::Context do
6
- describe '#initialize' do
7
- it 'sets the primary flag and expiry' do
8
- context = described_class.new(primary: true, expiry: 60)
9
- expect(context.use_primary?).to be true
10
- expect(context.last_used_connection).to eq(:primary)
11
- end
12
- end
13
-
14
- describe '#stick_to_primary' do
15
- it 'sets the primary flag to true' do
16
- context = described_class.new
17
- context.stick_to_primary
18
- expect(context.use_primary?).to be true
19
- end
20
- end
21
-
22
- describe '#potential_write' do
23
- it 'calls stick_to_primary' do
24
- context = described_class.new
25
- expect(context).to receive(:stick_to_primary)
26
- context.potential_write
27
- end
28
- end
29
-
30
- describe '#release_all' do
31
- it 'resets the primary flag and expiry' do
32
- context = described_class.new(primary: true, expiry: 60)
33
- context.release_all
34
- expect(context.use_primary?).to be false
35
- expect(context.last_used_connection).to be_nil
36
- end
37
- end
38
-
39
- describe '#used_connection' do
40
- it 'sets the last used connection' do
41
- context = described_class.new
42
- context.used_connection(:secondary)
43
- expect(context.last_used_connection).to eq(:secondary)
44
- end
45
- end
46
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'janus/context'
4
+
5
+ RSpec.describe Janus::Context do
6
+ describe '#initialize' do
7
+ it 'sets the primary flag and expiry' do
8
+ context = described_class.new(primary: true, expiry: 60)
9
+ expect(context.use_primary?).to be true
10
+ expect(context.last_used_connection).to eq(:primary)
11
+ end
12
+ end
13
+
14
+ describe '#stick_to_primary' do
15
+ it 'sets the primary flag to true' do
16
+ context = described_class.new
17
+ context.stick_to_primary
18
+ expect(context.use_primary?).to be true
19
+ end
20
+ end
21
+
22
+ describe '#potential_write' do
23
+ it 'calls stick_to_primary' do
24
+ context = described_class.new
25
+ expect(context).to receive(:stick_to_primary)
26
+ context.potential_write
27
+ end
28
+ end
29
+
30
+ describe '#release_all' do
31
+ it 'resets the primary flag and expiry' do
32
+ context = described_class.new(primary: true, expiry: 60)
33
+ context.release_all
34
+ expect(context.use_primary?).to be false
35
+ expect(context.last_used_connection).to be_nil
36
+ end
37
+ end
38
+
39
+ describe '#used_connection' do
40
+ it 'sets the last used connection' do
41
+ context = described_class.new
42
+ context.used_connection(:secondary)
43
+ expect(context.last_used_connection).to eq(:secondary)
44
+ end
45
+ end
46
+ end
@@ -1,31 +1,31 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Janus::Logging::Logger do
4
- describe '.log' do
5
- let(:logger) { double('logger') }
6
-
7
- before do
8
- described_class.logger = logger
9
- end
10
-
11
- it 'logs the message with the specified format' do
12
- expect(logger).to receive(:send).with(:info, '[Janus] Test message')
13
- described_class.log('Test message', :info)
14
- end
15
-
16
- it 'does not log the message if logger is not set' do
17
- described_class.logger = nil
18
- expect(logger).not_to receive(:send)
19
- described_class.log('Test message', :info)
20
- end
21
- end
22
-
23
- describe '.logger=' do
24
- let(:logger) { double('logger') }
25
-
26
- it 'sets the logger' do
27
- described_class.logger = logger
28
- expect(described_class.logger).to eq(logger)
29
- end
30
- end
31
- end
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Janus::Logging::Logger do
4
+ describe '.log' do
5
+ let(:logger) { double('logger') }
6
+
7
+ before do
8
+ described_class.logger = logger
9
+ end
10
+
11
+ it 'logs the message with the specified format' do
12
+ expect(logger).to receive(:send).with(:info, '[Janus] Test message')
13
+ described_class.log('Test message', :info)
14
+ end
15
+
16
+ it 'does not log the message if logger is not set' do
17
+ described_class.logger = nil
18
+ expect(logger).not_to receive(:send)
19
+ described_class.log('Test message', :info)
20
+ end
21
+ end
22
+
23
+ describe '.logger=' do
24
+ let(:logger) { double('logger') }
25
+
26
+ it 'sets the logger' do
27
+ described_class.logger = logger
28
+ expect(described_class.logger).to eq(logger)
29
+ end
30
+ end
31
+ end
@@ -1,59 +1,59 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Janus::QueryDirector do
4
- describe 'Constants' do
5
- it { expect(described_class::SQL_SKIP_ALL_MATCHERS).to eq [/\A\s*set\s+local\s/i] }
6
- it {
7
- expect(described_class::SQL_PRIMARY_MATCHERS).to eq(
8
- [
9
- /\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
10
- /\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i,
11
- /\A\s*show/i
12
- ]
13
- )
14
- }
15
- it { expect(described_class::SQL_REPLICA_MATCHERS).to eq([/\A\s*(select|with.+\)\s*select)\s/i]) }
16
- it { expect(described_class::SQL_ALL_MATCHERS).to eq([/\A\s*set\s/i]) }
17
- it {
18
- expect(described_class::WRITE_PREFIXES).to eq %w(INSERT UPDATE DELETE LOCK CREATE GRANT DROP ALTER TRUNCATE BEGIN
19
- SAVEPOINT FLUSH)
20
- }
21
-
22
- it { expect(described_class::ALL).to eq :all }
23
- it { expect(described_class::REPLICA).to eq :replica }
24
- it { expect(described_class::PRIMARY).to eq :primary }
25
- end
26
-
27
- describe '#where_to_send?' do
28
- before(:each) do
29
- Janus::Context.release_all
30
- end
31
-
32
- context 'when should send to all' do
33
- it 'returns :all' do
34
- sql = 'SET foo = bar'
35
- open_transactions = 0
36
- query_director = described_class.new(sql, open_transactions)
37
- expect(query_director.where_to_send?).to eq(:all)
38
- end
39
- end
40
-
41
- context 'when can go to replica' do
42
- it 'returns :replica' do
43
- sql = 'SELECT * FROM users'
44
- open_transactions = 0
45
- query_director = described_class.new(sql, open_transactions)
46
- expect(query_director.where_to_send?).to eq(:replica)
47
- end
48
- end
49
-
50
- context 'when should go to primary' do
51
- it 'returns :primary' do
52
- sql = 'INSERT INTO users (name) VALUES ("John")'
53
- open_transactions = 0
54
- query_director = described_class.new(sql, open_transactions)
55
- expect(query_director.where_to_send?).to eq(:primary)
56
- end
57
- end
58
- end
59
- end
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Janus::QueryDirector do
4
+ describe 'Constants' do
5
+ it { expect(described_class::SQL_SKIP_ALL_MATCHERS).to eq [/\A\s*set\s+local\s/i] }
6
+ it {
7
+ expect(described_class::SQL_PRIMARY_MATCHERS).to eq(
8
+ [
9
+ /\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
10
+ /\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i,
11
+ /\A\s*show/i
12
+ ]
13
+ )
14
+ }
15
+ it { expect(described_class::SQL_REPLICA_MATCHERS).to eq([/\A\s*(select|with.+\)\s*select)\s/i]) }
16
+ it { expect(described_class::SQL_ALL_MATCHERS).to eq([/\A\s*set\s/i]) }
17
+ it {
18
+ expect(described_class::WRITE_PREFIXES).to eq %w(INSERT UPDATE DELETE LOCK CREATE GRANT DROP ALTER TRUNCATE BEGIN
19
+ SAVEPOINT FLUSH)
20
+ }
21
+
22
+ it { expect(described_class::ALL).to eq :all }
23
+ it { expect(described_class::REPLICA).to eq :replica }
24
+ it { expect(described_class::PRIMARY).to eq :primary }
25
+ end
26
+
27
+ describe '#where_to_send?' do
28
+ before(:each) do
29
+ Janus::Context.release_all
30
+ end
31
+
32
+ context 'when should send to all' do
33
+ it 'returns :all' do
34
+ sql = 'SET foo = bar'
35
+ open_transactions = 0
36
+ query_director = described_class.new(sql, open_transactions)
37
+ expect(query_director.where_to_send?).to eq(:all)
38
+ end
39
+ end
40
+
41
+ context 'when can go to replica' do
42
+ it 'returns :replica' do
43
+ sql = 'SELECT * FROM users'
44
+ open_transactions = 0
45
+ query_director = described_class.new(sql, open_transactions)
46
+ expect(query_director.where_to_send?).to eq(:replica)
47
+ end
48
+ end
49
+
50
+ context 'when should go to primary' do
51
+ it 'returns :primary' do
52
+ sql = 'INSERT INTO users (name) VALUES ("John")'
53
+ open_transactions = 0
54
+ query_director = described_class.new(sql, open_transactions)
55
+ expect(query_director.where_to_send?).to eq(:primary)
56
+ end
57
+ end
58
+ end
59
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,43 +1,43 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pry'
4
-
5
- require 'active_record'
6
-
7
- require './lib/janus'
8
- require './lib/active_record/connection_adapters/janus_mysql2_adapter'
9
- require './lib/active_record/connection_adapters/janus_trilogy_adapter'
10
-
11
- require './spec/shared_examples/a_mysql_like_server.rb'
12
-
13
- class QueryLogger
14
- def initialize
15
- @_logs = []
16
- end
17
-
18
- def flush_all
19
- @_logs = []
20
- end
21
-
22
- def log(level, message)
23
- @_logs << "#{level}: #{message}"
24
- end
25
-
26
- def error(message)
27
- log('error', message)
28
- end
29
-
30
- def queries
31
- @_logs
32
- end
33
-
34
- def debug?
35
- true
36
- end
37
-
38
- def debug(message)
39
- log('debug', message)
40
- end
41
- end
42
-
43
- $query_logger = ActiveRecord::Base.logger = QueryLogger.new
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+
5
+ require 'active_record'
6
+
7
+ require './lib/janus'
8
+ require './lib/active_record/connection_adapters/janus_mysql2_adapter'
9
+ require './lib/active_record/connection_adapters/janus_trilogy_adapter'
10
+
11
+ require './spec/shared_examples/a_mysql_like_server.rb'
12
+
13
+ class QueryLogger
14
+ def initialize
15
+ @_logs = []
16
+ end
17
+
18
+ def flush_all
19
+ @_logs = []
20
+ end
21
+
22
+ def log(level, message)
23
+ @_logs << "#{level}: #{message}"
24
+ end
25
+
26
+ def error(message)
27
+ log('error', message)
28
+ end
29
+
30
+ def queries
31
+ @_logs
32
+ end
33
+
34
+ def debug?
35
+ true
36
+ end
37
+
38
+ def debug(message)
39
+ log('debug', message)
40
+ end
41
+ end
42
+
43
+ $query_logger = ActiveRecord::Base.logger = QueryLogger.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: janus-ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.2
4
+ version: 0.15.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lloyd Watkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-02 00:00:00.000000000 Z
11
+ date: 2024-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,13 +17,19 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 7.1.0
20
- type: :development
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.2'
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
29
  version: 7.1.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.2'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: activesupport
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -31,6 +37,9 @@ dependencies:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
33
39
  version: 7.1.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '7.2'
34
43
  type: :development
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -38,6 +47,9 @@ dependencies:
38
47
  - - ">="
39
48
  - !ruby/object:Gem::Version
40
49
  version: 7.1.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '7.2'
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: mysql2
43
55
  requirement: !ruby/object:Gem::Requirement
@@ -114,28 +126,28 @@ dependencies:
114
126
  requirements:
115
127
  - - "~>"
116
128
  - !ruby/object:Gem::Version
117
- version: 1.63.0
129
+ version: 1.65.0
118
130
  type: :development
119
131
  prerelease: false
120
132
  version_requirements: !ruby/object:Gem::Requirement
121
133
  requirements:
122
134
  - - "~>"
123
135
  - !ruby/object:Gem::Version
124
- version: 1.63.0
136
+ version: 1.65.0
125
137
  - !ruby/object:Gem::Dependency
126
138
  name: rubocop-rails
127
139
  requirement: !ruby/object:Gem::Requirement
128
140
  requirements:
129
141
  - - "~>"
130
142
  - !ruby/object:Gem::Version
131
- version: 2.24.0
143
+ version: 2.25.0
132
144
  type: :development
133
145
  prerelease: false
134
146
  version_requirements: !ruby/object:Gem::Requirement
135
147
  requirements:
136
148
  - - "~>"
137
149
  - !ruby/object:Gem::Version
138
- version: 2.24.0
150
+ version: 2.25.0
139
151
  - !ruby/object:Gem::Dependency
140
152
  name: rubocop-rspec
141
153
  requirement: !ruby/object:Gem::Requirement
@@ -241,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
253
  - !ruby/object:Gem::Version
242
254
  version: '0'
243
255
  requirements: []
244
- rubygems_version: 3.5.7
256
+ rubygems_version: 3.5.11
245
257
  signing_key:
246
258
  specification_version: 4
247
259
  summary: Read/Write proxy for ActiveRecord using primary/replica databases