Module: Switchman::ActiveRecord::ConnectionPool

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

Instance Method Summary collapse

Instance Method Details

#checkout_new_connectionObject



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

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

#connection(switch_shard: true) ⇒ Object



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

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



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

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



44
45
46
47
48
# File 'lib/switchman/active_record/connection_pool.rb', line 44

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
19
20
# File 'lib/switchman/active_record/connection_pool.rb', line 14

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



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

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