Skip to content

Choosing Primary instead of Replica when using 'with' (because of newlines) #376

Description

@jackcb123

Issue: Many of my complex sql statements were being performed by the primary instead of the replica. It turns out that the sql_replica_matchers regex is insufficient for queries that begin with CTE 'with' statements.

In lib/active_record/connection_adapters/makara_abstract_adapter.rb

SQL_REPLICA_MATCHERS = [/\A\s*(select|with.+\)\s*select)\s/i].map(&:freeze).freeze

should be changed to

SQL_REPLICA_MATCHERS = [/\A\s*(select|with[\s\S]*\)\s*select)\s/i].map(&:freeze).freeze

[\s\S] is the difference and accounts for new lines.

This is because '.' regex doesn't account for new lines (which will obviously be there in complex sql statements).

My workaround is this. Hope this helps! :)

class ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter < ::Makara::Proxy
  CUSTOM_SQL_REPLICA_MATCHERS = [/\A\s*(select|with[\s\S]*\)\s*select)\s/i].map(&:freeze).freeze

  def sql_slave_matchers
    CUSTOM_SQL_REPLICA_MATCHERS
  end

  def sql_replica_matchers
    CUSTOM_SQL_REPLICA_MATCHERS
  end
end

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