fix(datasource-active-record): map inet/cidr/macaddr(8) columns to String - #377
Conversation
|
Coverage Impact This PR will not change total coverage. 🚦 See full report on Qlty Cloud »🛟 Help
|
|
| pg name | resolved class | .type |
|---|---|---|
inet |
OID::Inet |
:inet ✅ |
cidr |
OID::Cidr |
:cidr ✅ |
macaddr |
OID::Macaddr |
:macaddr ✅ |
macaddr8 |
ActiveModel::Type::Value |
nil ❌ |
The path is deterministic: get_oid_type → load_additional_types only maps an OID when its typname is already a registered key (TypeMapInitializer#run: mapped = nodes.extract! { |row| @store.key? row["typname"] }). macaddr8 isn't, so type_map.fetch falls into the block → Type.default_value → Value#type returns nil.
Concrete effect on this branch. For a real macaddr8 column, TYPES.key?(nil) is false, so we still hit the else in column.rb:31-37 and log:
unknown type '' for field named 'device_mac', 'String' type use by default
Confirmed with a throwaway spec run against this branch (it passes):
column = instance_double(ActiveRecord::ConnectionAdapters::SQLite3::Column,
name: 'device_mac', type: nil) # what the PG adapter actually reports
expect(dummy_class.get_column_type(User, column)).to eq 'String'
expect(logger).to have_received(:log).with(
'Info', "unknown type '' for field named 'device_mac', 'String' type use by default"
)
# => 1 example, 0 failuresSo the misleading INFO log survives for macaddr8, and it's now harder to diagnose than before the PR, since the type name renders empty.
Worth noting the reason this slipped through: the new spec injects type: :macaddr8, a value no adapter ever emits, so it validates the map key that was chosen rather than what the adapter reports. All four cases pass with equal confidence even though one of them is wrong. (inet, cidr and macaddr are all correct — I checked them against the real type map, and the citext/hstore/jsonb analogy in the description holds.)
Two ways to close it
(a) Simplest — drop the macaddr8 entry and remove it from the spec's %i[...] array, then note on #372 that ActiveRecord doesn't map this type, so there's nothing the datasource can key off.
(b) More general — fall back to sql_type when the type doesn't resolve. This covers macaddr8 plus every other unregistered Postgres type (tsrange, domain types, extension types):
def get_column_type(model, column)
return 'Enum' if model.respond_to?(:defined_enums) && model.defined_enums.key?(column.name)
is_array = column.respond_to?(:array) && column.array == true
type = TYPES[column.type] || TYPES[sql_type_key(column)]
unless type
type = TYPES[:string]
ForestAdminAgent::Facades::Container.logger.log(
'Info',
"unknown type '#{column.type || column.sql_type}' for field named '#{column.name}', " \
"'#{TYPES[:string]}' type use by default"
)
end
is_array ? [type] : type
end
private
def sql_type_key(column)
return nil unless column.respond_to?(:sql_type) && column.sql_type
column.sql_type.delete_suffix('[]').to_sym
endThe delete_suffix('[]') matters: for macaddr8[], sql_type is "macaddr8[]". (inet[] is already fine either way — OID::Array does delegate :type, to: :subtype, so column.type == :inet and this PR correctly yields ['String'].)
Everything else in the PR looks good: no behaviour change beyond the log (both branches already returned 'String'), and the new specs are genuine guards — removing the four TYPES entries makes all four fail on expect(logger).not_to have_received(:log).
christophebrun-forest
left a comment
There was a problem hiding this comment.
IMO simple solution : delete macaddr8
ActiveRecord's Postgres adapter never registers macaddr8: column.type comes back nil for a real macaddr8 column (verified against the installed activerecord gem), so TYPES[:macaddr8] could never be hit. It still fell back to String via the unknown-type branch either way -- same runtime behavior as before this fix, just with a TYPES entry that falsely implied the case was handled. Found by @christophebrun-forest in review on #377. inet/cidr/macaddr are confirmed registered and unaffected.
Postgres inet/cidr/macaddr/macaddr8 columns fell through to the unknown-type fallback, logging a misleading INFO message on every schema generation even though the String fallback was already correct. Mirrors the existing citext/hstore/jsonb handling. Fixes #372
ActiveRecord's Postgres adapter never registers macaddr8: column.type comes back nil for a real macaddr8 column (verified against the installed activerecord gem), so TYPES[:macaddr8] could never be hit. It still fell back to String via the unknown-type branch either way -- same runtime behavior as before this fix, just with a TYPES entry that falsely implied the case was handled. Found by @christophebrun-forest in review on #377. inet/cidr/macaddr are confirmed registered and unaffected.
0995097 to
c65ba85
Compare
|
🎉 This PR is included in version 1.39.4 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |

What
Postgres network address column types (
inet,cidr,macaddr,macaddr8) weren't in theTYPESmap incolumn.rb, so they fell through to the unknown-type fallback. The fallback (String) was already correct, but it also logged a misleading INFO message on every schema generation, once per unmapped column:unknown type 'inet' for field named '...', 'String' type use by default.Added the four types to
TYPES, mirroring the existing Postgres-specific handling forcitext/hstore/jsonb.Why
Fixes #372.
How tested
column_spec.rb(one per type) asserting both theStringmapping and that the logger is no longer invoked for these types.forest_admin_datasource_active_recordsuite green locally (191 examples, 0 failures)rubocopclean on the changed files🤖 Generated with Claude Code
Note
Map Postgres
inet,cidr,macaddr, andmacaddr8columns toStringAdds
inet,cidr,macaddr, andmacaddr8to theTYPESmapping in column.rb soget_column_typereturns'String'for these Postgres types instead of falling into the unknown-type logging path. Adds parameterized tests in column_spec.rb verifying the mapping and that no unknown-type log is emitted.Changes since #377 opened
macaddr8type mapping from theTYPESconstant inForestAdminDatasourceActiveRecord::Parser::Columnand updated the corresponding spec [0995097]Macroscope summarized c65ba85.