Skip to content

Commit

Permalink
Merge pull request #334 from zendesk/nony--fix-rails-7-0
Browse files Browse the repository at this point in the history
Fix SqlComments
  • Loading branch information
HeyNonster authored May 13, 2024
2 parents a99b759 + 2d78503 commit 3c30e44
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## v5.5.1
### Fixed
* Fixed `SqlComments` when using Rails 7.0.

## v5.5.0

### Changed
Expand Down
2 changes: 1 addition & 1 deletion active_record_shards.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Gem::Specification.new "active_record_shards", "5.5.0" do |s|
Gem::Specification.new "active_record_shards", "5.5.1" do |s|
s.authors = ["Benjamin Quorning", "Gabe Martin-Dempesy", "Pierre Schambacher", "Mick Staugaard", "Eric Chapweske", "Ben Osheroff"]
s.email = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
s.homepage = "https://github.com/zendesk/active_record_shards"
Expand Down
4 changes: 2 additions & 2 deletions lib/active_record_shards/sql_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
module ActiveRecordShards
module SqlComments
module Methods
def execute(query, name = nil)
def execute(query, name = nil, **kwargs)
shard = ActiveRecord::Base.current_shard_selection.shard
shard_text = shard ? "shard #{shard}" : 'unsharded'
replica = ActiveRecord::Base.current_shard_selection.on_replica?
replica_text = replica ? 'replica' : 'primary'
query = "/* #{shard_text} #{replica_text} */ " + query
super(query, name)
super(query, name, **kwargs)
end
end

Expand Down
27 changes: 18 additions & 9 deletions test/active_record_shards/sql_comments_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

require_relative '../helper'
require 'active_record_shards/sql_comments'
require 'active_record/connection_adapters/mysql2_adapter'

describe ActiveRecordShards::SqlComments do
with_fresh_databases

class CommentTester
attr_reader :called
class CustomAdapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter
prepend ActiveRecordShards::SqlComments::Methods

def execute(query, _name = nil)
(@called ||= []) << query
end
end

let(:comment) { CommentTester.new }
before do
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
end

it "adds sql comment" do
comment.execute("foo")
assert_equal ["/* unsharded primary */ foo"], comment.called
old_logger = ActiveRecord::Base.logger
new_logger = StringIO.new
ActiveRecord::Base.logger = Logger.new(new_logger)
config = Account.connection.instance_variable_get(:@config)
custom_connection = CustomAdapter.new(Mysql2::Client.new(config), nil, nil, config)

Account.stub :connection, custom_connection do
Account.first
end

assert_includes(new_logger.string, "/* unsharded primary */")
ensure
ActiveRecord::Base.logger = old_logger
end
end
1 change: 0 additions & 1 deletion test/connection_switching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def clear_connection_pool

before do
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
require 'models'
end

describe "legacy_connection_handling" do
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
BaseMigration = ActiveRecord::Migration[4.2]

require 'active_support/test_case'
require 'models'

# support multiple before/after blocks per example
module SpecDslPatch
Expand Down

0 comments on commit 3c30e44

Please sign in to comment.