Skip to content

Use correct precision. - #57

Merged
osyo-manga merged 2 commits into
kufu:masterfrom
alpaca-tc:use_correct_prescision
Jul 9, 2020
Merged

osyo-manga merged 2 commits into
kufu:masterfrom
alpaca-tc:use_correct_prescision

Conversation

@alpaca-tc

@alpaca-tc alpaca-tc commented Jul 7, 2020 •

Copy link
Copy Markdown
Contributor

Thank you for the great gem :)

ActiveRecord::Attributes.attribute overrides the type of existing attributes. It isn't considered an expected behavior because database's subsecond precision is ignored by type caster.
In default mysql, datetime should be rounded nsec because the default precision is 0.

Employee.column_for_attribute(:valid_from).precision #=> 0

ActiveRecord::Bitemporal.valid_at(time) do
  employee = Employee.create!(...)
  employee.valid_from.nsec #=> 481148000. expected 0.
  employee.reload
  employee.valid_from.nsec #=> 0
end

Redefine the type in load_schema to use the correct precision dynamically fetched from the database.

@ghost
ghost requested review from sugamasao and wakasa51 July 7, 2020 13:05
@alpaca-tc
alpaca-tc force-pushed the use_correct_prescision branch 2 times, most recently from 18f4457 to f676530 Compare July 7, 2020 15:16
The expected result of `employee_deleted_at` is deleted record. However,
whether or not the record is at the beginning of collection depends on the optimizer.
Therefore, the rows should be filtered or ordered to return the same result.
@alpaca-tc
alpaca-tc force-pushed the use_correct_prescision branch from f676530 to da54ac3 Compare July 7, 2020 15:16

@wakasa51 wakasa51 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this pull request!!
LGTM 👍

@sugamasao sugamasao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for Great Pull Request 👍
LGTM 🎉

@osyo-manga
osyo-manga merged commit fc40b2e into kufu:master Jul 9, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants