Module: Switchman::ActiveRecord::ConnectionPool

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

Instance Method Summary collapse

Instance Method Details

#checkout_new_connectionObject



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

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

#connection(switch_shard: true) ⇒ Object



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

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



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

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

#release_connection(with_id = Thread.current) ⇒ Object



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

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

  flush
end

#switch_database(conn) ⇒ Object



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

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