knockoff 1.3.0 → 1.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22fb5337ef43e286ab938ab8d6e4905529607979266b8bb7673eccdba23c5d3c
4
- data.tar.gz: 9990634bf328df85fe419cbb3aea20b591d4a9c94420184f89012a15649ac2af
3
+ metadata.gz: 955ed88bdcc6efad21cc49e650e41b7f8be0090bcfb39c58b7445c58724f03f6
4
+ data.tar.gz: 2780acc35a3c2e45341677238e705aaae346eb258d7696de9092b894fc1fd138
5
5
  SHA512:
6
- metadata.gz: d7e7d9f11f4a7ea199d48e98c322d66b75db01eb2659b5218ea3928c586e1cab1d4087e3c7eb2aa1c54bdce744beb4f5c46fd2e4b43dc3972c1d735c6762acbf
7
- data.tar.gz: 4a6d620cffa48322560e060e1b500704791216d9daf6a3f6f81e90adaec25546012dbf0a55ca66c427265b87f42d3f0a1818f2aa93fbb84292a89ff7cd816bd2
6
+ metadata.gz: 31a78e1d5d861aa232411597f0dffc02b1283c7c3f0ce7ef5df07f81dc88f8b8f081b5842df8e8e7e51e570fae0325db617e4a6f8e70743884b8e648f08eccc4
7
+ data.tar.gz: 10677caed4bb00ebbd243af6131b3054324ea7218abbeef2ce0db0285410d1f3a91fcda5b06403177a88d441a6a3be3d064ee1de57016dfa6b044a5ad5dc4f70
@@ -9,14 +9,13 @@ jobs:
9
9
 
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: ['2.4.0', '2.5.1', '2.6.6', '2.7.2']
12
+ ruby-version: ['2.6.10', '2.7.8', '3.0.6', '3.1.4']
13
13
 
14
14
  steps:
15
15
  # Pin to this commit: v2
16
16
  - uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
17
17
  - name: Set up Ruby
18
- # Lock to this commit, v1.82.0
19
- uses: ruby/setup-ruby@5e4f0a10bfc39c97cd5358121291e27e5d97e09b
18
+ uses: ruby/setup-ruby@v1
20
19
  with:
21
20
  ruby-version: ${{ matrix.ruby-version }}
22
21
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.2
1
+ 2.7.8
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', '< 6.1'
22
- spec.add_runtime_dependency 'activesupport', '>= 4.0.0', '< 6.1'
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,56 +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
- # TODO: In ActiveRecord >= 6, this is a deprecated way to set a configuration. However
99
- # there appears to be an issue when calling `ActiveRecord::Base.configurations.to_h` in
100
- # version 6.0.4.8 where
101
- # multi-database setup is being ignored / dropped. For example if a database.yml setup
102
- # has something like..
103
- #
104
- # development:
105
- # primary:
106
- # ...
107
- # other:
108
- # ...
109
- #
110
- # then the 'other' database configuration is being dropped.
111
- key = "knockoff_replica_#{index}"
112
- config = replica_config.merge(uri_config)
113
- ActiveRecord::Base.configurations[key] = config
114
-
115
- @replicas_configurations[key] = config
72
+ to_copy = ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas')&.first&.config || {}
73
+ register_replica_copy(index, env_key, to_copy)
74
+
116
75
  rescue URI::InvalidURIError
117
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
118
77
  # Return a 'nil' which will be removed from
@@ -122,5 +81,35 @@ module Knockoff
122
81
  end
123
82
  end.compact
124
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
125
114
  end
126
115
  end
@@ -52,7 +52,7 @@ module Knockoff
52
52
  class #{class_name} < ActiveRecord::Base
53
53
  self.abstract_class = true
54
54
  establish_connection :#{config_key}
55
- def self.connection_config
55
+ def self.connection_db_config
56
56
  configurations[#{config_key.to_s.inspect}]
57
57
  end
58
58
  end
@@ -1,3 +1,3 @@
1
1
  module Knockoff
2
- VERSION = '1.3.0'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
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.3.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: 2022-06-28 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,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: '6'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.1'
22
+ version: '7'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 4.0.0
29
+ version: '6'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.1'
32
+ version: '7'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 4.0.0
39
+ version: '6'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '6.1'
42
+ version: '7'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 4.0.0
49
+ version: '6'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '6.1'
52
+ version: '7'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: bundler
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  - !ruby/object:Gem::Version
154
154
  version: '0'
155
155
  requirements: []
156
- rubygems_version: 3.1.4
156
+ rubygems_version: 3.1.6
157
157
  signing_key:
158
158
  specification_version: 4
159
159
  summary: A gem for easily using read replicas