Describe the bug
Rails query-log tags and other leading SQL comments prevent the proxy from recognizing the statement at the start of the SQL string. A tagged SELECT can therefore be sent to the primary, and tagged SET, SET LOCAL, and write statements can be classified incorrectly.
Steps to reproduce
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activerecord', '8.1.3.1'
gem 'active_record_proxy_adapters', '0.11.1'
end
require 'active_record_proxy_adapters'
DbConfig = Struct.new(:name)
Pool = Struct.new(:db_config)
Connection = Struct.new(:pool)
connection = Connection.new(Pool.new(DbConfig.new('primary')))
proxy = ActiveRecordProxyAdapters::PrimaryReplicaProxy.new(connection)
sql = '/*application:api*/ SELECT 1'
normalized = proxy.send(:coerce_query_to_string, sql)
p normalized
p proxy.send(:need_primary?, normalized)
No database connection is required to reproduce the classification result.
Expected behavior
Leading whitespace and comments are ignored for routing. The normalized statement is SELECT 1, and need_primary? returns false so the read can use the replica.
Actual behavior
The normalized statement remains /*application:api*/ SELECT 1, and need_primary? returns true because no read matcher recognizes it.
System configuration
Active Record version: 8.1.3.1
Active Record proxy adapters version: 0.11.1
Ruby version: 4.0.6
Describe the bug
Rails query-log tags and other leading SQL comments prevent the proxy from recognizing the statement at the start of the SQL string. A tagged SELECT can therefore be sent to the primary, and tagged SET, SET LOCAL, and write statements can be classified incorrectly.
Steps to reproduce
No database connection is required to reproduce the classification result.
Expected behavior
Leading whitespace and comments are ignored for routing. The normalized statement is
SELECT 1, andneed_primary?returnsfalseso the read can use the replica.Actual behavior
The normalized statement remains
/*application:api*/ SELECT 1, andneed_primary?returnstruebecause no read matcher recognizes it.System configuration
Active Record version: 8.1.3.1
Active Record proxy adapters version: 0.11.1
Ruby version: 4.0.6