Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to be Rails 6.1 compatible #29

Merged
merged 16 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ jobs:

strategy:
matrix:
ruby-version: ['2.4.0', '2.5.1', '2.6.6', '2.7.2']
ruby-version: ['2.6.10', '2.7.8', '3.0.6', '3.1.4']

steps:
# Pin to this commit: v2
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- name: Set up Ruby
# Lock to this commit, v1.82.0
uses: ruby/setup-ruby@5e4f0a10bfc39c97cd5358121291e27e5d97e09b
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.2
2.7.8
4 changes: 2 additions & 2 deletions knockoff.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency 'activerecord', '>= 4.0.0', '< 6.1'
spec.add_runtime_dependency 'activesupport', '>= 4.0.0', '< 6.1'
spec.add_runtime_dependency 'activerecord', '>= 4.0.0', '< 7'
spec.add_runtime_dependency 'activesupport', '>= 4.0.0', '< 7'

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
13 changes: 9 additions & 4 deletions lib/knockoff/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def replica_env_keys
end

def update_replica_configs(new_configs)
ActiveRecord::Base.configurations['knockoff_replicas'].merge(new_configs) if ActiveRecord::Base.configurations['knockoff_replicas'].present?
if ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').present?
ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').first.configuration_hash.merge(new_configs)
amy-hs marked this conversation as resolved.
Show resolved Hide resolved
end

@replicas_configurations.each do |key, _config|
update_replica_config(key, new_configs)
end
Expand Down Expand Up @@ -66,7 +69,7 @@ def parse_knockoff_replica_envs_to_configs
uri = URI.parse(ENV[env_key])

# Configure parameters such as prepared_statements, pool, reaping_frequency for all replicas.
replica_config = ActiveRecord::Base.configurations['knockoff_replicas'] || {}
replica_config = ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas')&.first || {}

adapter =
if uri.scheme == "postgres"
Expand Down Expand Up @@ -109,8 +112,10 @@ def parse_knockoff_replica_envs_to_configs
#
# then the 'other' database configuration is being dropped.
key = "knockoff_replica_#{index}"
config = replica_config.merge(uri_config)
ActiveRecord::Base.configurations[key] = config
config = replica_config.merge(uri_config).symbolize_keys
env_name = ActiveRecord::Base.configurations.configurations.first.env_name
new_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, key, config)
ActiveRecord::Base.configurations.configurations << new_config

@replicas_configurations[key] = config
rescue URI::InvalidURIError
Expand Down
2 changes: 1 addition & 1 deletion lib/knockoff/replica_connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def connection_class(config_key)
class #{class_name} < ActiveRecord::Base
self.abstract_class = true
establish_connection :#{config_key}
def self.connection_config
def self.connection_db_config
configurations[#{config_key.to_s.inspect}]
end
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.3.0'.freeze
VERSION = '1.4.0'.freeze
end
6 changes: 3 additions & 3 deletions spec/knockoff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ class ActiveRecord::Relation
Knockoff.clear_all_active_connections!
end

it 'defines self.connection_config' do
expect(Knockoff::KnockoffReplica0.connection_config).not_to be_nil
expect(Knockoff::KnockoffReplica0.connection_config['adapter']).to eq 'sqlite3'
it 'defines self.connection_db_config' do
expect(Knockoff::KnockoffReplica0.connection_db_config).not_to be_nil
expect(Knockoff::KnockoffReplica0.connection_db_config[:adapter]).to eq 'sqlite3'
end

context "bad configurations" do
Expand Down
8 changes: 4 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

require 'knockoff'

ActiveRecord::Base.configurations = {
'test' => { 'adapter' => 'sqlite3', 'database' => 'tmp/test_db' }
}
ActiveRecord::Base.configurations = ActiveRecord::DatabaseConfigurations.new({
:test => { :adapter => 'sqlite3', :database => 'tmp/test_db' }
})

# Setup the ENV's for replicas
ENV['KNOCKOFF_REPLICA1'] = 'sqlite3:tmp/test_replica_db'
Expand All @@ -29,4 +29,4 @@ class User < ActiveRecord::Base
User.create

# Reconnect to primary
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.establish_connection(:test)
Loading