Module: Switchman::ActiveRecord::ConnectionPool

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

Instance Method Summary collapse

Instance Method Details

#checkout_new_connectionObject



28
29
30
31
32
# File 'lib/switchman/active_record/connection_pool.rb', line 28

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

#connection(switch_shard: true) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/switchman/active_record/connection_pool.rb', line 34

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

rubocop:enable Naming/AccessorMethodName override method



21
22
23
24
25
26
# File 'lib/switchman/active_record/connection_pool.rb', line 21

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



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

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



42
43
44
45
46
# File 'lib/switchman/active_record/connection_pool.rb', line 42

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

  flush
end

#set_schema_cache(cache) ⇒ Object

rubocop:disable Naming/AccessorMethodName override method



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

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

#switch_database(conn) ⇒ Object



48
49
50
51
52
# File 'lib/switchman/active_record/connection_pool.rb', line 48

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

  conn.shard = current_shard
end