gorynich 1.2.3.198656 → 1.3.0.213297

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f3734f146fa75d100e86a78a9f832261baba91b8cb2a36847b1691b569d7dad
4
- data.tar.gz: 2460d2f46d994ef0524379f05e8888cceed81a1749047fbf426e11e1c5388ba4
3
+ metadata.gz: 3058d25e339ef8fa032918319885a98066b37f8b70e259d34f1ae511031ef8b2
4
+ data.tar.gz: 623452668fd141e1284f8b2c64cfd5966e87a79d38cbd9bf8380f1e98b13f045
5
5
  SHA512:
6
- metadata.gz: c393942be3e141d01fcde9a8f5c67904f5ecbe41fd80a66b83f39f29fe1aae381e984f27668d2bb89273aab990dc959182aeb6dec3251ac2be22f1d58444ccb2
7
- data.tar.gz: '0296fa69c0de6a81748473c12429111b706d69ffc52d03e467096ab828ea43b692cb57be0ebffe373f5391405c81c78b679cfeaf5b180942e485b5ae72a1af49'
6
+ metadata.gz: fd8754400d364dd584afb5900008d3c6e6fa78ef1ddb5a8b2705584f12495671da2ef309446a69924aa1224b9cb1e5c2631672c05bb7767cfb6188e9a7feedc9
7
+ data.tar.gz: a2108c1c6841be869fbffe671141f502bfb2ad5f06e88240a61cb4aeb4aa3422c7b2b60c237f111ad7eb5c6d6f00f55a4a6b6bbcb99882741bf1797ea94ab5ff
@@ -26,7 +26,7 @@ module Gorynich
26
26
  #
27
27
  def fetch
28
28
  cfg = Gorynich.configuration.cache.fetch(
29
- %i[gorynich fetcher fetch], expires_in: @cache_expiration.seconds, namespace: @namespace
29
+ cache_key, expires_in: @cache_expiration.seconds, namespace: @namespace
30
30
  ) do
31
31
  if @fetcher.nil?
32
32
  {}
@@ -51,5 +51,18 @@ module Gorynich
51
51
 
52
52
  cfg.deep_transform_keys(&:downcase)
53
53
  end
54
+
55
+ #
56
+ # Delete cache
57
+ #
58
+ def reset
59
+ Gorynich.configuration.cache.delete(cache_key)
60
+ end
61
+
62
+ private
63
+
64
+ def cache_key
65
+ %i[gorynich fetcher fetch]
66
+ end
54
67
  end
55
68
  end
@@ -7,9 +7,6 @@ module Gorynich
7
7
  end
8
8
 
9
9
  def call(env)
10
- @config ||= Gorynich.instance
11
- @config.actualize
12
-
13
10
  tenant, opts = Gorynich.switcher.analyze(env)
14
11
 
15
12
  Gorynich.with(tenant, **opts) do
@@ -1,5 +1,7 @@
1
1
  module Gorynich
2
2
  class Switcher
3
+ DATABASE_RETRY_LIMIT = 2
4
+
3
5
  def initialize(config:)
4
6
  @config = config
5
7
  end
@@ -20,14 +22,30 @@ module Gorynich
20
22
  [tenant, { host: host, uri: uri }]
21
23
  end
22
24
 
25
+ #
26
+ # Connect to database
27
+ #
28
+ # @param [String, Symbol] tenant Tenant (database role)
29
+ #
23
30
  def with_database(tenant)
31
+ retries ||= 0
24
32
  ::ActiveRecord::Base.connected_to role: tenant.to_sym do
25
33
  ::ActiveRecord::Base.connection_pool.with_connection do
26
34
  yield(tenant)
27
35
  end
28
36
  end
29
37
  rescue ::ActiveRecord::ConnectionNotEstablished => e
30
- raise TenantNotFound, tenant unless ::Gorynich.instance.tenants.include?(tenant.to_s)
38
+ config = ::Gorynich.instance
39
+ config.actualize
40
+
41
+ raise TenantNotFound, tenant unless config.tenants.include?(tenant.to_s)
42
+ if (retries += 1) < DATABASE_RETRY_LIMIT
43
+ ActiveRecord::Base.connection_handler.establish_connection(
44
+ config.database(tenant), role: tenant.to_sym
45
+ )
46
+
47
+ retry
48
+ end
31
49
 
32
50
  raise e
33
51
  end
@@ -1,3 +1,3 @@
1
1
  module Gorynich
2
- VERSION = '1.2.3'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -109,4 +109,18 @@ RSpec.describe Gorynich::Fetcher do
109
109
  expect(Gorynich.configuration.cache.exist?(%i[gorynich fetcher fetch])).to be(true)
110
110
  end
111
111
  end
112
+
113
+ describe '#reset' do
114
+ let(:fetcher) { Gorynich::Fetchers::File.new(file_path: file_path) }
115
+
116
+ subject { described_class.new(fetcher: fetcher) }
117
+
118
+ it do
119
+ subject.fetch
120
+ expect(Gorynich.configuration.cache.exist?(%i[gorynich fetcher fetch])).to be(true)
121
+
122
+ subject.reset
123
+ expect(Gorynich.configuration.cache.exist?(%i[gorynich fetcher fetch])).to be(false)
124
+ end
125
+ end
112
126
  end
@@ -0,0 +1,46 @@
1
+ RSpec.describe Gorynich::Switcher do
2
+ let(:config) do
3
+ {
4
+ 'test' => {
5
+ 'default' => {
6
+ 'db_config' => {},
7
+ 'secrets' => {}
8
+ }
9
+ }
10
+ }
11
+ end
12
+ let(:fetcher) { HashFetcher.new(**config) }
13
+ let(:gorynich_config) { Gorynich::Config.new(fetcher: fetcher) }
14
+
15
+ subject { described_class.new(config: gorynich_config) }
16
+
17
+ describe '#with_database' do
18
+ context 'when tenant not found' do
19
+ it do
20
+ expect(::ActiveRecord::Base).to receive(:connected_to).with(role: :test).and_raise(
21
+ ::ActiveRecord::ConnectionNotEstablished
22
+ )
23
+ expect { subject.with_database('test') }.to raise_error(Gorynich::TenantNotFound)
24
+ end
25
+ end
26
+
27
+ context 'when have tenant but ConnectionNotEstablished' do
28
+ before(:each) do
29
+ allow(::Gorynich).to receive(:instance).and_return(gorynich_config)
30
+ end
31
+
32
+ it do
33
+ expect(::ActiveRecord::Base).to receive(:connected_to).with(role: :default).and_raise(
34
+ ::ActiveRecord::ConnectionNotEstablished
35
+ )
36
+ expect_any_instance_of(
37
+ ::ActiveRecord::ConnectionAdapters::ConnectionHandler
38
+ ).to receive(:establish_connection).with(
39
+ gorynich_config.database('default'), role: :default
40
+ )
41
+ expect(::ActiveRecord::Base).to receive(:connected_to)
42
+ subject.with_database('default')
43
+ end
44
+ end
45
+ end
46
+ end
data/spec/rails_helper.rb CHANGED
@@ -3,6 +3,6 @@ require File.expand_path('dummy/config/environment.rb', __dir__)
3
3
  abort('The Rails environment is running in production mode!') if Rails.env.production?
4
4
  require 'rspec/rails'
5
5
 
6
- # это надо для програзки классов и корректного рассчёта SimpleCov. Без него мы теряем целый процент!!
6
+ # это надо для прогрузки классов и корректного рассчёта SimpleCov. Без него мы теряем целый процент!!
7
7
  Rails.application.eager_load!
8
8
  require 'gorynich'
data/spec/spec_helper.rb CHANGED
@@ -21,14 +21,14 @@ unless %w[F FALSE 0].include? ENV['COVERAGE'].to_s.upcase
21
21
  source_file.src.first&.match(/skip coverage/)
22
22
  end
23
23
  end
24
-
24
+
25
25
  if ENV['TEST_ENV_NUMBER'] # parallel specs
26
26
  SimpleCov.at_exit do
27
27
  result = SimpleCov.result
28
28
  result.format! if ParallelTests.number_of_running_processes <= 1
29
29
  end
30
30
  end
31
-
31
+
32
32
  end
33
33
 
34
34
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,11 @@
1
+ class HashFetcher
2
+ attr_accessor :data
3
+
4
+ def initialize(**opts)
5
+ @data = opts
6
+ end
7
+
8
+ def fetch
9
+ data
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gorynich
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3.198656
4
+ version: 1.3.0.213297
5
5
  platform: ruby
6
6
  authors:
7
7
  - Poliev Alexey
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-11-30 00:00:00.000000000 Z
12
+ date: 2024-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: diplomat
@@ -362,10 +362,12 @@ files:
362
362
  - spec/lib/gorynich/fetchers/consul_secure_spec.rb
363
363
  - spec/lib/gorynich/fetchers/consul_spec.rb
364
364
  - spec/lib/gorynich/fetchers/file_spec.rb
365
+ - spec/lib/gorynich/switcher_spec.rb
365
366
  - spec/lib/gorynich/version_spec.rb
366
367
  - spec/lib/gorynich_spec.rb
367
368
  - spec/rails_helper.rb
368
369
  - spec/spec_helper.rb
370
+ - spec/support/helpers/hash_fetcher.rb
369
371
  homepage: https://github.com/RND-SOFT/gorynich
370
372
  licenses:
371
373
  - MIT
@@ -385,7 +387,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
385
387
  - !ruby/object:Gem::Version
386
388
  version: '0'
387
389
  requirements: []
388
- rubygems_version: 3.4.10
390
+ rubygems_version: 3.5.3
389
391
  signing_key:
390
392
  specification_version: 4
391
393
  summary: Multitenancy for Rails and subsystems
@@ -455,7 +457,9 @@ test_files:
455
457
  - spec/lib/gorynich/fetchers/consul_secure_spec.rb
456
458
  - spec/lib/gorynich/fetchers/consul_spec.rb
457
459
  - spec/lib/gorynich/fetchers/file_spec.rb
460
+ - spec/lib/gorynich/switcher_spec.rb
458
461
  - spec/lib/gorynich/version_spec.rb
459
462
  - spec/lib/gorynich_spec.rb
460
463
  - spec/rails_helper.rb
461
464
  - spec/spec_helper.rb
465
+ - spec/support/helpers/hash_fetcher.rb