Module: Switchman::ActiveRecord::ConnectionPool
- Defined in:
- lib/switchman/active_record/connection_pool.rb
Instance Method Summary collapse
- #active_connection(switch_shard: true) ⇒ Object
- #checkout_new_connection ⇒ Object
- #connection(switch_shard: true) ⇒ Object
- #default_schema ⇒ Object
- #lease_connection(switch_shard: true) ⇒ Object
- #release_connection(with_id = Thread.current) ⇒ Object
- #switch_database(conn) ⇒ Object
- #with_connection(switch_shard: true, **kwargs) ⇒ Object
Instance Method Details
#active_connection(switch_shard: true) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/switchman/active_record/connection_pool.rb', line 29 def active_connection(switch_shard: true) conn = super() return nil if conn.nil? raise Errors::NonExistentShardError if current_shard.new_record? switch_database(conn) if conn.shard != current_shard && switch_shard conn end |
#checkout_new_connection ⇒ Object
14 15 16 17 18 |
# File 'lib/switchman/active_record/connection_pool.rb', line 14 def checkout_new_connection conn = super conn.shard = current_shard conn end |
#connection(switch_shard: true) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/switchman/active_record/connection_pool.rb', line 20 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_schema ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/switchman/active_record/connection_pool.rb', line 6 def default_schema connection_method = (::Rails.version < "7.2") ? :connection : :lease_connection send(connection_method) unless @schemas # default shard will not switch databases immediately, so it won't be set yet @schemas ||= send(connection_method).current_schemas @schemas.first end |
#lease_connection(switch_shard: true) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/switchman/active_record/connection_pool.rb', line 38 def lease_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 |
#release_connection(with_id = Thread.current) ⇒ Object
56 57 58 59 60 |
# File 'lib/switchman/active_record/connection_pool.rb', line 56 def release_connection(with_id = Thread.current) super flush end |
#switch_database(conn) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/switchman/active_record/connection_pool.rb', line 62 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 |
#with_connection(switch_shard: true, **kwargs) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/switchman/active_record/connection_pool.rb', line 46 def with_connection(switch_shard: true, **kwargs) super(**kwargs) do |conn| raise Errors::NonExistentShardError if current_shard.new_record? switch_database(conn) if conn.shard != current_shard && switch_shard yield conn end end |