Skip to content

Allow lazy construct attribute type after load_schema! - #39797

Closed
kamipo wants to merge 1 commit into
rails:masterfrom
kamipo:lazy_construct_attribute_type
Closed

kamipo wants to merge 1 commit into
rails:masterfrom
kamipo:lazy_construct_attribute_type

Conversation

@kamipo

@kamipo kamipo commented Jul 7, 2020

Copy link
Copy Markdown
Member

I've been reported this issue from a friend.

We'd like to define attribute by mixin, espetially to specify/overload
default value.

e.g.

module Mod
  included do
    attribute :reserved_column, :datetime, default: SENTINEL_VALUE
  end
end

Unfortunately, it will lose the original datetime precision.

I suggested using define_attribute and type_for_attribute
(define_attribute :attr, type_for_attribute(:attr), default: "..."),
but it will force load_schema! at mixin time.

It means that there is no way that overload default but maintain the
original type without force load_schema! happened.

So I'd propose a way to allow lazy construct attribute type after
load_schema!, it makes us achieve the purpose.

module Mod
  included do
    attribute :reserved_column, default: SENTINEL_VALUE do |name|
      type_for_attribute(name)
    end
  end
end

I've been reported this issue from a friend.

We'd like to define attribute by mixin, espetially to specify/overload
default value.

e.g.

```ruby
module Mod
  included do
    attribute :reserved_column, :datetime, default: SENTINEL_VALUE
  end
end
```

Unfortunately, it will lose the original datetime precision.

I suggested using `define_attribute` and `type_for_attribute`
(`define_attribute :attr, type_for_attribute(:attr), default: "..."`),
but it will force `load_schema!` at mixin time.

It means that there is no way that overload default but maintain the
original type without force `load_schema!` happened.

So I'd propose a way to allow lazy construct attribute type after
`load_schema!`, it makes us achieve the purpose.

```ruby
module Mod
  included do
    attribute :reserved_column, default: SENTINEL_VALUE do |name|
      type_for_attribute(name)
    end
  end
end
```
@eugeneius

Copy link
Copy Markdown
Member

How about reusing the database type in attribute if cast_type is omitted, rather than removing all typecasting:

diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index dc3cfed88f..79e3b685e6 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -205,7 +205,7 @@ module ClassMethods
       # tracking is performed. The methods +changed?+ and +changed_in_place?+
       # will be called from ActiveModel::Dirty. See the documentation for those
       # methods in ActiveModel::Type::Value for more details.
-      def attribute(name, cast_type = Type::Value.new, **options)
+      def attribute(name, cast_type = nil, **options)
         name = name.to_s
         reload_schema_from_cache

@@ -246,6 +246,8 @@ def define_attribute(
       def load_schema! # :nodoc:
         super
         attributes_to_define_after_schema_loads.each do |name, (type, options)|
+          type ||= type_for_attribute(name) || Type::Value.new
+
           if type.is_a?(Symbol)
             adapter_name = ActiveRecord::Type.adapter_name_from(self)
             type = ActiveRecord::Type.lookup(type, **options.except(:default), adapter: adapter_name)

I know this is a change in behaviour, so it would need a deprecation cycle, but the existing behaviour doesn't seem practically useful to me, and the deprecation would be easy to handle (pass Type::Value.new explicitly). The use case described here (overriding the column's default value but keeping its type) is much more realistic, and I don't think we should force users to look up the type themselves just to maintain questionable behaviour.

@eugeneius

Copy link
Copy Markdown
Member

Feel free to merge if you're not interested in my suggestion, by the way. This change is backwards-compatible, and #39820 shows that looking up the type with a block is useful in other places too. Ideally I don't think users should need to call type_for_attribute manually if they're not changing the typecasting behaviour, but I can propose my own idea as a followup.

@kamipo

kamipo commented Jul 13, 2020

Copy link
Copy Markdown
Member Author

Sorry for the delay, I had almost no time last week.

I agree that the current behavior is not practically useful, so I've create an alternative #39830 to achieve the purpose.

kamipo added a commit to kamipo/rails that referenced this pull request Jul 13, 2020
```ruby
class Post < ActiveRecord::Base
  attribute :written_at, default: -> { Time.now.utc }
end

# Rails 6.0
Post.type_for_attribute(:written_at) # => #<Type::Value ... precision: nil, ...>

# Rails 6.1
Post.type_for_attribute(:written_at) # => #<Type::DateTime ... precision: 6, ...>
```

This is an alternative of rails#39797.

Context rails#39797 (comment).

If people intend to override the existing type on the attribute, usually
an overriding type is explicitly specified, so I agree that the current
behavior (drop existing type information if type is omitted) is
practically quite useless, and it is almost like a bug in that there is
no way to override just the default.

So I'd like to change the current behavior on existing attributes
without a deprecation as a fix.

Closes rails#39797.

See also kufu/activerecord-bitemporal#57.
@kamipo
kamipo deleted the lazy_construct_attribute_type branch July 13, 2020 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants