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

Override master db selection method not working #346

Open
aman199002 opened this issue Feb 11, 2022 · 2 comments
Open

Override master db selection method not working #346

aman199002 opened this issue Feb 11, 2022 · 2 comments

Comments

@aman199002
Copy link

aman199002 commented Feb 11, 2022

I need to modify the default condition for master db selection in my application based on some cached value. I have defined a custom proxy class as mentioned in documentation in config/initializers directory.

class MyAwesomeSqlProxy < ::Makara::Proxy
  hijack_method :select, :ping
  send_to_all :connect, :reconnect, :disconnect, :clear_cache

  def connection_for(config)
    ::Sql::Client.new(config)
  end

  def needs_master?(method_name, args)
    # Modified this method for testing to always route to master    
    return true  
  end
end

Any changes in this class does not seem to work. Do I need to define this class at some other location and load explicitly? Or this feature to override db selection is not supported as mentioned in documentation?

@bleonard
Copy link
Contributor

bleonard commented Feb 12, 2022

It's been awhile for sure, but I don't see your database.yml here.
There is some thing to set this Proxy to the adapter: in your database.yml.

I'd consider extending to one you need, too. For example if you are using MySQL, do

module ActiveRecord
  module ConnectionAdapters
    class MakaraTestingAdapter < ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter
       def needs_primary?(method_name, args)
          # Modified this method for testing to always route to primary    
          return true  
      end
  end
end

database.yml

production:
  adapter: 'testing'

  makara:
   ...

@aman199002
Copy link
Author

aman199002 commented Feb 14, 2022

I tried the same with postgresql as mentioned below.

config/initializes/postgresql_makara_custom_adapter.rb

module ActiveRecord
  module ConnectionAdapters
    class PostgresqlMakaraCustomAdapter < ::Makara::Proxy
      
      def needs_primary?(method_name, args)
          # Modified this method for testing to always route to primary    
          return true  
      end

    end
  end
end
  

database.yml

production:
  adapter: 'postgresql_makara_custom'
  makara:
   ...

Getting active_record error on load when running the application:
activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:196:in `spec': undefined method `config' for #<Hash:0x00007fc4a0e036e0> (NoMethodError)

However I checked the code inside gem and tried by overriding the makara_abstract_adapter class via monkey patching in my application as mentioned below:
config/initializes/makara_abstract_adapter.rb

module ActiveRecord
  module ConnectionAdapters
    class MakaraAbstractAdapter < ::Makara::Proxy

      protected

      def needs_master?(method_name, args)
        return true if replication_lag?
        sql = coerce_query_to_sql_string(args.first)
        return true if sql_master_matchers.any?{|m| sql =~ m }
        return false if sql_slave_matchers.any?{|m| sql =~ m }
        true
      end
      
      private

      def replication_lag?
        ....
      end

    end
  end
end

database.yml

production:
  adapter: postgresql_makara
  makara:
   ...

The same is working and I can modify the condition inside needs_master method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants