Skip to content

Commit

Permalink
Merge pull request #9 from aktsk/support-rails71
Browse files Browse the repository at this point in the history
Support rails7.1
  • Loading branch information
fumihumi authored Jan 9, 2024
2 parents b50efb8 + dc8f138 commit 9a19e72
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
source 'https://rubygems.org'

if RUBY_VERSION < '3.0'
gem 'activerecord', '~> 6.1.0'
gem 'activesupport', '~> 6.1.0'
gem 'activerecord', '~> 7.1.1'
gem 'activesupport', '~> 7.1.1'
end

gemspec :path => '../'
16 changes: 9 additions & 7 deletions lib/octoball/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ class Octoball
module ConnectionHandlingAvoidAutoLoadProxy
private

def swap_connection_handler(handler, &blk)
old_handler, ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handler, handler
return_value = yield
return_value.load if !return_value.respond_to?(:ar_relation) && return_value.is_a?(ActiveRecord::Relation)
return_value
ensure
ActiveRecord::Base.connection_handler = old_handler
if ActiveRecord.gem_version < Gem::Version.new('7.1.0')
def swap_connection_handler(handler, &blk)
old_handler, ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handler, handler
return_value = yield
return_value.load if !return_value.respond_to?(:ar_relation) && return_value.is_a?(ActiveRecord::Relation)
return_value
ensure
ActiveRecord::Base.connection_handler = old_handler
end
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/octoball/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def sql(event)
private

def debug(progname = nil, &block)
conn = current_shard ? color("[Shard: #{current_shard}]", ActiveSupport::LogSubscriber::GREEN, true) : ''
if ActiveRecord.gem_version >= Gem::Version.new('7.1.0')
conn = current_shard ? color("[Shard: #{current_shard}]", ActiveSupport::LogSubscriber::GREEN, bold: true) : ''
else
conn = current_shard ? color("[Shard: #{current_shard}]", ActiveSupport::LogSubscriber::GREEN, true) : ''
end
super(conn + progname.to_s, &block)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/octoball/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@

it 'should clean #current_shard from proxy when using execute' do
User.using(:canada).connection.execute('select * from users limit 1;')
expect(User.connection.current_shard).to eq(:default)
expect(User.connection.current_shard).to eq(:master)
end

it 'should allow scoping dynamically' do
Expand Down Expand Up @@ -264,7 +264,7 @@

describe 'AR basic methods' do
it 'connects_to' do
expect(CustomConnection.connection.current_database).to eq('octoball_shard_2')
expect(CustomConnection.connection.current_database).to eq('octoball_shard_3')
end

it 'reuses parent model connection' do
Expand Down Expand Up @@ -590,7 +590,7 @@
describe 'custom connection' do
context 'by default' do
it 'with plain call should use custom connection' do
expect(CustomConnection.connection.current_database).to eq('octoball_shard_2')
expect(CustomConnection.connection.current_database).to eq('octoball_shard_3')
end

it 'should use model-specific shard' do
Expand Down

0 comments on commit 9a19e72

Please sign in to comment.