WIP: Allow created_at to be passed by app #17
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the trigger function so that the resulting
created_at
attribute of airontrail_changes
record can be changed by the app by passing it in the_created_at
metadata key.This effectively allows apps to bypass postgres'
NOW()
function and inject any time they'd like. This is useful when creating tests that depend on mocked data. Even though it can be used in production too, it is not recommended.The trigger function would use postgres'
NOW()
function to fill in the created_at attribute. Now, it'd to the following logic to determine the value forcreated_at
:_created_at
is set in the metadata map, then:Time.now.to_f
) to fill thecreated_at
attribute_db_created_at
key in the metadata toNOW()
, so preserving what would have been filled by postgres. This could be useful for debugging._created_at
is not set in the metadata map), then just useNOW()
for the value ofcreated_at
Caveats:
_created_at
isn't a floating point number, it'll crash and thus the record will end up in the errors table (irontrail_trigger_errors
). The solution as I see it is to validate this in ruby's side by having a proper interface to set that value. i.e. we don't want users to doIronTrail.store_metadata(:_created_at, some_value_here)
but to use something likeIronTrail.use_soft_created_at(some_time_object)
(that's probably a bad name)Related tasks: