instance method
configure_connection
Ruby on Rails edge
Private — implementation detail, not part of the public APISignature
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