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

Fix SqlComments #334

Merged
merged 2 commits into from
May 13, 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
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 = ["bquorning@zendesk.com", "gabe@zendesk.com", "pschambacher@zendesk.com", "mick@staugaard.com"]
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
Loading