-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamoid unable to read datetime field stored as Number after version update from 2.2.x to 3.7.x #513
Comments
Could you please provide the exact steps to reproduce the issue? Unfortunately, I didn't manage to reproduce the issue on my own. Model declaration: class TestTimestamp
include Dynamoid::Document
field :timestamp, :datetime
end Create an item. Environment: Dynamoid v2.2, Rails 5, ruby 2.2 require 'pp'
obj = TestTimestamp.create(timestamp: Time.now)
pp Dynamoid.adapter.scan(TestTimestamp.table_name).to_a Result - store timestamp/created_at/updated_at as BigDecimal:
Read items. Environment: Dynamoid v3.7, Rails 6, Ruby 3.0 objs = TestTimestamp.all.to_a
=> [#<TestTimestamp:0x00007fdae64de4f0 @new_record=false, @attributes={:created_at=>Tue, 22 Jun 2021 21:27:28 +0000, :updated_at=>Tue, 22 Jun 2021 21:27:28 +0000, :id=>"8a8d...
objs[0].attributes
=> {:created_at=>Tue, 22 Jun 2021 21:27:28 +0000, :updated_at=>Tue, 22 Jun 2021 21:27:28 +0000, :id=>"8a8de688-448a-43f7-8507-857455f081dc", :timestamp=>Tue, 22 Jun 2021 21:27:28 +0000}
objs[0].timestamp
=> Tue, 22 Jun 2021 21:27:28 +0000 As you can see datetime attributes have correct values. |
The creation part is working fine for me. And when I use the .all API (for reading) then i am able to get the datetime fields. Ex of my query logic : rows = []
result = Aws::DynamoDB::Table.new("TestTimestamp").query({
attributes_to_get: ["id","updated_at","created_at", "timestamp"],
key_conditions: {
"id" => {
attribute_value_list: ["1"],
comparison_operator: "EQ"
}
}
})
result.items.each do |item|
Rails.logger.info item # Here I get datetime fields as BigDecimal
rows << TestTimestamp.new(item)
end
Rails.logger.info rows[0].attributes. # Here I get nil for datatime fields |
Ah, I see. It's an expected behaviour. Type casting was introduced in the 3.0 version (changelog) and it prevents using raw values in some cases. Type casting for |
Is it possible to make some fix so that it can type cast datetime fields ? |
The current approach in typecasting is to copy Rails' behavior. As far as I know, Rails doesn't support type casting of Integer value for a DateTime attribute. I don't have strong arguments against the proposed change but IMHO it makes sense if it's really needed in the community. It can be implemented easily with monkey-patching of the proper type cast class. |
I think this is a valid fix to make as this was supported by previous versions of Dynamoid. |
You can patch type casting of module Dynamoid
module TypeCasting
module DateTimeTypeCastingWithNumericFormat
def process(value)
return Time.at(value.to_i) if value.respond_to?(:to_i)
super
end
end
DateTimeTypeCaster.send(:prepend, DateTimeTypeCastingWithNumericFormat)
end
end
obj = TestTimestamp.new(timestamp: Time.now.to_i)
=> #<TestTimestamp:0x00007fca4a289e18 @new_record=true, @attributes={:timestamp=>2021-06-24 01:31:34 +0300}, @associations={}, @attributes_before_type_cast={:timestamp=>1624... The only issue is that I use |
The datetime fields are not getting converted in Dynamodb model class to Datatime object. Instead they are returning nil object.
Example of use -
class Test
include Dynamoid::Document
table :name => :Table, :key => :Key
field :timestamp, :datetime
end
Field timestamp returns nil object now. Before it used to return Datatime object with version 2.2.x.
This issue I am facing for Magic fields also like created_at, updated_at. Even though they are stored as Number in DDB. They are being returned as nil object.
The text was updated successfully, but these errors were encountered: