Permalink
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

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
	activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
	activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
  • Loading branch information...
1 parent d0e5963 commit 47f717180c165ab605ce00ad5b0462375227769c @senny senny committed Apr 13, 2014
@@ -1,3 +1,9 @@
+* PostgreSQL adapter only warns once for every missing OID per connection.
+
+ Fixes #14275.
+
+ *Matthew Draper*, *Yves Senn*
+
* Fix insertion of records via `has_many :through` association with scope.
Fixes #3548.
@@ -143,10 +143,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] = OID::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)
@@ -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 = 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
@@ -749,6 +749,13 @@ def translate_exception(exception, message)
private
+ def get_oid_type(oid, fmod, column_name)
+ OID::TYPE_MAP.fetch(oid, fmod) {
+ warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String."
+ OID::TYPE_MAP[oid] = OID::Identity.new
+ }
+ end
+
def reload_type_map
OID::TYPE_MAP.clear
initialize_type_map
@@ -311,6 +311,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 47f7171

Please sign in to comment.