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

Fix inconsistent timestamp values #1784

Merged
merged 6 commits into from Mar 9, 2018

Conversation

Projects
1 participant
@ricardograca
Member

ricardograca commented Mar 7, 2018

Introduction

Ensure date values of automatic timestamps are always returned in a consistent format.

Motivation

When using SQLite dates are returned as Unix timestamps when fetching data from the database, but when a model is created and saved they are stored as Date objects on the model. This creates inconsistency when dealing with dates between saving and fetching, which leads to users having to deal with this inconsistency themselves. However it should be the ORM's job to deal with these issues and provide a consistent interface for users. See the above related issue for more info.

Fixes #1068.

Proposed solution

This adds a new method to format the dates whenever a model has the hasTimestamps attribute set to true or an array of key names. Unlike the previous PR this doesn't wrap existing methods (parse) and just calls the new method when needed. The advantage of this approach is that the parse method is kept untouched which is a positive thing since it's already complicated and tangled as it is.

Current PR Issues

Doesn't format other date columns the user might have set on the database, but since Bookshelf doesn't know about the database structure it wouldn't be feasible anyway.

There's also a problem in the way that MySQL handles dates that causes millisecond information to be discarded when saving to the database. This can be fixed but requires a change in Knex and there's nothing we can do on our side except for recommending that users use a custom date format in migrations when dealing with MySQL.

ricardograca added some commits Mar 4, 2018

Add model method to get timestamp key names
- This removes the code duplication in Model and ModelBase objects.
Ensure timestamps are fetched in consistent format
- This coerces a model's timestamps to a consistent Date format for all
fetch operations across all database implementations. Previously dates
would be returned as Unix timestamps after a fetch on SQLite, but they
would be a regular Date object right after model creation.

- There's currently a bug in knex that prevents this from working
correctly for MySQL, since timestamp columns will discard milisecond
information and round to the nearest second.

@ricardograca ricardograca added this to To Do in Version 0.13.0 via automation Mar 7, 2018

@ricardograca ricardograca moved this from To Do to In Progress in Version 0.13.0 Mar 7, 2018

@ricardograca ricardograca merged commit 904abcf into master Mar 9, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

Version 0.13.0 automation moved this from In Progress to Done Mar 9, 2018

@ricardograca ricardograca deleted the rg-consistent-timestamps branch Mar 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment