Skip to content
Browse files

PostgreSQL, warn once per connection per missing OID. Closes #14275.

[Yves Senn & Matthew Draper]

(cherry picked from commit 9f62344)

This is a partial backport of #14692.

Conflicts:
    activerecord/CHANGELOG.md
    activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
    activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
  • Loading branch information...
1 parent b9f6d14 commit 461171b13f7ab7154af5ebb623e3ef3e303c0181 @senny senny committed Apr 13, 2014
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* PostgreSQL adapter only warns once for every missing OID per connection.
+
+ Fixes #14275.
+
+ *Matthew Draper*, *Yves Senn*
+
* Fixed error for aggregate methods (`empty?`, `any?`, `count`) with `select`
which created invalid SQL.
View
5 activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
@@ -142,10 +142,7 @@ def exec_query(sql, name = 'SQL', binds = [])
fields.each_with_index do |fname, i|
ftype = result.ftype i
fmod = result.fmod i
- types[fname] = type_map.fetch(ftype, fmod) { |oid, mod|
- warn "unknown OID: #{fname}(#{oid}) (#{sql})"
- OID::Identity.new
- }
+ types[fname] = get_oid_type(ftype, fmod, fname)
end
ret = ActiveRecord::Result.new(fields, result.values, types)
View
4 activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -185,9 +185,7 @@ def indexes(table_name, name = nil)
def columns(table_name)
# Limit, precision, and scale are all handled by the superclass.
column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod|
- oid = type_map.fetch(oid.to_i, fmod.to_i) {
- OID::Identity.new
- }
+ oid = get_oid_type(oid.to_i, fmod.to_i, column_name)
PostgreSQLColumn.new(column_name, default, oid, type, notnull == 'f')
end
end
View
7 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -760,6 +760,13 @@ def type_map
@type_map
end
+ def get_oid_type(oid, fmod, column_name)
+ type_map.fetch(oid, fmod) {
+ warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String."
+ type_map[oid] = OID::Identity.new
+ }
+ end
+
def reload_type_map
type_map.clear
initialize_type_map(type_map)
View
9 activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -309,6 +309,15 @@ def test_raise_error_when_cannot_translate_exception
end
end
+ def test_only_warn_on_first_encounter_of_unknown_oid
+ warning = capture(:stderr) {
+ @connection.select_all "SELECT NULL::anyelement"
+ @connection.select_all "SELECT NULL::anyelement"
+ @connection.select_all "SELECT NULL::anyelement"
+ }
+ assert_match(/\Aunknown OID \d+: failed to recognize type of 'anyelement'. It will be treated as String.\n\z/, warning)
+ end
+
private
def insert(ctx, data)
binds = data.map { |name, value|

0 comments on commit 461171b

Please sign in to comment.
Something went wrong with that request. Please try again.