Skip to content

Commit

Permalink
Fix SqlComments
Browse files Browse the repository at this point in the history
`SqlComments` is broken in Rails 7.0 because it now passes an `async`
kwarg[^1] when it used to only have the two positional arguments.

[^1]:(https://github.com/rails/rails/blob/5873d6279a2b2ec11d0176da66df17b076733791/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L194)

I've updated the tests to... actually test with Rails. Before it didn't
run any Rails code at all.
  • Loading branch information
HeyNonster committed May 12, 2024
1 parent a99b759 commit 9cca5f7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
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 new_logger.string.include? "/* 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 9cca5f7

Please sign in to comment.