Sha256: 05f401e8b2e2a9cc55d964f79afbbd4884ea4532e1fe88ab9385c2ded4a89b26

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Switchman
  module ActiveRecord
    module ConnectionPool
      def get_schema_cache(connection)
        self.schema_cache ||= SharedSchemaCache.get_schema_cache(connection)
        self.schema_cache.connection = connection

        self.schema_cache
      end

      # rubocop:disable Naming/AccessorMethodName override method
      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
      # rubocop:enable Naming/AccessorMethodName override method

      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

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

      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

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

        flush
      end

      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

      private

      def current_shard
        ::Rails.version < '7.0' ? connection_klass.current_switchman_shard : connection_class.current_switchman_shard
      end

      def tls_key
        "#{object_id}_shard".to_sym
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switchman-3.3.4 lib/switchman/active_record/connection_pool.rb