Skip to content

Leading SQL comments bypass proxy routing classification #311

Description

@OskarEichler

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions