Skip to content
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

WIP: Allow created_at to be passed by app #17

Closed
wants to merge 1 commit into from

Conversation

andrepiske
Copy link
Collaborator

@andrepiske andrepiske commented Jan 23, 2025

This PR changes the trigger function so that the resulting created_at attribute of a irontrail_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 for created_at:

  1. If _created_at is set in the metadata map, then:
  2. Use its value (must be a float number with epoch time, like in ruby's Time.now.to_f) to fill the created_at attribute
  3. Set the _db_created_at key in the metadata to NOW(), so preserving what would have been filled by postgres. This could be useful for debugging.
  4. Else (if _created_at is not set in the metadata map), then just use NOW() for the value of created_at

Caveats:

  • This inserts another point of failure in the trigger function. If the value of _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 do IronTrail.store_metadata(:_created_at, some_value_here) but to use something like IronTrail.use_soft_created_at(some_time_object) (that's probably a bad name)

Related tasks:

@andrepiske
Copy link
Collaborator Author

Closing in favor of #18

@andrepiske andrepiske closed this Jan 28, 2025
@andrepiske andrepiske deleted the allow-soft-created_at branch January 28, 2025 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant