From 8ee6406acc67aa721127d69486bf8e244ea6d576 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Tue, 3 Jan 2012 16:57:34 +0100 Subject: Postgresql: Allow setting of any libpq connection parameters --- .../connection_adapters/postgresql_adapter.rb | 40 +++++++++++++--------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index d2361b5378..859987fbe9 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -11,20 +11,19 @@ module ActiveRecord # Establishes a connection to the database that's used by all Active Record objects def postgresql_connection(config) # :nodoc: config = config.symbolize_keys - host = config[:host] - port = config[:port] || 5432 - username = config[:username].to_s if config[:username] - password = config[:password].to_s if config[:password] - if config.key?(:database) - database = config[:database] - else - raise ArgumentError, "No database specified. Missing argument: database." - end + # Forward any unused config params to PGconn.connect. + conn_params = config.except(:statement_limit, :encoding, :min_messages, + :schema_search_path, :schema_order, + :adapter, :pool, :wait_timeout) + + # Map ActiveRecords param names to PGs. + conn_params[:user] = conn_params.delete(:username) if conn_params[:username] + conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database] # The postgres drivers don't allow the creation of an unconnected PGconn object, # so just pass a nil connection object for the time being. - ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, [host, port, nil, nil, database, username, password], config) + ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, conn_params, config) end end @@ -226,22 +225,29 @@ module ActiveRecord end end - # The PostgreSQL adapter works both with the native C (http://ruby.scripting.ca/postgres/) and the pure - # Ruby (available both as gem and from http://rubyforge.org/frs/?group_id=234&release_id=1944) drivers. + # The PostgreSQL adapter works with the native C (https://bitbucket.org/ged/ruby-pg) driver. # # Options: # - # * :host - Defaults to "localhost". + # * :host - Defaults to a Unix-domain socket in /tmp. On machines without Unix-domain sockets, + # the default is to connect to localhost. # * :port - Defaults to 5432. - # * :username - Defaults to nothing. - # * :password - Defaults to nothing. - # * :database - The name of the database. No default, must be provided. + # * :username - Defaults to be the same as the operating system name of the user running the application. + # * :password - Password to be used if the server demands password authentication. + # * :database - Defaults to be the same as the user name. # * :schema_search_path - An optional schema search path for the connection given # as a string of comma-separated schema names. This is backward-compatible with the :schema_order option. # * :encoding - An optional client encoding that is used in a SET client_encoding TO # call on the connection. # * :min_messages - An optional client min messages that is used in a # SET client_min_messages TO call on the connection. + # + # Any further options are used as connection parameters to libpq. See + # http://www.postgresql.org/docs/9.1/static/libpq-connect.html for the + # list of parameters. + # + # In addition, default connection parameters of libpq can be set per environment variables. + # See http://www.postgresql.org/docs/9.1/static/libpq-envars.html . class PostgreSQLAdapter < AbstractAdapter class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition def xml(*args) @@ -1200,7 +1206,7 @@ module ActiveRecord # Connects to a PostgreSQL server and sets up the adapter depending on the # connected server's characteristics. def connect - @connection = PGconn.connect(*@connection_parameters) + @connection = PGconn.connect(@connection_parameters) # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision -- cgit v1.2.3 From 92a3c487bb0d125c437e53a7f45c31fcca97f2d9 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Thu, 5 Jan 2012 12:58:51 +0100 Subject: Postgresql: restore previous behaviour regarding to nil values in connection specification Hash --- activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 859987fbe9..cf86caa21e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -16,6 +16,7 @@ module ActiveRecord conn_params = config.except(:statement_limit, :encoding, :min_messages, :schema_search_path, :schema_order, :adapter, :pool, :wait_timeout) + conn_params.delete_if { |k,v| v.nil? } # Map ActiveRecords param names to PGs. conn_params[:user] = conn_params.delete(:username) if conn_params[:username] -- cgit v1.2.3