Module: Switchman::ActiveRecord::ConnectionPool

Defined in:
lib/switchman/active_record/connection_pool.rb

Instance Method Summary collapse

Instance Method Details

#checkout_new_connectionObject



32
33
34
35
36
# File 'lib/switchman/active_record/connection_pool.rb', line 32

def checkout_new_connection
  conn = super
  conn.shard = current_shard
  conn
end

#connection(switch_shard: true) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/switchman/active_record/connection_pool.rb', line 38

def connection(switch_shard: true)
  conn = super()
  raise Errors::NonExistentShardError if current_shard.new_record?

  switch_database(conn) if conn.shard != current_shard && switch_shard
  conn
end

#default_schemaObject



25
26
27
28
29
30
# File 'lib/switchman/active_record/connection_pool.rb', line 25

def default_schema
  connection unless @schemas
  # default shard will not switch databases immediately, so it won't be set yet
  @schemas ||= connection.current_schemas
  @schemas.first
end

#get_schema_cache(connection) ⇒ Object



7
8
9
10
11
12
# File 'lib/switchman/active_record/connection_pool.rb', line 7

def get_schema_cache(connection)
  self.schema_cache ||= SharedSchemaCache.get_schema_cache(connection)
  self.schema_cache.connection = connection

  self.schema_cache
end

#release_connection(with_id = Thread.current) ⇒ Object



46
47
48
49
50
# File 'lib/switchman/active_record/connection_pool.rb', line 46

def release_connection(with_id = Thread.current)
  super(with_id)

  flush
end

#set_schema_cache(cache) ⇒ Object

rubocop:disable Naming/AccessorMethodName override method



15
16
17
18
19
20
21
# File 'lib/switchman/active_record/connection_pool.rb', line 15

def set_schema_cache(cache)
  schema_cache = get_schema_cache(cache.connection)

  cache.instance_variables.each do |x|
    schema_cache.instance_variable_set(x, cache.instance_variable_get(x))
  end
end

#switch_database(conn) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/switchman/active_record/connection_pool.rb', line 52

def switch_database(conn)
  if !@schemas && conn.adapter_name == "PostgreSQL" && !current_shard.database_server.config[:shard_name]
    @schemas = conn.current_schemas
  end

  conn.shard = current_shard
end