From d054a8fc0a5af1d40efa9b54c6efa19627aec025 Mon Sep 17 00:00:00 2001 From: Yianna Kokalas Date: Tue, 2 Apr 2024 15:54:47 -0700 Subject: [PATCH] Get started with Rails 7.1 support --- active_record_shards.gemspec | 4 ++-- gemfiles/rails7.1.gemfile | 3 +++ lib/active_record_shards.rb | 2 +- lib/active_record_shards/connection_switcher.rb | 2 +- lib/active_record_shards/shard_selection.rb | 7 ++++++- 5 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 gemfiles/rails7.1.gemfile diff --git a/active_record_shards.gemspec b/active_record_shards.gemspec index 5e0a7afc..d457f2e5 100644 --- a/active_record_shards.gemspec +++ b/active_record_shards.gemspec @@ -8,8 +8,8 @@ Gem::Specification.new "active_record_shards", "5.5.0" do |s| s.required_ruby_version = ">= 2.6" - s.add_runtime_dependency("activerecord", ">= 5.1", "< 7.1") - s.add_runtime_dependency("activesupport", ">= 5.1", "< 7.1") + s.add_runtime_dependency("activerecord", ">= 5.1", "<= 7.1") + s.add_runtime_dependency("activesupport", ">= 5.1", "<= 7.1") s.add_development_dependency("bump") s.add_development_dependency("minitest", ">= 5.10.0") diff --git a/gemfiles/rails7.1.gemfile b/gemfiles/rails7.1.gemfile new file mode 100644 index 00000000..4d078b43 --- /dev/null +++ b/gemfiles/rails7.1.gemfile @@ -0,0 +1,3 @@ +eval_gemfile 'common.rb' + +gem 'activerecord', '~> 7.1.0' diff --git a/lib/active_record_shards.rb b/lib/active_record_shards.rb index 794d51e5..c0ed2775 100644 --- a/lib/active_record_shards.rb +++ b/lib/active_record_shards.rb @@ -75,7 +75,7 @@ def self.reset_app_env! # https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/associations/preloader/association.rb#L96 ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsPreloaderAssociationLoadRecordsPatch) -when '6.0', '6.1', '7.0' +when '6.0', '6.1', '7.0', '7.1' # https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/type_caster/connection.rb#L28 ActiveRecord::TypeCaster::Connection.prepend(ActiveRecordShards::DefaultReplicaPatches::TypeCasterConnectionConnectionPatch) diff --git a/lib/active_record_shards/connection_switcher.rb b/lib/active_record_shards/connection_switcher.rb index 94ccaab4..07fc9521 100644 --- a/lib/active_record_shards/connection_switcher.rb +++ b/lib/active_record_shards/connection_switcher.rb @@ -271,7 +271,7 @@ def method_missing(method, *args, &block) # rubocop:disable Style/MethodMissingS require 'active_record_shards/connection_switcher-6-0' when '6.1' require 'active_record_shards/connection_switcher-6-1' -when '7.0' +when '7.0', '7.1' require 'active_record_shards/connection_switcher-7-0' else raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}" diff --git a/lib/active_record_shards/shard_selection.rb b/lib/active_record_shards/shard_selection.rb index d55db563..a7065e2c 100644 --- a/lib/active_record_shards/shard_selection.rb +++ b/lib/active_record_shards/shard_selection.rb @@ -35,7 +35,8 @@ def resolve_connection_name(sharded:, configurations:) name << "_shard_#{resolved_shard}" if resolved_shard replica_config = begin case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" - when '7.0' + when '7.0', '7.1' + binding.pry configurations.configs_for(env_name: "#{name}_replica", include_hidden: true).any? when '6.1' configurations.configs_for(env_name: "#{name}_replica", include_replicas: true).any? @@ -48,6 +49,10 @@ def resolve_connection_name(sharded:, configurations:) else # ActiveRecord always names its default connection pool 'primary' # while everything else is named by the configuration name + + # NOTE: for some reason this might need to return the environment. + # my 7.1 specs fail because see `connection_switcher-7-0.rb:8` + # configurations.configs_for(env_name: "primiary", include_hidden: true).any? == false resolved_shard ? name : PRIMARY end end