Module: Switchman::ActiveRecord::ConnectionPool

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

Instance Method Summary collapse

Instance Method Details

#checkout_new_connectionObject



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

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

#connection(switch_shard: true) ⇒ Object



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

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

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

#default_schemaObject



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

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



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

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

  flush
end

#switch_database(conn) ⇒ Object



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

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