instance method configure_connection

Ruby on Rails edge

Private — implementation detail, not part of the public API

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.11.3 v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

configure_connection()

Configures the encoding, verbosity, schema search path, and time zone of the connection. This is called by #connect and should not be called manually.

Source
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 1149
        def configure_connection
          super

          if @config[:encoding]
            @raw_connection.set_client_encoding(@config[:encoding])
          end

          if @config[:error_verbosity]
            @raw_connection.set_error_verbosity(@config[:error_verbosity])
          end

          @notice_receiver_fatal_error = nil
          @raw_connection.set_notice_receiver do |result|
            next if capture_fatal_notice(result)

            if ActiveRecord.db_warnings_action
              message = result.error_field(PG::Result::PG_DIAG_MESSAGE_PRIMARY)
              code = result.error_field(PG::Result::PG_DIAG_SQLSTATE)
              level = result.error_field(PG::Result::PG_DIAG_SEVERITY)
              @notice_receiver_sql_warnings << SQLWarning.new(message, code, level, nil, @pool)
            end
          end

          # Build a single hash of all settings we want to configure, then
          # set them all through internal_set_config which can skip redundant
          # SETs by checking parameter_status.
          settings = {}

          # We normalize to lowercase to dedup against built-ins, but retain
          # the user's spelling just in case.
          original_variable_names = CANONICAL_GUC_NAMES.dup

          # Use standard-conforming strings so we don't have to do the E'...' dance.
          settings["standard_conforming_strings"] = "on" unless @config[:standard_conforming_strings] == false

          # Set interval output format to ISO 8601 for ease of parsing by ActiveSupport::Duration.parse
          settings["intervalstyle"] = "iso_8601" unless @config[:intervalstyle] == false

          unless @config[:min_messages] == false
            settings["client_min_messages"] = @config[:min_messages] || "warning"
          end

          # https://www.postgresql.org/docs/current/static/sql-set.html
          @config.fetch(:variables, {}).stringify_keys.each do |k, v|
            lower = k.dup
            original_variable_names[lower] ||= k if lower.downcase!

            case v
            when :default, ":default"
              # Server default; remove any Rails-default we've set above
              settings.delete(lower)
            when nil
              # Rails default
            else
              settings[lower] = v
            end
          end

          server_default_tz = @raw_connection.parameter_status("TimeZone")
          @static_timezone = settings.key?("timezone") ||
            %w[UTC Etc/UTC].include?(server_default_tz)

          settings["timezone"] = "UTC" if default_timezone == :utc && !@static_timezone

          settings.each do |lower, value|
            internal_set_config(original_variable_names[lower] || lower, value)
          end

          # search_path uses unquoted, comma-separated identifiers so it
          # goes through the existing setter rather than internal_set_config.
          unless @config[:schema_search_path] == false
            if @config[:schema_order]
              ActiveRecord.deprecator.warn(<<~MSG.squish)
                The `schema_order` option in PostgreSQL database configurations is
                deprecated and will be removed in Rails 9.0. Use `schema_search_path` instead.
              MSG
            end
            self.schema_search_path = @config[:schema_search_path] || @config[:schema_order]
          end

          add_pg_encoders
          add_pg_decoders

          schema_search_path # populate cache

          reload_type_map
        end

Defined in activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb line 1149 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close