<commit_before>module ActiveRecord
  module ConnectionHandling

    #
    # Active record will call this method to 
    # establish a connection. This will in
    # turn create multiple connections to the
    # different databases supplied by the
    # configuration
    #
    # @param config The database configuration
    #
    def ai_failover_connection(config)
      config = config.with_indifferent_access
      default_config = config.merge(adapter: config[:node_adapter]).with_indifferent_access
      default_config.delete(:hosts)
      default_config.delete(:urls)
      default_config.delete(:node_adapter)

      establish_adapter(default_config[:adapter])
      db_destinations = (config[:urls] || config[:hosts])
      db_conn_key = config[:urls] ? :url : :host

      db_connections = []
      db_destinations.each do |dest|
        conn_config = {}
        conn_config[db_conn_key] = dest
        db_config = default_config.merge(conn_config)

        begin
          establish_adapter(db_config[:adapter])
          conn = send("#{db_config[:adapter]}_connection".to_sym, db_config)
          db_connections << conn
        rescue StandardError => e
          if logger
            logger.error("Error connecting to database #{db_config.inspect}")
            logger.error(e)
          end
        end
      end

      if db_connections.length == 0
        raise 'Unable to connect to any of the specified databases'
      elsif db_connections.length < db_destinations.length
        logger.warn("Unable to connect to all specified databases. Conntected to #{db_connections.length}")
      end

      klass = ::ActiveRecord::ConnectionAdapters::AiFailoverAdapter.adapter_class(db_connections.first)
      klass.new(nil, logger, db_connections)

    end

    #
    # This method ensures the actual adapter
    # that is needed is loaded.
    #
    def establish_adapter(adapter)
      raise AdapterNotSpecified.new('Database configuration does not specify adapter') unless adapter
      raise AdapterNotFound.new('Database configuration must specify adapters. Do not use ai_failover_adapter') if adapter == 'ai_failover_adapter'

      if defined?(JRuby)
        ar_adapter = "jdbc#{adapter}"
      end

      begin
        require 'rubygems'
        gem "activerecord-#{ar_adapter}-adapter"
        require "active_record/connection_adapters/#{ar_adapter}_adapter"
      rescue LoadError
        begin
          require "active_record/connection_adapters/#{ar_adapter}_adapter"
        rescue LoadError
          raise "Please install the #{ar_adapter}: `gem install activerecord-#{ar_adapter}-adapter` (#{$!})"
        end
      end

      adapter_method = "#{adapter}_connection"
      if !respond_to?(adapter_method)
        raise AdapterNotFound, "Database configuration specifies nonexistent #{adapter} adapter"
      end

    end

  end

  module ConnectionAdapters
    class AiFailoverAdapter < AbstractAdapter

      attr_reader :available_connections
      attr_reader :current_connection

      class << self

        def adapter_class(connection)
          adapter_class_name = connection.adapter_name.classify
          return const_get(adapter_class_name) if const_defined?(adapter_class_name, false)

          connection_methods = []
          override_classes = (connection.class.ancestors - AbstractAdapter.ancestors)
          override_classes.each do |connection_class|
            connection_methods.concat(connection_class.public_instance_methods(false))
            connection_methods.concat(connection_class.protected_instance_methods(false))
          end
          connection_methods = connection_methods.collect { |m| m.to_sym }.uniq
          connection_methods -= public_instance_methods(false) + protected_instance_methods(false) + private_instance_methods(false)

          klass = Class.new(self)
          connection_methods.each do |method_name|
            klass.class_eval <<-EOS, __FILE__, __LINE__ + 1
              def #{method_name}(*args, &block)
                connection = current_connection
                return proxy_connection_method(connection, :#{method_name}, *args, &block)
              end
            EOS
          end

          const_set(adapter_class_name, klass)

          return klass
        end

        def visitor_for(pool)
          # This is ugly, but then again, so is the code in ActiveRecord for setting the arel
          # visitor. There is a note in the code indicating the method signatures should be updated.
          config = pool.spec.config.with_indifferent_access
          adapter = config[:node_adapter]
          AiFailoverAdapter.adapter_class_for(adapter).visitor_for(pool)
        end

      end

      def initialize(connection, logger, available_connections)
        @all_connections = available_connections.dup.freeze
        @available_connections = available_connections.each_with_index.map { |conn, idx| AvailableConnection.new(conn, idx) }
        super(connection, logger)
      end

      def adapter_name
        'Ai_Failover_Adapter'
      end

      def all_connections
        @all_connections
      end

      def current_connection
        available_connections.first.try(:connection)
      end

      def requires_reloading?
        false
      end

      def visitor=(visitor)
        @all_connections.each { |conn| conn.visitor = visitor }
      end
<commit_msg>Remove override of the adapter name<commit_after>module ActiveRecord
  module ConnectionHandling

    #
    # Active record will call this method to 
    # establish a connection. This will in
    # turn create multiple connections to the
    # different databases supplied by the
    # configuration
    #
    # @param config The database configuration
    #
    def ai_failover_connection(config)
      config = config.with_indifferent_access
      default_config = config.merge(adapter: config[:node_adapter]).with_indifferent_access
      default_config.delete(:hosts)
      default_config.delete(:urls)
      default_config.delete(:node_adapter)

      establish_adapter(default_config[:adapter])
      db_destinations = (config[:urls] || config[:hosts])
      db_conn_key = config[:urls] ? :url : :host

      db_connections = []
      db_destinations.each do |dest|
        conn_config = {}
        conn_config[db_conn_key] = dest
        db_config = default_config.merge(conn_config)

        begin
          establish_adapter(db_config[:adapter])
          conn = send("#{db_config[:adapter]}_connection".to_sym, db_config)
          db_connections << conn
        rescue StandardError => e
          if logger
            logger.error("Error connecting to database #{db_config.inspect}")
            logger.error(e)
          end
        end
      end

      if db_connections.length == 0
        raise 'Unable to connect to any of the specified databases'
      elsif db_connections.length < db_destinations.length
        logger.warn("Unable to connect to all specified databases. Conntected to #{db_connections.length}")
      end

      klass = ::ActiveRecord::ConnectionAdapters::AiFailoverAdapter.adapter_class(db_connections.first)
      klass.new(nil, logger, db_connections)

    end

    #
    # This method ensures the actual adapter
    # that is needed is loaded.
    #
    def establish_adapter(adapter)
      raise AdapterNotSpecified.new('Database configuration does not specify adapter') unless adapter
      raise AdapterNotFound.new('Database configuration must specify adapters. Do not use ai_failover_adapter') if adapter == 'ai_failover_adapter'

      if defined?(JRuby)
        ar_adapter = "jdbc#{adapter}"
      end

      begin
        require 'rubygems'
        gem "activerecord-#{ar_adapter}-adapter"
        require "active_record/connection_adapters/#{ar_adapter}_adapter"
      rescue LoadError
        begin
          require "active_record/connection_adapters/#{ar_adapter}_adapter"
        rescue LoadError
          raise "Please install the #{ar_adapter}: `gem install activerecord-#{ar_adapter}-adapter` (#{$!})"
        end
      end

      adapter_method = "#{adapter}_connection"
      if !respond_to?(adapter_method)
        raise AdapterNotFound, "Database configuration specifies nonexistent #{adapter} adapter"
      end

    end

  end

  module ConnectionAdapters
    class AiFailoverAdapter < AbstractAdapter

      attr_reader :available_connections
      attr_reader :current_connection

      class << self

        def adapter_class(connection)
          adapter_class_name = connection.adapter_name.classify
          return const_get(adapter_class_name) if const_defined?(adapter_class_name, false)

          connection_methods = []
          override_classes = (connection.class.ancestors - AbstractAdapter.ancestors)
          override_classes.each do |connection_class|
            connection_methods.concat(connection_class.public_instance_methods(false))
            connection_methods.concat(connection_class.protected_instance_methods(false))
          end
          connection_methods = connection_methods.collect { |m| m.to_sym }.uniq
          connection_methods -= public_instance_methods(false) + protected_instance_methods(false) + private_instance_methods(false)

          klass = Class.new(self)
          connection_methods.each do |method_name|
            klass.class_eval <<-EOS, __FILE__, __LINE__ + 1
              def #{method_name}(*args, &block)
                connection = current_connection
                return proxy_connection_method(connection, :#{method_name}, *args, &block)
              end
            EOS
          end

          const_set(adapter_class_name, klass)

          return klass
        end

        def visitor_for(pool)
          # This is ugly, but then again, so is the code in ActiveRecord for setting the arel
          # visitor. There is a note in the code indicating the method signatures should be updated.
          config = pool.spec.config.with_indifferent_access
          adapter = config[:node_adapter]
          AiFailoverAdapter.adapter_class_for(adapter).visitor_for(pool)
        end

      end

      def initialize(connection, logger, available_connections)
        @all_connections = available_connections.dup.freeze
        @available_connections = available_connections.each_with_index.map { |conn, idx| AvailableConnection.new(conn, idx) }
        super(connection, logger)
      end

      # def adapter_name
      #   'Ai_Failover_Adapter'
      # end

      def all_connections
        @all_connections
      end

      def current_connection
        available_connections.first.try(:connection)
      end

      def requires_reloading?
        false
      end

      def visitor=(visitor)
        @all_connections.each { |conn| conn.visitor = visitor }
      end

{'fraction_non_alphanumeric': 0.0742216456634544, 'fraction_numerical': 0.00037064492216456633, 'mean_word_length': 2.746268656716418, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1085932', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2793, 'n_words': 683}