Module: ActiveRecordShards

Defined in:
lib/active_record_shards/sql_comments.rb,
lib/active_record_shards.rb,
lib/active_record_shards/model.rb,
lib/active_record_shards/tasks.rb,
lib/active_record_shards/migration.rb,
lib/active_record_shards/deprecation.rb,
lib/active_record_shards/shard_support.rb,
lib/active_record_shards/connection_pool.rb,
lib/active_record_shards/shard_selection.rb,
lib/active_record_shards/connection_switcher.rb,
lib/active_record_shards/configuration_parser.rb,
lib/active_record_shards/default_slave_patches.rb,
lib/active_record_shards/connection_switcher-4-2.rb,
lib/active_record_shards/connection_switcher-5-0.rb,
lib/active_record_shards/connection_switcher-5-1.rb,
lib/active_record_shards/connection_switcher-6-0.rb,
lib/active_record_shards/default_replica_patches.rb,
lib/active_record_shards/schema_dumper_extension.rb,
lib/active_record_shards/association_collection_connection_selection.rb

Overview

show which connection was picked to debug primary/replica slowness when both servers are the same

Defined Under Namespace

Modules: ActualMigrationExtension, AssociationCollectionConnectionSelection, ConfigurationParser, ConnectionSwitcher, DefaultReplicaPatches, MigrationClassExtension, Model, SchemaDumperExtension, SqlComments, Tasks Classes: ConnectionPoolNameDecorator, Deprecation, ShardSelection, ShardSupport

Constant Summary collapse

ConnectionSpecification =
ActiveRecord::ConnectionAdapters::ConnectionSpecification
DefaultSlavePatches =
DefaultReplicaPatches

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.disable_replica_readonly_recordsObject

Returns the value of attribute disable_replica_readonly_records.



16
17
18
# File 'lib/active_record_shards.rb', line 16

def disable_replica_readonly_records
  @disable_replica_readonly_records
end

Class Method Details

.app_envObject



19
20
21
22
23
24
25
26
# File 'lib/active_record_shards.rb', line 19

def self.app_env
  env = Rails.env if defined?(Rails.env)
  env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
  env ||= ENV['RAILS_ENV']
  env ||= APP_ENV if Object.const_defined?(:APP_ENV)
  env ||= ENV['APP_ENV']
  env || 'development'
end

.override_connection_handler_methods(method_names) ⇒ Object

It overrides given connection handler methods (they differ depend on Rails version).

It takes the first argument, ActiveRecord::Base object or String (connection_pool_name), converts it in Struct object and passes to the original method.

Example:

methods_to_override = [:establish_connection, :remove_connection]
ActiveRecordShards.override_connection_handler_methods(methods_to_override)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_record_shards/connection_pool.rb', line 17

def self.override_connection_handler_methods(method_names)
  method_names.each do |method_name|
    ActiveRecord::ConnectionAdapters::ConnectionHandler.class_eval do
      define_method("#{method_name}_with_connection_pool_name") do |*args|
        unless args[0].is_a? ConnectionPoolNameDecorator
          name = if args[0].is_a? String
                   args[0]
                 else
                   args[0].connection_pool_name
                 end
          args[0] = ConnectionPoolNameDecorator.new(name)
        end
        send("#{method_name}_without_connection_pool_name", *args)
      end
      alias_method :"#{method_name}_without_connection_pool_name", method_name
      alias_method method_name, :"#{method_name}_with_connection_pool_name"
    end
  end
end