Skip to content

Commit

Permalink
Add support for Rails 7.2 (#39)
Browse files Browse the repository at this point in the history
* Add support for Rails 7.2+

* Update base file name

* Move files to `janus-ar` folder

* Update Readme

* Sync version with Rails

* Updates

* Make rubocop happy

* Rubocop
  • Loading branch information
lloydwatkin authored Aug 28, 2024
1 parent c7299f0 commit ae41f83
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ Style/GlobalVars:
Exclude:
- 'spec/**/*'

Naming/FileName:
Exclude:
- lib/janus-ar.rb

Metrics/AbcSize:
Max: 25
28 changes: 15 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
PATH
remote: .
specs:
janus-ar (0.15.2)
activerecord (>= 7.1.0, < 7.2)
janus-ar (7.2.0)
activerecord (~> 7.2)

GEM
remote: http://rubygems.org/
specs:
activemodel (7.1.3.4)
activesupport (= 7.1.3.4)
activerecord (7.1.3.4)
activemodel (= 7.1.3.4)
activesupport (= 7.1.3.4)
activemodel (7.2.1)
activesupport (= 7.2.1)
activerecord (7.2.1)
activemodel (= 7.2.1)
activesupport (= 7.2.1)
timeout (>= 0.4.0)
activesupport (7.1.3.4)
activesupport (7.2.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
Expand All @@ -35,9 +36,9 @@ GEM
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
logger (1.6.0)
method_source (1.0.0)
minitest (5.25.1)
mutex_m (0.2.0)
mysql2 (0.5.6)
parallel (1.26.3)
parser (3.3.4.2)
Expand Down Expand Up @@ -92,6 +93,7 @@ GEM
rubocop-thread_safety (0.5.1)
rubocop (>= 0.90.0)
ruby-progressbar (1.13.0)
securerandom (0.3.1)
strscan (3.1.0)
timeout (0.4.1)
trilogy (2.8.1)
Expand All @@ -104,7 +106,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
activesupport (>= 7.1.0, < 7.2)
activesupport (>= 7.2.0)
janus-ar!
mysql2
pry
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ This project assumes that your read/write endpoints are handled by a separate sy

After a write request during a thread the adapter will continue using the `primary` server, unless the context is specifically released.

## Rails 7.2+

For Rails 7.2 you'll need to manually register the database adaptor in `config/application.rb` after requiring rails but before entering the application configuration, e.g.

```ruby
require 'rails/all'

ActiveRecord::ConnectionAdapters.register("janus_trilogy", "ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter", 'janus-ar/active_record/connection_adapters/janus_trilogy_adapter')
# ...or...
ActiveRecord::ConnectionAdapters.register("janus_mysql2", "ActiveRecord::ConnectionAdapters::JanusMysql2Adapter", 'janus-ar/active_record/connection_adapters/janus_mysql2_adapter')
```

## Rails <= 7.1

ActiveRecord 7.1 was tested up to releases v0.15.*. After this release we only tested Rails 7.2+. This does not mean it is not compatible, just not tested.

### Configuration

Update your **database.yml** as follows:
Expand Down Expand Up @@ -104,6 +120,9 @@ There are some edge cases:
* Calls inside a transaction will always be sent to the primary (otherwise changes from within the transaction could not be read back on most transaction isolation levels)
* Locking reads (e.g. `SELECT ... FOR UPDATE`) will always be sent to the primary

# Notes

Janus does not support Rails' read/write split or sharding using `with_connection`.

# Acknowlegements

Expand Down
6 changes: 3 additions & 3 deletions janus-ar.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require File.expand_path('lib/janus/version.rb', __dir__)
require File.expand_path('lib/janus-ar/version.rb', __dir__)

Gem::Specification.new do |gem|
gem.authors = ['Lloyd Watkin']
Expand All @@ -21,8 +21,8 @@ Gem::Specification.new do |gem|

gem.required_ruby_version = '>= 3.2.0'

gem.add_dependency 'activerecord', '>= 7.1.0', '< 7.2'
gem.add_development_dependency 'activesupport', '>= 7.1.0', '< 7.2'
gem.add_dependency 'activerecord', '~> 7.2'
gem.add_development_dependency 'activesupport', '>= 7.2.0'
gem.add_development_dependency 'mysql2'
gem.add_development_dependency 'trilogy'
gem.add_development_dependency 'pry'
Expand Down
22 changes: 22 additions & 0 deletions lib/janus-ar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'active_support'

module Janus
autoload :Context, 'janus-ar/context'
autoload :Client, 'janus-ar/client'
autoload :QueryDirector, 'janus-ar/query_director'
autoload :VERSION, 'janus-ar/version'
autoload :DbConsoleConfig, 'janus-ar/db_console_config'

module Logging
autoload :Subscriber, 'janus-ar/logging/subscriber'
autoload :Logger, 'janus-ar/logging/logger'
end
end

ActiveSupport.on_load(:active_record) do
ActiveRecord::LogSubscriber.log_subscribers.each do |subscriber|
subscriber.extend Janus::Logging::Subscriber
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/mysql2_adapter'
require_relative '../../janus'
require_relative '../../../janus-ar'

module ActiveRecord
module ConnectionHandling
Expand Down Expand Up @@ -47,6 +47,10 @@ def initialize(*args)
update_config
end

def with_connection(_args = {})
self
end

def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
case where_to_send?(sql)
when :all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/trilogy_adapter'
require_relative '../../janus'
require_relative '../../../janus-ar'

module ActiveRecord
module ConnectionHandling
Expand Down Expand Up @@ -89,6 +89,10 @@ def execute_and_free(sql, name = nil, async: false)
end
end

def with_connection(_args = {})
self
end

def connect!(...)
replica_connection.connect!(...)
super
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/janus/version.rb → lib/janus-ar/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module Janus
unless defined?(::Janus::VERSION)
module VERSION
MAJOR = 0
MINOR = 15
PATCH = 3
MAJOR = 7
MINOR = 2
PATCH = 0
PRE = nil

def self.to_s
Expand Down
22 changes: 0 additions & 22 deletions lib/janus.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'ssl' => true,
'ssl_mode' => 'REQUIRED',
'tls_min_version' => Trilogy::TLS_VERSION_12,
'found_rows' => true,
}
end
let(:replica_config) do
Expand All @@ -25,6 +26,7 @@
'ssl' => true,
'ssl_mode' => 'REQUIRED',
'tls_min_version' => Trilogy::TLS_VERSION_12,
'found_rows' => true,
}
end
let(:config) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'janus/client'
require 'janus-ar/client'

RSpec.describe Janus::Client do
it { expect(described_class::FOUND_ROWS).to eq 2 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'janus/context'
require 'janus-ar/context'

RSpec.describe Janus::Context do
describe '#initialize' do
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

require 'active_record'

require './lib/janus'
require './lib/active_record/connection_adapters/janus_mysql2_adapter'
require './lib/active_record/connection_adapters/janus_trilogy_adapter'
require './lib/janus-ar'
require './lib/janus-ar/active_record/connection_adapters/janus_mysql2_adapter'
require './lib/janus-ar/active_record/connection_adapters/janus_trilogy_adapter'

ActiveRecord::ConnectionAdapters.register("janus_trilogy", "ActiveRecord::ConnectionAdapters::JanusTrilogyAdapter")
ActiveRecord::ConnectionAdapters.register("janus_mysql2", "ActiveRecord::ConnectionAdapters::JanusMysql2Adapter")

require './spec/shared_examples/a_mysql_like_server.rb'

Expand Down

0 comments on commit ae41f83

Please sign in to comment.