zilverline/sequent

`type_cast` is deprecated in activerecord 6.1.x

Closed this issue · 6 comments

It seems like the way we cast types for the ReplayOptimizedPostgresPersistor is not reliable:

The type information for type casting is entirely separated to type
object, so if anyone does passing a column to `type_cast` in Rails 6,
they are likely doing something wrong. See the comment for more details:

From: rails/rails@92360e9

Related: https://github.com/rails/rails/blob/28d815b89487ce4001a3f6f0ab684e6f9c017ed0/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb#L33-L42

lvonk commented

The column does not have sufficient type information if the user
provided a custom type on the class level either explicitly (via
Attributes::ClassMethods#attribute) or implicitly (via
AttributeMethods::Serialization::ClassMethods#serialize, +time_zone_aware_attributes+)

This is not really relevant for Sequent, but understand that this is deprecated. Any suggestions how to migrate this?

type_cast_from_column warns against calling it directly on using it, but is available:

https://github.com/rails/rails/blob/28d815b89487ce4001a3f6f0ab684e6f9c017ed0/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb#L33

# If you are having to call this function, you are likely doing something
# wrong. The column does not have sufficient type information if the user
# provided a custom type on the class level either explicitly (via
# Attributes::ClassMethods#attribute) or implicitly (via
# AttributeMethods::Serialization::ClassMethods#serialize, +time_zone_aware_attributes+).
# In almost all cases, the sql type should only be used to change quoting behavior, when the primitive to
# represent the type doesn't sufficiently reflect the differences
# (varchar vs binary) for example. The type used to get this primitive
# should have been provided before reaching the connection adapter.
lvonk commented

Okay, perhaps there is a a higher level type_cast_from_column available which takes these possible custom types into account?

Just discussed this and the active_record_persistor solves this nicely with Arel. See #convert_to_values in lib/sequent/core/persistors/active_record_persistor.rb

lvonk commented

@stephanvd I think you resolved this right?

lvonk commented

This is resolved by using type_cast without a column value, which is not deprecated. See a7f2a98