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

Support rails7.1 #9

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading