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

Add support for Rails 7.2 #46

Merged
merged 2 commits into from
Sep 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
30 changes: 17 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.2
3.3.4
14 changes: 14 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
source 'https://rubygems.org'
gem "activerecord", "~> 7.1.0"

# Specify your gem's dependencies in octoshark.gemspec
gemspec
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
2 changes: 2 additions & 0 deletions gemfiles/rails5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
2 changes: 2 additions & 0 deletions gemfiles/rails6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
2 changes: 2 additions & 0 deletions gemfiles/rails6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
2 changes: 2 additions & 0 deletions gemfiles/rails7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
4 changes: 1 addition & 3 deletions gemfiles/rails5.0.gemfile → gemfiles/rails7.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
13 changes: 13 additions & 0 deletions lib/octoshark/active_record_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
10 changes: 8 additions & 2 deletions lib/octoshark/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion lib/octoshark/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Octoshark
VERSION = "0.5.1"
VERSION = "0.6.0"
end
7 changes: 4 additions & 3 deletions octoshark.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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