diff --git a/.github/gemfiles/rails_7.1.gemfile b/.github/gemfiles/rails_7.1.gemfile deleted file mode 100644 index a17a9e9..0000000 --- a/.github/gemfiles/rails_7.1.gemfile +++ /dev/null @@ -1,6 +0,0 @@ -source 'https://rubygems.org' - -gemspec :path => '../../' - -gem 'activerecord', '~> 7.1.0' -gem 'activesupport', '~> 7.1.0' diff --git a/.github/gemfiles/rails_8.1.gemfile b/.github/gemfiles/rails_8.1.gemfile new file mode 100644 index 0000000..3684078 --- /dev/null +++ b/.github/gemfiles/rails_8.1.gemfile @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +gemspec :path => '../../' + +gem 'activerecord', '~> 8.1.0' +gem 'activesupport', '~> 8.1.0' diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index 9a4a60e..2a3ae2f 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -17,8 +17,8 @@ jobs: strategy: fail-fast: true matrix: - ruby: ["3.2","3.3", "3.4"] - rails: ["7.1", "7.2", "8.0"] + ruby: ["3.2", "3.3", "3.4"] + rails: ["7.2", "8.0", "8.1"] env: BUNDLE_GEMFILE: .github/gemfiles/rails_${{ matrix.rails }}.gemfile MYSQL_HOST: 127.0.0.1 diff --git a/Rakefile b/Rakefile index 6bfa273..92d9028 100644 --- a/Rakefile +++ b/Rakefile @@ -46,11 +46,7 @@ namespace :db do require './spec/models/application_record' ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |config| ActiveRecord::Base.establish_connection(config) - schema_migration = if ActiveRecord.gem_version >= Gem::Version.new(7.2) - ActiveRecord::Base.connection.pool.schema_migration - else - ActiveRecord::Base.connection.schema_migration - end + schema_migration = ActiveRecord::Base.connection.pool.schema_migration ActiveRecord::MigrationContext.new("spec/migration", schema_migration) .migrate(config.database == 'octoball_shard_5' ? 2 : 1) end diff --git a/lib/octoball.rb b/lib/octoball.rb index dea60af..63a8d15 100644 --- a/lib/octoball.rb +++ b/lib/octoball.rb @@ -6,7 +6,6 @@ ActiveSupport.on_load(:active_record) do require 'octoball/relation_proxy' require 'octoball/connection_adapters' - require 'octoball/connection_handling' require 'octoball/current_shard_tracker' require 'octoball/association_shard_check' require 'octoball/persistence' diff --git a/lib/octoball/connection_adapters.rb b/lib/octoball/connection_adapters.rb index 7585a36..557c849 100644 --- a/lib/octoball/connection_adapters.rb +++ b/lib/octoball/connection_adapters.rb @@ -13,41 +13,38 @@ module ConnectionHasCurrentShard attr_accessor :current_shard end - if ActiveRecord.gem_version >= Gem::Version.new('7.2.0') - module ConnectionPoolSetCurrentShard - def with_connection(prevent_permanent_checkout: false) - lease = connection_lease - if lease.connection - lease.connection.current_shard = lease.connection.shard - end - - super + module ConnectionPoolSetCurrentShard + def with_connection(prevent_permanent_checkout: false) + lease = connection_lease + if lease.connection + lease.connection.current_shard = lease.connection.shard end - def active_connection? - conn = connection_lease.connection - conn.current_shard = conn.shard if conn - conn - end + super + end - def active_connection - conn = connection_lease.connection - conn.current_shard = conn.shard if conn - conn - end + def active_connection? + conn = connection_lease.connection + conn.current_shard = conn.shard if conn + conn + end - def lease_connection - lease = connection_lease - lease.sticky = true - lease.connection ||= checkout - lease.connection.current_shard = lease.connection.shard - lease.connection - end + def active_connection + conn = connection_lease.connection + conn.current_shard = conn.shard if conn + conn end - ::ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(ConnectionPoolSetCurrentShard) + def lease_connection + lease = connection_lease + lease.sticky = true + lease.connection ||= checkout + lease.connection.current_shard = lease.connection.shard + lease.connection + end end + ::ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(ConnectionPoolSetCurrentShard) ::ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ConnectionHandlerSetCurrentShard) ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ConnectionHasCurrentShard) end diff --git a/lib/octoball/connection_handling.rb b/lib/octoball/connection_handling.rb deleted file mode 100644 index 8b62162..0000000 --- a/lib/octoball/connection_handling.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -class Octoball - module ConnectionHandlingAvoidAutoLoadProxy - private - - 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 - - ::ActiveRecord::Base.extend(ConnectionHandlingAvoidAutoLoadProxy) -end diff --git a/lib/octoball/log_subscriber.rb b/lib/octoball/log_subscriber.rb index b30dfa3..c684034 100644 --- a/lib/octoball/log_subscriber.rb +++ b/lib/octoball/log_subscriber.rb @@ -12,11 +12,7 @@ def sql(event) private def debug(progname = nil, &block) - 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 + conn = current_shard ? color("[Shard: #{current_shard}]", ActiveSupport::LogSubscriber::GREEN, bold: true) : '' super(conn + progname.to_s, &block) end end diff --git a/octoball.gemspec b/octoball.gemspec index 422dce7..9990800 100644 --- a/octoball.gemspec +++ b/octoball.gemspec @@ -17,8 +17,8 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 3.2.0' - s.add_dependency 'activerecord', '>= 7.0' - s.add_dependency 'activesupport', '>= 7.0' + s.add_dependency 'activerecord', '>= 7.2' + s.add_dependency 'activesupport', '>= 7.2' s.add_development_dependency 'trilogy' s.add_development_dependency 'rake'