knockoff 1.2.0 → 1.4.0

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: 95bb6904e4373aa3b026fc1e950b8f5cc9081a4abd174e58ef4ed521824fb5bb
4
- data.tar.gz: 6bfeeb88cb514854c2e550f531d65c76c160bcac1225b741deab01f6183e0abf
3
+ metadata.gz: 955ed88bdcc6efad21cc49e650e41b7f8be0090bcfb39c58b7445c58724f03f6
4
+ data.tar.gz: 2780acc35a3c2e45341677238e705aaae346eb258d7696de9092b894fc1fd138
5
5
  SHA512:
6
- metadata.gz: e2afdaf7285a09cb98a2946f19313160d84fe57208e86e00278732c391143c524093ab40e241a4db0c59711db789737b64e4958f2667fec6c7727f006e584663
7
- data.tar.gz: ee421e025448df4043319d90be8ed9aa8f9e2979d633feaa29d8876ea8bfb24aa1411d6103aeb65d50cb25f5c549732165d60330355bfe944f53fdc6bb14f955
6
+ metadata.gz: 31a78e1d5d861aa232411597f0dffc02b1283c7c3f0ce7ef5df07f81dc88f8b8f081b5842df8e8e7e51e570fae0325db617e4a6f8e70743884b8e648f08eccc4
7
+ data.tar.gz: 10677caed4bb00ebbd243af6131b3054324ea7218abbeef2ce0db0285410d1f3a91fcda5b06403177a88d441a6a3be3d064ee1de57016dfa6b044a5ad5dc4f70
@@ -0,0 +1,23 @@
1
+ name: Ruby
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ test:
7
+
8
+ runs-on: ubuntu-20.04
9
+
10
+ strategy:
11
+ matrix:
12
+ ruby-version: ['2.6.10', '2.7.8', '3.0.6', '3.1.4']
13
+
14
+ steps:
15
+ # Pin to this commit: v2
16
+ - uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: ${{ matrix.ruby-version }}
21
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
22
+ - name: Run tests
23
+ run: bundle exec rake spec
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.8
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Knockoff
2
2
 
