Class: ActiveRecordShards::ShardSelection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_shards/shard_selection.rb

Constant Summary collapse

NO_SHARD =
:_no_shard
PRIMARY =
"primary"

Instance Method Summary collapse

Constructor Details

#initializeShardSelection

Returns a new instance of ShardSelection.



8
9
10
11
# File 'lib/active_record_shards/shard_selection.rb', line 8

def initialize
  @on_replica = false
  @shard = nil
end

Instance Method Details

#on_replica=(new_replica) ⇒ Object Also known as: on_slave=



82
83
84
# File 'lib/active_record_shards/shard_selection.rb', line 82

def on_replica=(new_replica)
  @on_replica = (new_replica == true)
end

#on_replica?Boolean Also known as: on_slave?

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_record_shards/shard_selection.rb', line 77

def on_replica?
  @on_replica
end

#optionsObject



87
88
89
# File 'lib/active_record_shards/shard_selection.rb', line 87

def options
  { shard: @shard, replica: @on_replica }
end

#resolve_connection_name(sharded:, configurations:) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_record_shards/shard_selection.rb', line 51

def resolve_connection_name(sharded:, configurations:)
  resolved_shard = sharded ? shard : nil
  env = ActiveRecordShards.app_env

  @connection_names ||= {}
  @connection_names[env] ||= {}
  @connection_names[env][resolved_shard] ||= {}
  @connection_names[env][resolved_shard][@on_replica] ||= begin
    name = env.dup
    name << "_shard_#{resolved_shard}" if resolved_shard
    if @on_replica && configurations["#{name}_replica"]
      "#{name}_replica"
    else
      # ActiveRecord always names its default connection pool 'primary'
      # while everything else is named by the configuration name
      resolved_shard ? name : PRIMARY
    end
  end
end

#shardObject



15
16
17
18
19
20
21
22
23
# File 'lib/active_record_shards/shard_selection.rb', line 15

def shard(klass = nil)
  if (@shard || self.class.default_shard) && (klass.nil? || klass.is_sharded?)
    if @shard == NO_SHARD
      nil
    else
      @shard || self.class.default_shard
    end
  end
end

#shard=(new_shard) ⇒ Object



73
74
75
# File 'lib/active_record_shards/shard_selection.rb', line 73

def shard=(new_shard)
  @shard = (new_shard || NO_SHARD)
end

#shard_name(klass = nil, try_replica = true) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record_shards/shard_selection.rb', line 25

def shard_name(klass = nil, try_replica = true)
  the_shard = shard(klass)

  @shard_names ||= {}
  @shard_names[ActiveRecordShards.app_env] ||= {}
  @shard_names[ActiveRecordShards.app_env][the_shard] ||= {}
  @shard_names[ActiveRecordShards.app_env][the_shard][try_replica] ||= {}
  @shard_names[ActiveRecordShards.app_env][the_shard][try_replica][@on_replica] ||= begin
    s = ActiveRecordShards.app_env.dup
    s << "_shard_#{the_shard}" if the_shard
    s << "_replica"            if @on_replica && try_replica
    s
  end
end