diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index db8e3dc..feb57d6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest services: mysql: - image: mysql:5.7 + image: mysql:8.0 env: MYSQL_ROOT_PASSWORD: pass ports: @@ -28,31 +28,35 @@ jobs: strategy: matrix: include: - - gemfile: rails5.0 - ruby: 2.6 - gemfile: rails5.1 - ruby: 2.6 + ruby: 3.1 + - gemfile: rails5.1 + ruby: 3.2 + - gemfile: rails5.2 - ruby: 2.6 + ruby: 3.1 - gemfile: rails5.2 - ruby: 3.0 + ruby: 3.2 - gemfile: rails6.0 - ruby: 2.7 + ruby: 3.1 + - gemfile: rails6.0 + ruby: 3.2 + - gemfile: rails6.1 - ruby: 2.7 + ruby: 3.2 - gemfile: rails6.1 - ruby: 3.0 + ruby: 3.3 - gemfile: rails7.0 - ruby: 3.0 + ruby: 3.2 - gemfile: rails7.0 - ruby: 3.1 + ruby: 3.3 - - gemfile: rails7.1 - ruby: 3.1 - gemfile: rails7.1 ruby: 3.2 + - gemfile: rails7.1 + ruby: 3.3 env: BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile diff --git a/.ruby-version b/.ruby-version index be94e6f..a0891f5 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.2.2 +3.3.4 diff --git a/Appraisals b/Appraisals index 2784573..26b8e35 100644 --- a/Appraisals +++ b/Appraisals @@ -1,23 +1,37 @@ appraise "rails5.1" do gem "activerecord", "~> 5.1.0" + gem "mysql2", "~> 0.5.2" + gem "sqlite3", "~> 1.7.3" end appraise "rails5.2" do gem "activerecord", "~> 5.2.0" + gem "mysql2", "~> 0.5.2" + gem "sqlite3", "~> 1.7.3" end appraise "rails6.0" do gem "activerecord", "~> 6.0.0" + gem "mysql2", "~> 0.5.2" + gem "sqlite3", "~> 1.7.3" end appraise "rails6.1" do gem "activerecord", "~> 6.1.0" + gem "mysql2", "~> 0.5.2" + gem "sqlite3", "~> 1.7.3" end appraise "rails7.0" do gem "activerecord", "~> 7.0.0" + gem "mysql2", "~> 0.5.2" + gem "sqlite3", "~> 1.7.3" end appraise "rails7.1" do gem "activerecord", "~> 7.1.0" end + +appraise "rails7.2" do + gem "activerecord", "~> 7.2.0" +end diff --git a/CHANGELOG.md b/CHANGELOG.md index eda1912..9407f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 0.6.0 2024-09-04 + +- Add support for Rails 7.2 + ## 0.5.1 2024-04-22 - Add kwargs argument to the ActiveRecordAbstractAdapter diff --git a/Gemfile b/Gemfile index 8c8b150..e6edf8e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,4 @@ source 'https://rubygems.org' -gem "activerecord", "~> 7.1.0" # Specify your gem's dependencies in octoshark.gemspec gemspec diff --git a/README.md b/README.md index 8885c8b..7b63ff0 100644 --- a/README.md +++ b/README.md @@ -109,21 +109,23 @@ To tell an ActiveRecord model to use the Octoshark connection we can override th ```ruby class Post < ActiveRecord::Base - def self.connection - CONN_MANAGER.current_connection - end -end -``` + class << self + def connection + CONN_MANAGER.current_connection + end -Alternatively, we can extract it as a module and include in multiple models. + # For Rails 7.2+ we need to override the following methods too: -```ruby -module ShardingModel - extend ActiveSupport::Concern + def lease_connection + connection + end - module ClassMethods - def connection - CONN_MANAGER.current_connection + def with_connection + yield connection + end + + def schema_cache + connection.schema_cache end end end diff --git a/gemfiles/rails5.1.gemfile b/gemfiles/rails5.1.gemfile index e2f8f85..e3c70d2 100644 --- a/gemfiles/rails5.1.gemfile +++ b/gemfiles/rails5.1.gemfile @@ -3,5 +3,7 @@ source "https://rubygems.org" gem "activerecord", "~> 5.1.0" +gem "mysql2", "~> 0.5.2" +gem "sqlite3", "~> 1.7.3" gemspec path: "../" diff --git a/gemfiles/rails5.2.gemfile b/gemfiles/rails5.2.gemfile index 027888d..a3c3a16 100644 --- a/gemfiles/rails5.2.gemfile +++ b/gemfiles/rails5.2.gemfile @@ -3,5 +3,7 @@ source "https://rubygems.org" gem "activerecord", "~> 5.2.0" +gem "mysql2", "~> 0.5.2" +gem "sqlite3", "~> 1.7.3" gemspec path: "../" diff --git a/gemfiles/rails6.0.gemfile b/gemfiles/rails6.0.gemfile index b07bd13..3c76ef8 100644 --- a/gemfiles/rails6.0.gemfile +++ b/gemfiles/rails6.0.gemfile @@ -3,5 +3,7 @@ source "https://rubygems.org" gem "activerecord", "~> 6.0.0" +gem "mysql2", "~> 0.5.2" +gem "sqlite3", "~> 1.7.3" gemspec path: "../" diff --git a/gemfiles/rails6.1.gemfile b/gemfiles/rails6.1.gemfile index 07548db..7270569 100644 --- a/gemfiles/rails6.1.gemfile +++ b/gemfiles/rails6.1.gemfile @@ -3,5 +3,7 @@ source "https://rubygems.org" gem "activerecord", "~> 6.1.0" +gem "mysql2", "~> 0.5.2" +gem "sqlite3", "~> 1.7.3" gemspec path: "../" diff --git a/gemfiles/rails7.0.gemfile b/gemfiles/rails7.0.gemfile index bc1dfc9..c2eb66e 100644 --- a/gemfiles/rails7.0.gemfile +++ b/gemfiles/rails7.0.gemfile @@ -3,5 +3,7 @@ source "https://rubygems.org" gem "activerecord", "~> 7.0.0" +gem "mysql2", "~> 0.5.2" +gem "sqlite3", "~> 1.7.3" gemspec path: "../" diff --git a/gemfiles/rails5.0.gemfile b/gemfiles/rails7.2.gemfile similarity index 52% rename from gemfiles/rails5.0.gemfile rename to gemfiles/rails7.2.gemfile index bdefa96..9a0f1ea 100644 --- a/gemfiles/rails5.0.gemfile +++ b/gemfiles/rails7.2.gemfile @@ -2,8 +2,6 @@ source "https://rubygems.org" -gem "activerecord", "~> 5.0.0" -gem "mysql2", "~> 0.5.2" -gem "sqlite3", "~> 1.3.13" +gem "activerecord", "~> 7.2.0" gemspec path: "../" diff --git a/lib/octoshark/active_record_extensions.rb b/lib/octoshark/active_record_extensions.rb index 849965d..4af24a7 100644 --- a/lib/octoshark/active_record_extensions.rb +++ b/lib/octoshark/active_record_extensions.rb @@ -34,6 +34,18 @@ def log(sql, name = "SQL", *other_args, **kwargs, &block) super(sql, name, *other_args, **kwargs, &block) end end + + module ConnectionPool + # Handle Rails 8.0 ConnectionPool#connection deprecation + def connection + if respond_to?(:lease_connection) + # Rails 7.2+ + lease_connection + else + super + end + end + end end # Rails 3.0 and 3.1 does not lazy load @@ -43,3 +55,4 @@ def log(sql, name = "SQL", *other_args, **kwargs, &block) ActiveRecord::ConnectionAdapters::ConnectionHandler.send(:prepend, Octoshark::ConnectionHandler) ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:prepend, Octoshark::ActiveRecordAbstractAdapter) +ActiveRecord::ConnectionAdapters::ConnectionPool.send(:prepend, Octoshark::ConnectionPool) diff --git a/lib/octoshark/connection_manager.rb b/lib/octoshark/connection_manager.rb index c9197fc..fdc3a65 100644 --- a/lib/octoshark/connection_manager.rb +++ b/lib/octoshark/connection_manager.rb @@ -3,9 +3,15 @@ class ConnectionManager include CurrentConnection def with_connection(config, connection_name: nil, &block) - connection_method = "#{config[:adapter]}_connection" + connection = + if ActiveRecord::ConnectionAdapters.respond_to?(:resolve) + # Rails 7.2+ + ActiveRecord::ConnectionAdapters.resolve(config[:adapter]).new(config) + else + connection_method = "#{config[:adapter]}_connection" + ActiveRecord::Base.send(connection_method, config) + end - connection = ActiveRecord::Base.send(connection_method, config) connection.connection_name = connection_name connection.database_name = config[:database] if config[:database] diff --git a/lib/octoshark/version.rb b/lib/octoshark/version.rb index ea66066..49283d3 100644 --- a/lib/octoshark/version.rb +++ b/lib/octoshark/version.rb @@ -1,3 +1,3 @@ module Octoshark - VERSION = "0.5.1" + VERSION = "0.6.0" end diff --git a/octoshark.gemspec b/octoshark.gemspec index 209216f..d569f76 100644 --- a/octoshark.gemspec +++ b/octoshark.gemspec @@ -21,8 +21,9 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "activerecord", ">= 3.0" spec.add_development_dependency "rake" - spec.add_development_dependency "rspec", "~> 3.12.0" - spec.add_development_dependency "sqlite3", "~> 1.4.1" - spec.add_development_dependency "mysql2", "~> 0.5.2" + spec.add_development_dependency "rspec" + spec.add_development_dependency "activerecord", "~> 7.2.0" + spec.add_development_dependency "sqlite3" + spec.add_development_dependency "mysql2" spec.add_development_dependency "appraisal" end