Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PostgreSQL] Replace deprecated PG constants. #28522

Merged
merged 1 commit into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def verify!(*ignored)

# Provides access to the underlying database driver for this adapter. For
# example, this method returns a Mysql2::Client object in case of Mysql2Adapter,
# and a PGconn object in case of PostgreSQLAdapter.
# and a PG::Connection object in case of PostgreSQLAdapter.
#
# This is useful for when you need to call a proprietary method such as
# PostgreSQL's lo_* methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Bytea < Type::Binary # :nodoc:
def deserialize(value)
return if value.nil?
return value.to_s if value.is_a?(Type::Binary::Data)
PGconn.unescape_bytea(super)
PG::Connection.unescape_bytea(super)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def quote_table_name(name) # :nodoc:

# Quotes schema names for use in SQL queries.
def quote_schema_name(name)
PGconn.quote_ident(name)
PG::Connection.quote_ident(name)
end

def quote_table_name_for_assignment(table, attr)
Expand All @@ -42,7 +42,7 @@ def quote_table_name_for_assignment(table, attr)

# Quotes column names for use in SQL queries.
def quote_column_name(name) # :nodoc:
@quoted_column_names[name] ||= PGconn.quote_ident(super).freeze
@quoted_column_names[name] ||= PG::Connection.quote_ident(super).freeze
end

# Quote date/time values for use in SQL input.
Expand Down Expand Up @@ -105,7 +105,7 @@ def _type_cast(value)
case value
when Type::Binary::Data
# Return a bind param hash with format as binary.
# See http://deveiate.org/code/pg/PGconn.html#method-i-exec_prepared-doc
# See https://deveiate.org/code/pg/PG/Connection.html#method-i-exec_prepared-doc
# for more information
{ value: value.to_s, format: 1 }
when OID::Xml::Data, OID::Bit::Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def to_s

def quoted
if schema
PGconn.quote_ident(schema) << SEPARATOR << PGconn.quote_ident(identifier)
PG::Connection.quote_ident(schema) << SEPARATOR << PG::Connection.quote_ident(identifier)
else
PGconn.quote_ident(identifier)
PG::Connection.quote_ident(identifier)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def postgresql_connection(config)
conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]

# Forward only valid config params to PGconn.connect.
valid_conn_param_keys = PGconn.conndefaults_hash.keys + [:requiressl]
# Forward only valid config params to PG::Connection.connect.
valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
conn_params.slice!(*valid_conn_param_keys)

# The postgres drivers don't allow the creation of an unconnected PGconn object,
# The postgres drivers don't allow the creation of an unconnected PG::Connection object,
# so just pass a nil connection object for the time being.
ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, conn_params, config)
end
Expand Down Expand Up @@ -201,8 +201,8 @@ def dealloc(key)
end

def connection_active?
@connection.status == PGconn::CONNECTION_OK
rescue PGError
@connection.status == PG::CONNECTION_OK
rescue PG::Error
false
end
end
Expand Down Expand Up @@ -249,7 +249,7 @@ def truncate(table_name, name = nil)
def active?
@connection.query "SELECT 1"
true
rescue PGError
rescue PG::Error
false
end

Expand Down Expand Up @@ -414,7 +414,7 @@ def default_index_type?(index) # :nodoc:
def translate_exception(exception, message)
return exception unless exception.respond_to?(:result)

case exception.result.try(:error_field, PGresult::PG_DIAG_SQLSTATE)
case exception.result.try(:error_field, PG::PG_DIAG_SQLSTATE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PGresult has been renamed to PG::Result source

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, PG_DIAG_SQLSTATE is first defined as PG::Constants::PG_DIAG_SQLSTATE but PG::Constants is included into PG , PG::Result and PG::Connection . Each of these would work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just noticed this. It wasn't so obvious for the first time.

when UNIQUE_VIOLATION
RecordNotUnique.new(message)
when FOREIGN_KEY_VIOLATION
Expand Down Expand Up @@ -651,7 +651,7 @@ def exec_cache(sql, name, binds)
CACHED_PLAN_HEURISTIC = "cached plan must not change result type".freeze
def is_cached_plan_failure?(e)
pgerror = e.cause
code = pgerror.result.result_error_field(PGresult::PG_DIAG_SQLSTATE)
code = pgerror.result.result_error_field(PG::PG_DIAG_SQLSTATE)
code == FEATURE_NOT_SUPPORTED && pgerror.message.include?(CACHED_PLAN_HEURISTIC)
rescue
false
Expand Down Expand Up @@ -690,7 +690,7 @@ def prepare_statement(sql)
# 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 = PG.connect(@connection_parameters)
configure_connection
rescue ::PG::Error => error
if error.message.include?("does not exist")
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module Transactions
# # statement will cause a PostgreSQL error, even though the unique
# # constraint is no longer violated:
# Number.create(i: 1)
# # => "PGError: ERROR: current transaction is aborted, commands
# # => "PG::Error: ERROR: current transaction is aborted, commands
# # ignored until end of transaction block"
# end
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
class InactivePGconn
class InactivePgConnection
def query(*args)
raise PGError
raise PG::Error
end

def status
PGconn::CONNECTION_BAD
PG::CONNECTION_BAD
end
end

Expand All @@ -31,7 +31,7 @@ def test_cache_is_per_pid
end

def test_dealloc_does_not_raise_on_inactive_connection
cache = StatementPool.new InactivePGconn.new, 10
cache = StatementPool.new InactivePgConnection.new, 10
cache["foo"] = "bar"
assert_nothing_raised { cache.clear }
end
Expand Down