Skip to content

Commit

Permalink
Merge pull request #25 from joinhandshake/support-schema-cache
Browse files Browse the repository at this point in the history
Add support for setting the schema cache on each replica pool
  • Loading branch information
sgringwe authored Jun 28, 2022
2 parents c1862f2 + 5f3becd commit 6d52a0e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/knockoff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def config
@config ||= Config.new
end

def set_schema_cache(cache)
replica_pool.set_schema_cache(cache)
end

def base_transaction_depth
@base_transaction_depth ||= begin
testcase = ActiveSupport::TestCase
Expand Down
24 changes: 15 additions & 9 deletions lib/knockoff/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,24 @@ def parse_knockoff_replica_envs_to_configs
end

# Store the hash in configuration and use it when we establish the connection later.
# TODO: In ActiveRecord >= 6, this is a deprecated way to set a configuration. However
# there appears to be an issue when calling `ActiveRecord::Base.configurations.to_h` in
# version 6.0.4.8 where
# multi-database setup is being ignored / dropped. For example if a database.yml setup
# has something like..
#
# development:
# primary:
# ...
# other:
# ...
#
# then the 'other' database configuration is being dropped.
key = "knockoff_replica_#{index}"
config = replica_config.merge(uri_config)
ActiveRecord::Base.configurations[key] = config

if ActiveRecord::VERSION::MAJOR >= 6
full_config = ActiveRecord::Base.configurations.to_h.merge(key => config)

ActiveRecord::Base.configurations = full_config
@replicas_configurations[key] = config
else
ActiveRecord::Base.configurations[key] = config
@replicas_configurations[key] = config
end
@replicas_configurations[key] = config
rescue URI::InvalidURIError
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
# Return a 'nil' which will be removed from
Expand Down
6 changes: 6 additions & 0 deletions lib/knockoff/replica_connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def reconnect_all_replicas!
end
end

def set_schema_cache(cache)
@pool.each do |_name, klass|
klass.connection_pool.schema_cache = cache
end
end

def random_replica_connection
@pool[@pool.keys.sample]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/knockoff/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Knockoff
VERSION = '1.2.0'.freeze
VERSION = '1.3.0'.freeze
end
8 changes: 8 additions & 0 deletions spec/knockoff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def on_replica?
end
end

context 'setting schema cache' do
it 'sets the cache to each pool schema_cache value' do
expect(Knockoff::KnockoffReplica0.connection_pool.schema_cache).to be_nil
Knockoff.set_schema_cache('test')
expect(Knockoff::KnockoffReplica0.connection_pool.schema_cache).to eq 'test'
end
end

context 'in transaction' do
it 'raises error in transaction if replica is attempted' do
User.transaction do
Expand Down

0 comments on commit 6d52a0e

Please sign in to comment.