3
- [![Build Status](https://travis-ci.com/joinhandshake/knockoff.svg?branch=master)](https://travis-ci.com/github/joinhandshake/knockoff)
3
+ [![Build Status](https://github.com/joinhandshake/knockoff/actions/workflows/ruby.yml/badge.svg)](https://github.com/joinhandshake/knockoff/actions)
4
4
  [![Gem Version](https://badge.fury.io/rb/knockoff.svg)](https://badge.fury.io/rb/knockoff)
5
5
 
6
6
  A gem for easily using read replicas.
data/knockoff.gemspec CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency 'activerecord', '>= 4.0.0'
22
- spec.add_runtime_dependency 'activesupport', '>= 4.0.0'
21
+ spec.add_runtime_dependency 'activerecord', '>= 6', '< 7'
22
+ spec.add_runtime_dependency 'activesupport', '>= 6', '< 7'
23
23
 
24
24
  spec.add_development_dependency "bundler"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
@@ -34,9 +34,12 @@ module Knockoff
34
34
  end
35
35
 
36
36
  def update_replica_configs(new_configs)
37
- ActiveRecord::Base.configurations['knockoff_replicas'].merge(new_configs) if ActiveRecord::Base.configurations['knockoff_replicas'].present?
37
+ if ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').present?
38
+ updated_config = new_configs.deep_dup.merge!(ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').first.config)
39
+ end
40
+
38
41
  @replicas_configurations.each do |key, _config|
39
- update_replica_config(key, new_configs)
42
+ update_replica_config(key, updated_config)
40
43
  end
41
44
  end
42
45
 
@@ -49,9 +52,10 @@ module Knockoff
49
52
 
50
53
  private
51
54
 
52
- def update_replica_config(key, new_configs)
53
- @replicas_configurations[key].merge!(new_configs)
54
- ActiveRecord::Base.configurations[key].merge!(new_configs)
55
+ def update_replica_config(key, updated_config)
56
+ merged_config = @replicas_configurations[key].config.deep_dup.merge!(updated_config)
57
+ @replicas_configurations[key] = ActiveRecord::DatabaseConfigurations::HashConfig.new(key, key, merged_config)
58
+ ActiveRecord::Base.configurations.configurations << @replicas_configurations[key]
55
59
  end
56
60
 
57
61
  def set_replica_configs
@@ -63,50 +67,11 @@ module Knockoff
63
67
  # and don't add the uri to the final list if it can't be parsed
64
68
  replica_env_keys.map.with_index(0) do |env_key, index|
65
69
  begin
66
- uri = URI.parse(ENV[env_key])
67
70
 
68
71
  # Configure parameters such as prepared_statements, pool, reaping_frequency for all replicas.
69
- replica_config = ActiveRecord::Base.configurations['knockoff_replicas'] || {}
70
-
71
- adapter =
72
- if uri.scheme == "postgres"
73
- 'postgresql'
74
- else
75
- uri.scheme
76
- end
77
-
78
- # Base config from the ENV uri. Sqlite is a special case
79
- # and all others follow 'normal' config
80
- uri_config =
81
- if uri.scheme == 'sqlite3'
82
- {
83
- 'adapter' => adapter,
84
- 'database' => uri.to_s.split(':')[1]
85
- }
86
- else
87
- {
88
- 'adapter' => adapter,
89
- 'database' => (uri.path || "").split("/")[1],
90
- 'username' => uri.user,
91
- 'password' => uri.password,
92
- 'host' => uri.host,
93
- 'port' => uri.port
94
- }
95
- end
96
-
97
- # Store the hash in configuration and use it when we establish the connection later.
98
- key = "knockoff_replica_#{index}"
99
- config = replica_config.merge(uri_config)
100
-
101
- if ActiveRecord::VERSION::MAJOR >= 6
102
- full_config = ActiveRecord::Base.configurations.to_h.merge(key => config)
103
-
104
- ActiveRecord::Base.configurations = full_config
105
- @replicas_configurations[key] = config
106
- else
107
- ActiveRecord::Base.configurations[key] = config
108
- @replicas_configurations[key] = config
109
- end
72
+ to_copy = ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas')&.first&.config || {}
73
+ register_replica_copy(index, env_key, to_copy)
74
+
110
75
  rescue URI::InvalidURIError
111
76
  Rails.logger.info "LOG NOTIFIER: Invalid URL specified in follower_env_keys. Not including URI, which may result in no followers used." # URI is purposely not printed to logs
112
77
  # Return a 'nil' which will be removed from
@@ -116,5 +81,35 @@ module Knockoff
116
81
  end
117
82
  end.compact
118
83
  end
84
+
85
+ def register_replica_copy(index, env_key, configuration_hash)
86
+ key = "knockoff_replica_#{index}"
87
+ new_config = create_replica_copy(env_key, key, configuration_hash.deep_dup)
88
+ ActiveRecord::Base.configurations.configurations << new_config
89
+ @replicas_configurations[key] = new_config
90
+ end
91
+
92
+ def create_replica_copy(env_key, key, replica_config_hash)
93
+ uri = URI.parse(ENV[env_key])
94
+
95
+ replica_config_hash[:adapter] =
96
+ if uri.scheme == "postgres"
97
+ 'postgresql'
98
+ else
99
+ uri.scheme
100
+ end
101
+
102
+ if uri.scheme == 'sqlite3'
103
+ replica_config_hash[:database] = uri.to_s.split(':')[1]
104
+ else
105
+ replica_config_hash[:database] = (uri.path || "").split("/")[1]
106
+ replica_config_hash[:username] = uri.user
107
+ replica_config_hash[:password] = uri.password
108
+ replica_config_hash[:host] = uri.host
109
+ replica_config_hash[:port] = uri.port
110
+ end
111
+
112
+ ActiveRecord::DatabaseConfigurations::HashConfig.new(key, key, replica_config_hash)
113
+ end
119
114
  end
120
115
  end
@@ -30,6 +30,12 @@ module Knockoff
30
30
  end
31
31
  end
32
32
 
33
+ def set_schema_cache(cache)
34
+ @pool.each do |_name, klass|
35
+ klass.connection_pool.schema_cache = cache
36
+ end
37
+ end
38
+
33
39
  def random_replica_connection
34
40
  @pool[@pool.keys.sample]
35
41
  end
@@ -46,7 +52,7 @@ module Knockoff
46
52
  class #{class_name} < ActiveRecord::Base
47
53
  self.abstract_class = true
48
54
  establish_connection :#{config_key}
49
- def self.connection_config
55
+ def self.connection_db_config
50
56
  configurations[#{config_key.to_s.inspect}]
51
57
  end
52
58
  end
@@ -1,3 +1,3 @@
1
1
  module Knockoff
2
- VERSION = '1.2.0'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
data/lib/knockoff.rb CHANGED
@@ -49,6 +49,10 @@ module Knockoff
49
49
  @config ||= Config.new
50
50
  end
51
51
 
52
+ def set_schema_cache(cache)
53
+ replica_pool.set_schema_cache(cache)
54
+ end
55
+
52
56
  def base_transaction_depth
53
57
  @base_transaction_depth ||= begin
54
58
  testcase = ActiveSupport::TestCase
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knockoff
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Ringwelski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-29 00:00:00.000000000 Z
11
+ date: 2024-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,28 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: '6'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
20
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
- version: 4.0.0
29
+ version: '6'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: activesupport
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
33
- version: 4.0.0
39
+ version: '6'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '7'
34
43
  type: :runtime
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
47
  - - ">="
39
48
  - !ruby/object:Gem::Version
40
- version: 4.0.0
49
+ version: '6'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '7'
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: bundler
43
55
  requirement: !ruby/object:Gem::Requirement
@@ -101,9 +113,10 @@ executables: []
101
113
  extensions: []
102
114
  extra_rdoc_files: []
103
115
  files:
116
+ - ".github/workflows/ruby.yml"
104
117
  - ".gitignore"
105
118
  - ".rspec"
106
- - ".travis.yml"
119
+ - ".ruby-version"
107
120
  - CHANGELOG.md
108
121
  - Gemfile
109
122
  - LICENSE.txt
@@ -140,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
153
  - !ruby/object:Gem::Version
141
154
  version: '0'
142
155
  requirements: []
143
- rubygems_version: 3.1.2
156
+ rubygems_version: 3.1.6
144
157
  signing_key:
145
158
  specification_version: 4
146
159
  summary: A gem for easily using read replicas
data/.travis.yml DELETED
@@ -1,9 +0,0 @@
1
- language: ruby
2
- notifications:
3
- email: false
4
- rvm:
5
- - 2.4.0
6
- - 2.5.1
7
- - 2.6.6
8
- - 2.7.2
9
- - ruby-head